gpt4 book ai didi

javascript - 使用 globbing 模式时 grunt-contrib-concat 的奇怪行为

转载 作者:搜寻专家 更新时间:2023-11-01 04:17:19 24 4
gpt4 key购买 nike

我正在使用 grunt,我想以特定顺序连接特定目录中的所有 js 文件(这是一个 Angular js 应用程序,所以我想先做我的模块定义,然后是其他所有内容)。我的 grunt concat 目标看起来像:

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;
}())
}
}

最佳答案

我自己在使用 Angular 应用程序时遇到了这个问题,我考虑了三个选项:

1) 按照项目github线程上的建议

你并不孤单。有一个 long thread在关于这个问题的 grunt-contrib-concat github 项目。它与 cwddest 的工作方式有关。

A suggested workaround在那里发布将 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 并使用具有 <%= %> 变量语法的静态方法

如果您的 srcdest 目录在整个 gruntfile 中是一致的,您可以定义 srcdest 目录做这样的事情:

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"
}
}

对于我的大多数项目,这就足够了。当我稍后回来需要进行更改时,它很简单、DRY 且易于理解。

当然:由于所有 <%=%> 困惑,配置属性起初变得有点难以阅读,但在我的编辑器中可以改进一些格式。此外,其他程序员也很容易阅读本文并了解您的意图。

3) 使用像 usemin 这样的插件为你做这件事

如果你也在做缩小,你可以考虑一个像grunt-usemin这样的插件。 .

此选项的缺点很明显:usemin 增加了复杂性,因为您现在有另一个插件来管理和配置并且您需要格式化 HTML 才能使用 usemin 功能。 Usemin 配置一开始可能会令人困惑,如果您与团队合作,程序员或设计人员很容易破坏 usemin 配置或 usemin 需要的 HTML 注释。

好处是 usemin 会直接从 HTML 为您生成 grunt 的复制、连接和缩小配置。因此,如果您添加/删除脚本或更改 HTML 中的加载顺序,您无需在 grunt 配置中执行任何操作,因为 usemin 会选择它。

关于javascript - 使用 globbing 模式时 grunt-contrib-concat 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21496226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com