Grunt lodash模板不会映射并返回数组,无法找出正确的语法

时间:2015-11-22 04:58:53

标签: coffeescript gruntjs build-automation lodash

目标:以下是笨拙的任务。这是工作,连接和混淆通过字符串数组传入的文件。订单很重要,订单已经设定。典型的和非常基本的grunt uglify设置。

问题:数组在正确的顺序中充满了相对网址,需要在字符串'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顺序非常重要。在100次尝试编写正确的lodash语法之后,我无法找到如何编写模板以返回我需要的映射数组。任务完成的唯一方法是将表达式包装在{}中,为我提供无用的包。

tldr; 映射数组的正确lodash语法是什么,以便将识别的模式返回给Node / Grunt的系统。我需要的是一个前缀为字符串的文件数组!它在理论上太简单了,我无法弄明白。

1 个答案:

答案 0 :(得分:0)

prod.files是模板。可能您使用该模板的脚本无法访问“app”变量。

编译模板

  # 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":_.template("<%= app.bummer_js %>")({app: app})

  # 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":_.template("<%= _.map(app.js,(file)=>`app/${file}`) %>")({app: app})

  # none of these syntax styles below work either
  # remove '=' to have it evaluated by lodash
  "<%= dirs.build %>/js/app.js":_.template("<% _.map(app.js,(file)=>`app/${file}`) %>")({app: app})
  "<%= dirs.build %>/js/app.js":_.template("<%= app.js.map((file)=>`app/${file}`) %>")({app: app})

  # 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":_.template("{<%= _.map(app.js,(file)=>`app/${file}`) %>}")({app: app})

导出应用

module.exports =
  app: app,
  prod:
    options: ...

在这种情况下,编译模板时必须手动将app添加到脚本中,例如

tmpls = require('your/module')
_.template(tmpls.prod.files['<%= dirs.build %>/js/app.js'])({app: tmpls.app})