你能否将Grunt变量传递给Grunt任务中的JavaScript函数?

Mic*_*rra 5 javascript node.js gruntjs

这是我正在寻找的一个例子:

module.exports = function(grunt) {
  grunt.initConfig({

    config: grunt.file.readYAML('_config.yml'),
    // example variable: <%= config.scripts %>

    copy: {
      scripts: (function() {
        if (config.scripts === true) { // I want to target <%= config.scripts %>
          return {
            expand: true,
            cwd: '<%= input %>/_assets/js/',
            src: '**/*.js',
            dest: '<%= output %>/assets/js/'
          };
        } else {
          return {
            // do nothing
          };
        }
      })()
    }

  });
};
Run Code Online (Sandbox Code Playgroud)

我知道咕噜可以使用"grunt.file.readJSON"在一个文件中读取数据,然后具有用于以下类型的变量,"<%= pkg.value%>"的数据.

我想要做的是使用基于JSON文件中的变量的if/else选项创建任务.我不清楚的是如何以一种它理解的方式将Grunt变量'<%= pkg.value%>'传递到JavaScript if语句中.我尝试使用'<%=%>'以相同的Grunt格式传递它,以及剥离该部分并传递'pkg.value',但似乎都不起作用.

如果有人可以了解这是否可以做到以及如何做,我将非常感激.谢谢!

Mic*_*rra 1

当我有更多时间时,我可能会研究一些额外的想法,但根据这里提供的想法,这就是我现在最终采用的想法。

module.exports = function(grunt) {

  var config = grunt.file.readYAML('_config.yml');

  grunt.initConfig({

    copy: {
      scripts: (function() {
        if (config.scripts) {
          return {
            expand: true,
            cwd: 'input/_assets/js/',
            src: '**/*.js',
            dest: 'output/assets/js/'
          };
        } else {
          return {
            // do nothing
          };
        }
      })()
    }

  });
};
Run Code Online (Sandbox Code Playgroud)

谢谢大家的帮助!