網站搜尋

javascript - 拆分 Gruntfile


我的 Gruntfile 現在變得相當大,我想把它拆分成多個文件。我已經上網搜尋和做了很多實驗,但還是不知道怎麼讓它運作。

我想要像這樣的東西:

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

你可以這樣做:

你需要匯出你的 concat 配置,並在你的 Gruntfile 中引用它(基本的 node.js 操作)!

我建議將所有每個任務特定的配置放入一個文件中,該文件以配置命名(在這個例子中,我將其命名為 concat.js)。

此外,我把 concat.js 移動到名為 grunt 的資料夾中

Gruntfile.js

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

grunt/concat.js

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

你應該這樣做:

那裡已經有人創建了一個名為 load-grunt-config 的模組。這正是你想要的功能。

繼續將所有內容(如上所述)放入單獨的文件中,存放到你選擇的位置(默認資料夾是 grunt)。

那麼你的標準 gruntfile 可能應該看起來像這樣:

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

  // define some alias tasks here
};