grunt.js配置中常见配置选项的继承

nwi*_*ler 10 javascript node.js gruntjs

人们如何处理Grunt中针对多个项目的常见配置选项.这些项目将共享一些常见的配置选项,例如min,但是每个项目也有私有自定义配置设置,例如,三个项目中只有一个需要less或有不同的选项.

有没有办法在项目之间共享这种通用配置,使用继承或导入现有文件,还是每个项目都必须定义所有设置?

我所指的项目将驻留在目录层次结构中

root
    module1
         grunt.js
    module2
         grunt.js
    module3
         grunt.js
Run Code Online (Sandbox Code Playgroud)

有没有办法在该root级别提供通用配置设置?

Dmi*_*ich 10

您可以根据需要轻松地将配置存储在尽可能多的外部JSON文件中.grunt.file.readJSON将在这里帮助你.例如:

module.exports = function(grunt) {

  var concatConf = grunt.file.readJSON('../concat-common.json'),
      minConf = grunt.file.readJSON('../min-common.json');

  // do whatever you want with concatConf and minConf here
  // ...

  // Project configuration.
  grunt.initConfig({
    pkg: '<json:grunt-sample.jquery.json>',
    meta: {
      banner: '/*! <%= pkg.title || pkg.name %> - v<%= pkg.version %> - ' +
        '<%= grunt.template.today("yyyy-mm-dd") %>\n' +
        '<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
        '* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
        ' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
    },

    concat: concatConf,
    min: minConf

    // ...
  });

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

};
Run Code Online (Sandbox Code Playgroud)

不要忘记gruntfile是在Node环境中执行的常规JavaScript文件,配置选项是常规的JavaScript对象:)