grunt-parallel icon indicating copy to clipboard operation
grunt-parallel copied to clipboard

grunt.config properties are not shared across spawned tasks

Open davemo opened this issue 11 years ago • 2 comments

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:

screen shot 2013-10-13 at 6 29 08 pm

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:

screen shot 2013-10-13 at 6 31 59 pm

screen shot 2013-10-13 at 6 35 24 pm

davemo avatar Oct 13 '13 22:10 davemo

+1 I have the same problem trying to pass a port number from grunt-freeport to parallel tasks.

stas-vilchik avatar May 28 '15 11:05 stas-vilchik

I had the same problem and created a pull request to solve this: https://github.com/iammerrick/grunt-parallel/pull/45 for this.

JanST123 avatar Jul 05 '18 06:07 JanST123