简体   繁体   English

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

[英]Can I have multiple gruntjs files in my project for code organization?

I'm using gruntjs for my project and was wondering if it's possible to have multiple grunt.js files in my project? 我正在为我的项目使用gruntjs,并想知道我的项目中是否可以有多个grunt.js文件? The reason I'm asking is that my project is organized like so: 我问的原因是我的项目是这样组织的:

grunt.js
|
|- project1
|   |
|   |-grunt.js
|
|- project2
|
|- project3 (etc..)

I want the top level grunt.js to build all the projects. 我想要顶级grunt.js来构建所有项目。 However as the list of projects grow I don't want my top level grunt.js file to become huge. 但是随着项目列表的增长,我不希望我的顶级grunt.js文件变得庞大。 So I would like to organize it so that the top level grunt can call the project level grunt files to build them. 所以我想组织它,以便顶级grunt可以调用项目级别的grunt文件来构建它们。 Or if someone wants to just build project1, they can go to the project1 folder and run it's own grunt.js. 或者,如果有人想要构建project1,他们可以转到project1文件夹并运行它自己的grunt.js。 Can this be done? 可以这样做吗? If so, how do I call the other grunt files? 如果是这样,我该如何调用其他grunt文件? If not, then what's an alternative solution other than having one huge grunt.js file? 如果没有,那么除了拥有一个巨大的grunt.js文件之外,还有什么替代解决方案? Thanks. 谢谢。

I recently solved this issue with a very simple solution. 我最近用一个非常简单的解决方案解决了这个问题
I implemented grunt.config.merge(config) to replace grunt.initConfig(config) . 我实现了grunt.config.merge(config)来替换grunt.initConfig(config) You can call it multiple times, and it will simply merge the config into a single config. 您可以多次调用它,它只是将配置合并到一个配置中。

Update : As of Grunt v0.4.5, the grunt.config.merge function is built-in, so you no longer need to include it as a separate library. 更新 :从Grunt v0.4.5开始, grunt.config.merge函数是内置的,因此您不再需要将其作为单独的库包含在内。

This allows me to organize my config by feature. 这允许我按功能组织我的配置。
I create a config file for each feature, such as desktop-javascripts.js , desktop-css.js , mobile-javascripts.js , mobile-css.js . 我为每个功能创建了一个配置文件,例如desktop-javascripts.jsdesktop-css.jsmobile-javascripts.jsmobile-css.js
Also, they share common configuration in default-options.js , so I can specify compression settings and what not. 此外,它们在default-options.js共享通用配置,因此我可以指定压缩设置,而不是。

Example

Gruntfile.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');

};

desktop-javascripts.js: 桌面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' ] } }
  });

};

There's a grunt task called grunt-hub which seems to do what you want to do. 有一个叫做grunt-hub的繁琐任务似乎可以做你想做的事情。

grunt-hub: 咕噜毂:

A Grunt task to watch and run tasks on multiple Grunt projects. 在多个Grunt项目上观察和运行任务的Grunt任务。

hub task 枢纽任务

The hub task is for running tasks on multiple projects. 中心任务用于在多个项目上运行任务。 It would like to know which Gruntfiles to use and which tasks to run on each Grunt project. 它想知道使用哪个Gruntfiles以及在每个Grunt项目上运行哪些任务。 For example if I would like to lint and test on every Grunt project one folder up: 例如,如果我想在每个Grunt项目中对一个文件夹进行lint和测试:

I haven't used it, but it might meet your requirements. 我没有使用它,但它可能符合您的要求。 Also you might want to look into more lower level such as grunt-exec or creating your own tasks to spawn child processes. 此外,您可能希望查看更低级别的内容,例如grunt-exec或创建自己的任务以生成子进程。

I know this question might be old, but I want to clarify there is a way to organise your codes in a proper way. 我知道这个问题可能已经过时了,但我想澄清一下如何以适当的方式组织代码。

To prevent having multi call like this: 为了防止像这样的多次通话:

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

Replace them with: 替换为:

require('load-grunt-tasks')(grunt);

And use: 并使用:

grunt-config-dir

To run the tasks from a folder. 从文件夹运行任务。

To know more about it, check this out. 要了解更多信息, 请查看此信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM