I've been trying to find the best way to set global options for all of my grunt tasks from within a grunt task, but haven't had much luck getting things to work.
I'm wondering whether the tasks get compiled in some way, so that they're stuck with the initial option value, or if it is possible to change options during runtime (while the watch
task is running) and I'm just doing something wrong.
Base case
Here's a specific situation (note that Gruntfile is written in coffeescript). I start with the following task:
sass:
options:
sourcemap: true
compile:
files:
"css/style.css" : "sass/style.sass"
What I'm trying to do
I'd like to be able to dynamically set the sourcemap option from within another task, something like this:
sass:
options:
sourcemap: '<% grunt.options('local') %>'
compile:
files:
"css/style.css" : "sass/style.sass"
The watch task would pick up changes, and run a task to set the global option appropriately.
watch:
local:
files: ['local.json']
tasks: ['local']
dist:
files: ['dist.json']
tasks: ['dist']
grunt.option('local', true) # Base declaration
grunt.registerTask( 'local', 'Local is true', () -> grunt.option('local', true) )
grunt.registerTask( 'dist', 'Local is false', () -> grunt.option('no-local') )
I want it configured so that any tasks that fire after the "local" or "dist" tasks run (like when watch
runs the sass
task again) it will use the most recent value of my "local" option. I've tried a few approaches, and this seemed the most promising, but I haven't been able to get it to work as expected yet.