我正在使用grunt,我想按特定顺序在某个目录中合并所有js文件(这是一个有角度的js应用程序,因此我想先进行模块定义,然后再进行其他操作)。我的咕unt连音目标看起来像:
concat: {
mobile: {
expand: true,
cwd: "static/javascript/mobile/app/",
src: ["main-module.js", "**/*-module.js", "**/*.js", "!static/javascript/mobile/dist/*"],
dest: "static/javascript/mobile/app/dist/ngmobile.concat.js"
}
}
上面的配置似乎应该先连接main-module.js,再连接所有其他module.js文件,再连接其他所有文件,并忽略dist文件夹中的所有文件。但是,当使用--verbose运行grunt时,结果如下所示:
Running "concat:mobile" (concat) task
Verifying property concat.mobile exists in config...OK
Files: static/javascript/mobile/app/main-module.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/main-module.js
Files: static/javascript/mobile/app/clients/clients-module.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/clients/clients-module.js
Files: static/javascript/mobile/app/reports/reports-module.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/reports/reports-module.js
Files: static/javascript/mobile/app/schedules/schedules-module.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/schedules/schedules-module.js
Files: static/javascript/mobile/app/services/services-module.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/services/services-module.js
Files: static/javascript/mobile/app/clients/addclient-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/clients/addclient-ctrl.js
Files: static/javascript/mobile/app/clients/clientprofile-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/clients/clientprofile-ctrl.js
Files: static/javascript/mobile/app/clients/clients-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/clients/clients-ctrl.js
Files: static/javascript/mobile/app/clients/clients-service.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/clients/clients-service.js
Files: static/javascript/mobile/app/common/directives.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/common/directives.js
Files: static/javascript/mobile/app/common/filters.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/common/filters.js
Files: static/javascript/mobile/app/common/header-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/common/header-ctrl.js
Files: static/javascript/mobile/app/common/navbarcollapse-directive.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/common/navbarcollapse-directive.js
Files: static/javascript/mobile/app/dist/ngmobile.concat.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/dist/ngmobile.concat.js
Files: static/javascript/mobile/app/main-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/main-ctrl.js
Files: static/javascript/mobile/app/main-service.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/main-service.js
Files: static/javascript/mobile/app/reports/reports-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/reports/reports-ctrl.js
Files: static/javascript/mobile/app/schedules/schedules-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/schedules/schedules-ctrl.js
Files: static/javascript/mobile/app/schedules/schedules-service.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/schedules/schedules-service.js
Files: static/javascript/mobile/app/services/addservice-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/services/addservice-ctrl.js
Files: static/javascript/mobile/app/services/serviceprofile-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/services/serviceprofile-ctrl.js
Files: static/javascript/mobile/app/services/services-ctrl.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/services/services-ctrl.js
Files: static/javascript/mobile/app/services/services-service.js -> static/javascript/mobile/app/dist/ngmobile.concat.js/services/services-service.js
Options: separator="\n", banner="", footer="", stripBanners=false, process=false
Reading static/javascript/mobile/app/main-module.js...OK
Writing static/javascript/mobile/app/dist/ngmobile.concat.js/main-module.js...ERROR
Warning: Unable to write "static/javascript/mobile/app/dist/ngmobile.concat.js/main-module.js" file (Error code: ENOTDIR). Use --force to continue.
Aborted due to warnings.
这告诉我,它正在查找要查找的文件,然后尝试将它们写入(复制?)到dest中指定的文件路径中。我怎么会这么严重地弄错呢?:)如果有人愿意把最后几根头发留在我的头上,我将非常感谢您提供有关我在这里做错了什么的信息。我想将src中的所有文件合并到dest文件中。
编辑
如果删除expand属性,则输出如下所示:
Running "concat:mobile" (concat) task
Verifying property concat.mobile exists in config...OK
Files: main-module.js, clients/clients-module.js, reports/reports-module.js, schedules/schedules-module.js, services/services-module.js, clients/addclient-ctrl.js, clients/clientprofile-ctrl.js, clients/clients-ctrl.js, clients/clients-service.js, common/directives.js, common/filters.js, common/header-ctrl.js, common/navbarcollapse-directive.js, dist/ngmobile.concat.js, main-ctrl.js, main-service.js, reports/reports-ctrl.js, schedules/schedules-ctrl.js, schedules/schedules-service.js, services/addservice-ctrl.js, services/serviceprofile-ctrl.js, services/services-ctrl.js, services/services-service.js -> static/javascript/mobile/app/dist/ngmobile.concat.js
Options: separator="\n", banner="", footer="", stripBanners=false, process=false
>> Source file "main-module.js" not found.
>> Source file "clients/clients-module.js" not found.
>> Source file "reports/reports-module.js" not found.
>> Source file "schedules/schedules-module.js" not found.
>> Source file "services/services-module.js" not found.
>> Source file "clients/addclient-ctrl.js" not found.
>> Source file "clients/clientprofile-ctrl.js" not found.
>> Source file "clients/clients-ctrl.js" not found.
>> Source file "clients/clients-service.js" not found.
>> Source file "common/directives.js" not found.
>> Source file "common/filters.js" not found.
>> Source file "common/header-ctrl.js" not found.
>> Source file "common/navbarcollapse-directive.js" not found.
>> Source file "dist/ngmobile.concat.js" not found.
>> Source file "main-ctrl.js" not found.
>> Source file "main-service.js" not found.
>> Source file "reports/reports-ctrl.js" not found.
>> Source file "schedules/schedules-ctrl.js" not found.
>> Source file "schedules/schedules-service.js" not found.
>> Source file "services/addservice-ctrl.js" not found.
>> Source file "services/serviceprofile-ctrl.js" not found.
>> Source file "services/services-ctrl.js" not found.
>> Source file "services/services-service.js" not found.
Writing static/javascript/mobile/app/dist/ngmobile.concat.js...OK
File "static/javascript/mobile/app/dist/ngmobile.concat.js" created.
请注意,“文件:...”上方的第三行列出了我要查找的所有文件,但随后说未找到源文件。
编辑2
Matt下面的解决方案解决了这个问题,下面是我的更新代码:
concat: {
mobile: {
dest: "static/javascript/mobile/app/dist/ngmobile-concat.js",
src: (function () {
var cwd = "static/javascript/mobile/app/";
var files = ["*-module.js", "**/*-module.js", "**/*.js"];
files = files.map(function (file) {
return cwd + file;
});
files.push("! static/javascript/mobile/app/dist");
return files;
}())
}
}
我自己使用了棱角分明的应用程序遇到了这个问题,我考虑了以下三种选择:
1)按照项目github线程上的建议
你不是一个人。在grunt-contrib-concat github项目上有一个关于此问题的长篇文章。它与的工作方式cwd
有关dest
。
此处发布的建议解决方法将cwd
属性移到一个函数中,该函数然后产生预期的结果,而不是ENOTDIR
错误。
src: (function() {
var cwd = 'src/js/';
var arr = [];
// determine file order here and concat to arr
return arr.map(function(file) { return cwd + file; });
}())
我认为缺点是在gruntfile中增加了一个功能,这通常非常简单。如果您不是唯一维护该应用程序的人,这可能会造成混淆。
2)放弃cwd
并使用带有<%=%>变量语法的静态方法
如果您的src
和dest
目录在整个gruntfile中是一致的,则可以这样定义src
和dest
目录:
myapp: {
// configurable paths
app: "static/javascript/mobile/app/",
dist: "static/javascript/mobile/app/dist/"
}
...然后您可以删除该cwd
属性,并将其配置为看起来更“静态”:
concat: {
mobile: {
expand: true,
src: ["<%= myapp.app %>/main-module.js", "<%= myapp.app %>/**/*-module.js", "<%= myapp.app %>/**/*.js", "!<%= myapp.dist %>/*"],
dest: "<%= myapp.dist %>/ngmobile.concat.js"
}
}
对于我的大多数项目来说,这就足够了。当我稍后回来并需要进行更改时,它很简单,很干而且很容易理解。
授予:首先,由于所有<%=%>混乱,配置属性变得有点难以阅读,但是在我的编辑器中有一些格式可以改进。另外,另一位程序员阅读此书并了解您的意图非常容易。
3)使用类似usemin的插件为您完成此操作
如果您还在进行缩小,则可以考虑使用类似grunt-usemin的插件。
此选项的缺点是显而易见的:usemin增加了复杂性,因为你现在有另一个插件来管理和配置以及你需要格式化你的HTML使用usemin功能。起初,Usemin配置可能会造成混乱,如果您与团队合作,则修改usemin配置或usemin需求的HTML注释的程序员或设计人员很容易破坏事情。
好处是usemin将直接从HTML为您生成grunt的副本,concat和缩小配置。因此,如果您添加/删除脚本或更改HTML中的加载顺序,则无需在grunt配置中执行任何操作,因为usemin会选择它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句