Domanda

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.

È stato utile?

Soluzione

By default, grunt-contrib-watch will spawn task runs as child processes. Those child processes don't share the context of the parent process and thus changes to the config are not present.

The easiest way is to disable spawning tasks with:

watch:
  options: spawn: false

and the watch task will run tasks within the same process context.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top