gruntjs - 如何使用 grunt 配置的模板字符串?

标签 gruntjs

我在 gruntfile 中的 module.exports 顶部设置了模板:

// Self calling so we get a value.
// Expects only a single schema in the schema folder.
grunt.config.set('schemaPath', function() {
    console.log(grunt.file.expand('schema/*.json')[0]);
    return grunt.file.expand('schema/*.json')[0];
}());

console.log(grunt.config.get('schemaPath'));

稍后,在配置命令时,我想使用模板字符串,如下所示:

stripJsonComments: {
  currentSchema: {
    files: {
      'build/<%= schemaPath %>': '<%= schemaPath %>'
    }
  }
}

但是我收到错误:处理模板时发生错误(无法读取未定义的属性“src”)。经过调查,模板字符串似乎没有扩展,而是被删除。

最佳答案

您不能使用模板作为属性名称,它们不会被处理。所以'build/<%= schemaPath %>': '<%= schemaPath %>'行不通。

我建议研究 task configuration 的另一个变体,您可以在其中定义 srcdest明确属性,然后使用值模板。
如果您正在寻找多个 src-dest 文件映射,您可以使用 files array format 。它看起来像这样:

module.exports = function(grunt) {

  grunt.initConfig({
    concat: {
      main: {
        files: [
          {src: ['<%= file1 %>', '<%= file2 %>'], dest: '<%= destPath %>'},
        ]
      },
    }    
  });


  grunt.config.set('file1', 'foo.js');
  grunt.config.set('file2', 'bar.js');
  grunt.config.set('destPath', 'dest/baz.js');

  grunt.loadNpmTasks('grunt-contrib-concat');

  grunt.registerTask('default', ['concat']);

};

关于gruntjs - 如何使用 grunt 配置的模板字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471549/

相关文章:

javascript - Dart 有类似 GruntJs 的东西吗?

javascript - 使用测试框架时如何启动 Node 服务器?

javascript - grunt Jasmine Node 测试运行两次

django - 通过 Grunt 获取 Django runserver dev 日志记录输出

javascript - 变量可以同时是对象和未定义吗?

node.js - 从 Grunt 任务设置 NODE_ENV

javascript - 是否有一个 grunt 插件可以将文本 append 到文件内的字符串中?

javascript - Debowerify 无法与 Grunt 一起使用

javascript - grunt : How to run a function and then a task and then another function, 依次,当函数不能拆分到不同的任务时?

javascript - 在没有 NodeJS 的情况下丑化和缩小 AngularJS 源代码