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

时间:2015-12-31 22:16:27

标签: 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
          };
        }
      })()
    }

  });
};

我知道Grunt可以使用&#39; grunt.file.readJSON&#39;从文件中读取数据,然后使用以下类型的变量提供该数据,&#39;&lt;%= pkg .value%&gt;&#39;。

我想要做的是使用基于JSON文件中的变量的if / else选项创建任务。我不清楚的是如何传递Grunt变量&lt;%= pkg.value%&gt;&#39;以一种它理解的方式进入JavaScript if语句。我尝试使用&#39;&lt;%=%&gt;&#39;以及删除该部分并传递&#39; pkg.value&#39;,然后将其传递给相同的Grunt格式,但似乎都不起作用。

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

3 个答案:

答案 0 :(得分:1)

不是直接在>>> import module >>> module.global_dataframe Empty DataFrame Columns: [] Index: [] >>> module.CustomClass <class module.CustomClass at 0x7fdf73f3d0b8> 属性中分配grunt配置,而是将其存储在变量(config)中。现在,您可以使用以下代码访问它。

gruntConfig

答案 1 :(得分: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
          };
        }
      })()
    }

  });
};

谢谢大家的帮助!

答案 2 :(得分:0)

Grunt有一个API来读取文件,您可以使用以下内容:

test.json

doubleValue

Gruntfile.js

{
  "fruit": "apple"
}
相关问题