grunt-parallel
grunt-parallel copied to clipboard
grunt.config properties are not shared across spawned tasks
Parallel works great for shaving a few seconds off intensive tasks like grunt-contrib-uglify
, but it has an issue right now where grunt.config properties that are set in a previous task are not shared across the spawned tasks.
I ran into this when attempting to inject the git-sha from a shell command into my banner during uglification, here's the grunt configuration:
module.exports = (grunt) ->
grunt.loadTasks "tasks"
grunt.loadNpmTasks "grunt-contrib-concat"
grunt.loadNpmTasks "grunt-contrib-uglify"
grunt.loadNpmTasks "grunt-parallel"
# Project configuration.
config =
# Metadata.
pkg: grunt.file.readJSON("package.json")
banner: "/*! sha: <%= sha %> */\n"
# Task configuration.
git_rev_parse:
prop: "sha"
concat:
options:
banner: "<%= banner %>"
stripBanners: true
bundle:
files:
"dist/app.js": ["vendor/**/*.js", "app/**/*.js"]
parallel:
minification:
tasks: [
{ grunt: true, args: ['uglify:dist'] }
{ grunt: true, args: ['uglify:alternate'] }
]
uglify:
options:
banner: "<%= banner %>"
dist:
src: "dist/app.js"
dest: "dist/app.min.js"
alternate:
files:
"dist/other-app.js" : [
"dist/app.js"
"other-things/**/*.js"
]
grunt.initConfig(config)
grunt.registerTask "default", ["git_rev_parse", "concat", "parallel"]
And here's my git_rev_parse task:
module.exports = (grunt) ->
grunt.registerTask "git_rev_parse", "Git revision parse", ->
property = grunt.config("git_rev_parse").prop
done = @async()
gitRevParseCommand =
cmd: "git"
args: "rev-parse --short HEAD".split(" ")
writesGitRevision = (err, result) ->
if err
grunt.log.error(err)
return done(false)
grunt.config property, result.stdout
grunt.log.write "#{gitRevParseCommand.cmd} #{gitRevParseCommand.args.join(" ")}: "
grunt.log.ok result.stdout
done()
grunt.util.spawn(gitRevParseCommand, writesGitRevision)
Which yields the following:
This workflow works fine if I simply setup my default task to use uglify directly, as values persisted to grunt.config
in previous tasks will be present in memory when subsequent tasks execute:
+1 I have the same problem trying to pass a port number from grunt-freeport to parallel tasks.
I had the same problem and created a pull request to solve this: https://github.com/iammerrick/grunt-parallel/pull/45 for this.