拆分Gruntfile

时间:2014-12-08 11:01:13

标签: javascript gruntjs

我的Gruntfile现在变得非常大,我想把它分成多个文件。我已经用Google搜索并进行了很多实验,但我无法让它发挥作用。

我想要这样的事情:

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    concat: getConcatConfiguration()
  });
}

functions.js

function getConcatConfiguration() {
  // Do some stuff to generate and return configuration
}

如何将functions.js加载到我的Gruntfile.js?

1 个答案:

答案 0 :(得分:6)

如何做到这一点:

你需要导出你的concat配置,并在你的Gruntfile中需要它(基本的node.js东西)!

我建议将所有特定于任务的配置放入一个以配置命名的文件中(在本例中我将其命名为concat.js)。

此外,我将concat.js移到了名为grunt

的文件夹中

Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    concat: require('grunt/concat')(grunt);
  });
};

咕噜/ concat.js

module.exports = function getConcatConfiguration(grunt) {
  // Do some stuff to generate and return configuration
};

你应该怎么做:

那里已经有人创建了一个名为load-grunt-config的模块。这正是你想要的。

继续将所有内容(如上所述)放入单独的文件中,放入您选择的位置(默认文件夹ist grunt)。

那么你的标准gruntfile应该是这样的:

module.exports = function(grunt) {
  require('load-grunt-config')(grunt);

  // define some alias tasks here
};