我的项目中是否可以有多个gruntjs文件用于代码组织?

Cha*_*ton 14 javascript node.js gruntjs

我正在为我的项目使用gruntjs,并想知道我的项目中是否可以有多个grunt.js文件?我问的原因是我的项目是这样组织的:

grunt.js
|
|- project1
|   |
|   |-grunt.js
|
|- project2
|
|- project3 (etc..)
Run Code Online (Sandbox Code Playgroud)

我想要顶级grunt.js来构建所有项目.但是随着项目列表的增长,我不希望我的顶级grunt.js文件变得庞大.所以我想组织它,以便顶级grunt可以调用项目级别的grunt文件来构建它们.或者,如果有人想要构建project1,他们可以转到project1文件夹并运行它自己的grunt.js.可以这样做吗?如果是这样,我该如何调用其他grunt文件?如果没有,那么除了拥有一个巨大的grunt.js文件之外,还有什么替代解决方案?谢谢.

Sco*_*pey 15

我最近用一个非常简单的解决方案解决了这个问题
我实施grunt.config.merge(config)了替换grunt.initConfig(config).您可以多次调用它,它只是将配置合并到一个配置中.

更新:从Grunt v0.4.5开始,该grunt.config.merge函数是内置函数,因此您不再需要将其作为单独的库包含在内.

这允许我按功能组织我的配置.
我创建一个配置文件为每个要素,如desktop-javascripts.js,desktop-css.js,mobile-javascripts.js,mobile-css.js.
此外,它们共享通用配置default-options.js,因此我可以指定压缩设置,而不是.

Gruntfile.js:

module.exports = function(grunt) {

  require('./default-options.js')(grunt);
  require('./desktop-javascripts.js')(grunt);
  require('./desktop-css.js')(grunt);
  require('./mobile-javascripts.js')(grunt);
  require('./mobile-css.js')(grunt);

  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');

};
Run Code Online (Sandbox Code Playgroud)

桌面javascripts.js:

module.exports = function(grunt) {

  // Configure all JavaScript tasks:
  grunt.registerTask('desktop-javascripts', [ 'concat:JS', 'jshint' ]);
  grunt.config.merge({
    concat: { 'JS': { files: allJS } },
    jshint: { 'JS': { files: allJS } },
    watch: { 'JS': { files: allJS, tasks: [ 'desktop-javascripts' ] } }
  });

};
Run Code Online (Sandbox Code Playgroud)


jai*_*ime 13

有一个叫做grunt-hub的繁琐任务似乎可以做你想做的事情.

咕噜毂:

在多个Grunt项目上观察和运行任务的Grunt任务.

枢纽任务

中心任务用于在多个项目上运行任务.它想知道使用哪个Gruntfiles以及在每个Grunt项目上运行哪些任务.例如,如果我想在每个Grunt项目中对一个文件夹进行lint和测试:

我没有使用它,但它可能符合您的要求.此外,您可能希望查看更低级别的内容,例如grunt-exec或创建自己的任务以生成子进程.