How to pass in package.json array to grunt.js

こ雲淡風輕ζ 提交于 2019-12-19 05:19:18

问题


is there a way to pass in an array to grunt.js from the package.json file? I've tried a few different ways and none of them seem to work. I currently have:

/*global module:false*/
module.exports = function(grunt) {

     // Project configuration.
     grunt.initConfig({
    pkg: '<json:package.json>',

    lint: {
      files: '<%= pkg.lint.join(", ") %>'
    }

    // Default task 'lint qunit concat min'
    grunt.registerTask('default', 'lint');
};

package.json

{
  "lint": [   
              "grunt.js",
              "test.js"
          ]
}

The only solution that I have been able to find is to pass in a specific index of the array; e.g. <%= pkg.lint[0] %>. Thanks in advance for your help!


回答1:


Since gruntjs in run in node you can access the package.json like:

var package = require('./package.json'),
    property = package.property[0];



回答2:


I think that the <%= … %> syntax (variable interpolation in Underscore's template system) can only output strings, not arrays/objects.

Try this instead:

lint: {
    files: '<config:pkg.lint>'
}

I found this syntax in Grunt's jQuery init task.




回答3:


grunt.initConfig({
  lint: grunt.file.readJSON('package.json').lint,
});


来源:https://stackoverflow.com/questions/12408535/how-to-pass-in-package-json-array-to-grunt-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!