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

标签 javascript node.js file parsing gruntjs

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

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

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

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

谢谢

最佳答案

您正在寻找 grunt.config.get , grunt.config.process 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'

关于javascript - 如何在 grunt 中解析 '<%= =>' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29106631/

相关文章:

javascript - JavaScript 中的 ActiveXObject 问题

node.js - Mongoose schematype.castForQuery 错误。用户身份验证失败

c++ - 将二进制文件内容读取到 std::string 的最佳方式是什么?

python - 将 .htm 或 .html 扩展名与 python RE 匹配

javascript - 将元素(作为 jQuery 对象)作为参数传递给函数

javascript - 如何在 html 页面中使用从 native 应用程序接收的数据?

javascript - 如何将 bootstrap 3 面板引入 bootstrap 2 网站?

mysql - 在 NodeJS 中生成交互式进程并退出

javascript - 在 NestJS 中测试解析器

如果路径包含非拉丁字符,则 Mac OS FileNotFound 上的 Java