如何将package.json数组传递给grunt.js

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

本文关键字:grunt js 数组 package json      更新时间:2023-09-26

有没有办法将数组从package.json文件传递到grunt.js?我尝试了几种不同的方法,但似乎都不起作用。我目前有:

/*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');
};

软件包.json

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

我能找到的唯一解决方案是传入数组的特定索引;例如<%=pkg。lint[0]%>。提前感谢您的帮助!

由于gruntjs在运行节点中,您可以访问package.json,如:

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

我认为<%= … %>语法(Underscore模板系统中的变量插值)只能输出字符串,而不能输出数组/对象。

试试这个:

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

我在Grunt的jQueryinit任务中发现了这种语法。

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