我有两种不同的路径,我想编译移动和桌面代码。我想通过在命令行中传递一个grunt参数来替代。
/**
* @module Build
* @class Build.Config
* @static
*/
module.exports = function(grunt) {
var config = {};
var NewPath;
var env = grunt.option('target') || "Mobile";
if (env == "Desktop") { // MAKE THIS DYNAMIC WITH COMMAND LINE ARGUMENT
newPath = "source/desktop/";
}
else {
newPath = "source/mobile/";
}
config.root = newPath;
config.stylesheets = config.root + '/stylesheets';
config.javascripts = config.root + '/javascripts';
config.images = config.root + '/images';
config.jsbin = config.javascripts + '/generated';
config.cssbin = config.stylesheets + '/generated';
config.docsbin = 'docs';
// Project configuration.
grunt.initConfig({
'beautifier': {
'options': {
'indentSize': 1,
'indentChar': '\t',
'spaceAfterAnonFunction': true
}
},
'beautify': {
'files': [ config.javascripts + '/app/**/*.js' ]
},
'requirejs': require('./build/config/requirejs.js')(config),
'watch': require('./build/config/watch.js')(config),
'stylus':require('./build/config/stylus.js')(config)
});
// Default task.
grunt.registerTask('default', ['stylus:compile','requirejs']);
grunt.registerTask('dev', ['stylus:dev']);
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-contrib-stylus');
};发布于 2013-10-28 18:14:12
结果,我做得很对,我只需要正确地传入env的变量:
$ grunt --target="Desktop“
发布于 2014-02-09 06:16:38
另一种选择是通过冒号传递它。例如,将它传递给jshint
grunt jshint:desktop然后配置grunt,使用process.argv获取命令行参数,您可以使用它来配置路径或其他可能需要的内容:
module.exports = function(grunt) {
"use strict";
//dynamic config after the ':'. 'desktop' here
var env = process.argv[2].split(':')[1];
var config = {
pkg: grunt.file.readJSON('package.json'),
jshint: {
options: {
jshintrc: '.jshintrc',
"force": true
}
},
};
//...
config.jshint[env] = { // ex: $ grunt jshint:desktop
src: ['public/'+env+'/js/main.js']
};
//...
// Project configuration.
grunt.initConfig(config);
//...
};关于使用process的一个警告是,当您使用像有用的咕噜-同时那样响应您的进程的繁重任务时,它将无法工作。在这种情况下,最好使用grunt.option,如@im_benton所示。把grunt mytask --myvar=myval传递给你,然后在你的Gruntfile.js里把它接上grunt.option('myvar')‘
https://stackoverflow.com/questions/19641627
复制相似问题