问题
Tried a few things and cannot seem to get this to work, but I would have thought it was something quite simple. I am trying to pass a variable|option into a Grunt initialize config when the task is run.
At the minute I have two separate configs:
sass: {
dev: {
options: {
style: 'expanded'
},
files: [{
expand: true,
cwd: source + 'scss/',
src: '*.scss',
dest: destination + 'css',
ext: '.css'
}]
},
production: {
options: {
style: 'compressed'
},
files: [{
expand: true,
cwd: source + 'scss/',
src: '*.scss',
dest: destination + 'css',
ext: '.css'
}]
}
}
As you can see, these are the same except the style option. What I would like to do is:
sass: {
build: {
options: {
style: style
},
files: [{
expand: true,
cwd: source + 'scss/',
src: '*.scss',
dest: destination + 'css',
ext: '.css'
}]
}
}
...
grunt.registerTask("sass", function () {
grunt.task.run('sass:build:style=expanded');
});
But I cannot work out how to pass the option from grunt.task.run to the initialize config in this way. Any Grunt experts know how to do this? Google didn't seem to have the answer either... Not sure if I'm approaching this wrong?
Thanks for any help!
回答1:
see Grunt API example - it is exactly what are you looking for.
$ grunt --type=dev
Now you can get this value by:
grunt.registerTask('default','description...', function() {
var type = grunt.option('type') || 'dev'; //if nothing passed from cli set to 'dev'
if (type == 'dev') {
grant.task.run('sass:dev');
} else if (type == 'production') {
grant.task.run('sass:production');
}
});
I think it must be something like I pointed above..
来源:https://stackoverflow.com/questions/20347253/pass-grunt-config-options-from-task-run