【发布时间】:2016-02-24 10:06:19
【问题描述】:
目标:以下是繁重的任务。它的工作是连接和混淆通过字符串数组传入的文件。顺序很重要,顺序已经确定。典型且非常基本的 grunt uglify 设置。
问题:该数组虽然按正确的顺序排列,但却充满了相关的 url,需要在前面加上字符串 'app/'。 app.bummer_js 您将在下面的第一个示例中看到,是一个手动添加前缀的数组,以证明任务有效并且 lodash 模板按预期工作。以下示例旨在说明模板内的任何表达式都没有正确返回预期的数组。
# assume the following arrays are available from another file
app =
js: [
'path/to/file/lib/dependency.js'
'path/to/file/app.js'
]
bummer_js: [
'app/path/to/file/lib/dependency.js'
'app/path/to/file/app.js'
]
module.exports =
prod:
options:
banner:"<%= banner %>"
files:
# straight array reference in template, **manually** prefixed with 'app/' base dir in the app config,
# src order maintained, works great except there's a manual process to it.
"<%= dirs.build %>/js/app.js":"<%= app.bummer_js %>"
# simply mapping the array fails in grunt, not a recognized pattern or not written because src files were empty.
# when console logged, the array appears correct and in proper src order
"<%= dirs.build %>/js/app.js":"<%= _.map(app.js,(file)=>`app/${file}`) %>"
# none of these syntax styles below work either
# remove '=' to have it evaluated by lodash
"<%= dirs.build %>/js/app.js":"<% _.map(app.js,(file)=>`app/${file}`) %>"
"<%= dirs.build %>/js/app.js":"<%= app.js.map((file)=>`app/${file}`) %>"
# wrapping template in brackets turns the contents into an array, but src order is lost (i believe this is from going from an object to an array)
"<%= dirs.build %>/js/app.js":"{<%= _.map(app.js,(file)=>`app/${file}`) %>}"
对于 Uglify 的连接部分,保持 src 顺序非常重要。在尝试编写正确的 lodash 语法 100 次之后,我找不到如何编写模板以根据需要返回映射数组。任务完成的唯一方法是当表达式被包裹在{} 中时,为我提供了一个无用的包。
tldr; 映射数组的正确 lodash 语法是什么,以便将识别的模式返回给 Node/Grunt 的系统。我只需要一个以字符串为前缀的文件数组!这在理论上太简单了,我无法弄清楚。
【问题讨论】:
-
为什么不在module.exports之前做map呢?您将动态创建数组,但之后不会出现语法问题。
-
好主意。我试过了,grunt 没有替换模板字符串,在导入的任务范围之外没有模板字符串。
标签: coffeescript gruntjs build-automation lodash