私は同じ問題を抱えていて、それを解決しました。
覚えておいてください: grunt はノードで実行されるすべての JavaScript であるため、JavaScript とノードがサポートすることは何でも実行できます。
私のソリューションは次のように機能します。
まず、コア アプリのすべての要素を別の JavaScript ファイル ("grunt-my-app-core.js" など) に配置します。そのエクスポートでは、2 つの関数init(grunt)
とgetConfig(grunt, options)
.
(function() {
"use strict"; //enable ECMAScript 5 Strict Mode
function init(grunt) {
}
function getConfig(grunt, options) {
return {};
}
///////////////////////////
// Exports
exports = module.exports = {
init: init,
getConfig: getConfig
};
})();
init(grunt)
タスクをロードして登録することです。次のようになります。
/**
* public API
*
* add all required settings to grunt
*
* register task "dist" and task "doc"
*
* @param {object} grunt the grunt instance
* @return {void}
*/
function init(grunt) {
// overwrite platform specific setting get always unix like line feed char
grunt.util.linefeed = '\n';
// Load plugins provide necessary task.
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.registerTask('dist', ['clean:build', 'copy:build', 'copy:dist', 'uglify', 'less']);
grunt.registerTask('doc', ['clean:doc', 'copy:doc']);
}
getConfig(grunt, options)
構成オブジェクトを構築して返します。
/**
* public API
*
* will return a config object that have to be given as argument to "grunt.initConfig"
*
* @param {object} grunt the grunt instance
* @param {object|undefined} options to change some pathes
* @return {object} the configuration object for initConfig
*/
function getConfig(grunt, options) {
options = options || {};
var buildDir = options.buildDir || "build/";
var distDir = options.distDir || "dist/";
var docDir = options.docDir || "doc/";
// ... doing some stuff to collect files or what ever ...
return {
clean: {
build: {
src: [buildDir]
},
doc: {
src: [docDir]
}
},
copy: {
// ...
}
// ... add here all the stuff you like to config ...
};
}
それよりも、Gruntfile.js
テーマ プロジェクトの は非常に短く、コア アプリのすべてのものを必要としない場合があります。次のようになります。
/**
* Module dependencies.
*/
var gruntMyApp = require("../my-app/grunt-my-app-core.js");
/*global module:false*/
module.exports = function(grunt) {
var config = gruntMyApp.getConfig(grunt);
// ... extend config, if you need it
grunt.initConfig(config);
// will register Task "dist" and "doc"
gruntMyApp.init(grunt);
// Default task.
grunt.registerTask('default', ['dist', 'doc']);
};
これで、コア アプリで何かを変更すると、すべてのテーマがこれを取得します。マニュアルを更新する必要があるのdevDependencies
は、package.json
ファイル内の だけです。