我有两种模式,即在 prod 模式下用于 js 和 css 缩小的生产和开发模式,在 dev 模式下只有 concat。
我为此有多个任务,并且出于某种原因我已将默认模式设置为开发,但 grunt 无法说未找到任务“开发”。当我在默认任务中明确定义开发时。
这是我的 gruntfile.js
module.exports = function (grunt) {
// Load grunt tasks automatically
require('load-grunt-tasks')(grunt);
// object to represent the type of environment we are running in.
var EnvType = {
prod: 'production',
dev: 'development'
};
grunt.initConfig({
pkg: require('./package.json'),
config: {
// Configurable paths
app: 'src/main/webapp',
lib: '<%= config.app %>/lib',
dist: 'target',
distApp: '<%= config.dist %>/XXX'
},
clean:{
dist: '<%= config.distApp%>',
// Remove target folder
target: '<%= config.dist%>'
},
// GENERATE CSS ----------------------------------------------------
less: {
development: {
options: {
compress: false,
yuicompress: false,
optimization: 2
},
files: {
'<%= config.app%>/css/main.css': 'src/less/main.less'
}
},
production: {
files: {
'<%= config.app%>/css/main.css': 'src/less/main.less'
}
}
},
//----------------------Concat CSS files----------------------
concat: {
css: {
src: ['<%= config.app %>/css/ux.css', '<%= config.app %>/css/main.css' ],
dest: '<%= config.distApp %>/css/style.css'
},
js: {
src : ['<%= config.app %>/js/Utilities.js',
'<%= config.app %>/common/**/*.js',
'<%= config.app %>/js/**/*.js',
'<%= config.app %>/js/app.js'],
dest: '<%= config.distApp %>/js/core.js'
}
},
//----------------- Minify concatenated CSS stylesheet ------------------
cssmin: {
dist: {
files: [{
expand: true,
cwd: '<%= config.distApp %>/css',
src: 'style.css',
dest: '<%= config.distApp %>/css',
ext: '.min.css'
}]
}
},
uglify: {
js: {
files: {
'<%= config.distApp %>/js/core.js': ['<%= config.distApp %>/js/core.js']
}
}
}
});
/**
* Utility function to register the stylesheets task to grunt.
* @param {[type]} mode [the mode, either dev, or production]
*/
var registerStyleSheetsTask = function(mode) {
var styleTask = (mode === EnvType.dev) ? 'concat:css' : 'cssmin';
grunt.registerTask('stylesheets:' + mode,
'Compiles the stylesheets for development mode',
['clean', 'less:' + mode, styleTask]
);
};
/**
* Utility function to register the scripts task to grunt.
* @param {[type]} mode [the mode, either dev, or production]
*/
var registerScriptsTask = function(mode) {
// if we are running in dev mode, only concat the scripts
// otherwise minify them also
var scriptTask = (mode === EnvType.dev) ? 'concat:js' : 'uglify:js';
grunt.registerTask('scripts:' + mode,
'Compiles the javascript files in ' + mode + ' mode',
['clean', scriptTask]
);
};
//register tasks for both prod and dev modes
registerStyleSheetsTask(EnvType.dev);
registerStyleSheetsTask(EnvType.prod);
registerScriptsTask(EnvType.dev);
registerScriptsTask(EnvType.prod);
// register development mode as the main task
grunt.registerTask('default', 'Default task: development', 'development');
};