【问题标题】:Grunt lodash template won't map and return an array, can't figure out proper syntaxGrunt lodash 模板不会映射并返回数组,无法找出正确的语法
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,但我已经尝试了您的解决方案,并且 (app: app) 未定义。此外,如果提供了下面的此字符串,则会按预期记录一个数组,表明应用程序在执行 .map 时在模板范围内。 "app/${file})) %>"
  • 我确定 'app' 变量被正确传递。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
相关资源
最近更新 更多