如何在 grunt 中解析 '<%= =>'?

How do I parse '<%= =>' in grunt?

很多 grunt 插件在告诉它包含文件时允许使用这种语法:

['<%= src_dir %>/common/**/*.js', '<%= src_dir %>/app/**/*.js']

['<%= test_files.js %>']

有什么方法可以调用一些库来解析这些并给我一个实际输出的数组?还是直接内置到 grunt 中?我不确定要 google 什么条件才能显示出来。

谢谢

您正在寻找 grunt.config.get, grunt.config.process or grunt.template.process,具体取决于您从何处获取值以及您希望如何处理它们。

grunt.config.get

Get a value from the project's Grunt configuration. If prop is specified, that property's value is returned, or null if that property is not defined. If prop isn't specified, a copy of the entire config object is returned. Templates strings will be recursively processed using the grunt.config.process method.

grunt.config.get([prop])

grunt.config.process

Process a value, recursively expanding <% %> templates (via the grunt.template.process method) in the context of the Grunt config, as they are encountered. this method is called automatically by grunt.config.get but not by grunt.config.getRaw.

grunt.config.process(value)

[...]


grunt.template.process

Process a Lo-Dash template string. The template argument will be processed recursively until there are no more templates to process.

The default data object is the entire config object, but if options.data is set, that object will be used instead. The default template delimiters are <% %> but if options.delimiters is set to a custom delimiter name (set with grunt.template.addDelimiters), those template delimiters will be used instead.

grunt.template.process(template [, options])

Inside templates, the grunt object is exposed so that you can do things like <%= grunt.template.today('yyyy') %>. Note that if the data object already has a grunt property, the grunt API will not be accessible in templates.

In this example, the baz property is processed recursively until there are no more <% %> templates to process.

var obj = {
  foo: 'c',
  bar: 'b<%= foo %>d',
  baz: 'a<%= bar %>e'
};
grunt.template.process('<%= baz %>', {data: obj}) // 'abcde'