Grunt/Yeoman Multiple Gruntfile.js Files

2019-06-11 12:52发布

问题:

My goal is the be able to build my project to two separate build folders, each with its' own Grunt tasks.

I noticed the grunt-cli has the --gruntfile option which allows you to specify another Gruntfile to use. So far, I have a Gruntfile.js working perfectly (near stock from Yeoman). Also, I have another Gruntfile2.js sitting alongside.

Gruntfile.js

var yeomanConfig = {
    app: 'app',
    dist: '../www_browser'
};

Gruntfile2.js

var yeomanConfig = {
    app: 'app',
    dist: '../www'
};

grunt build is meant to run Gruntfile.js, and does so perfectly.

grunt build --gruntfile Gruntfile2.js is meant to run Gruntfile2.js, and does so with some hiccups. (supposed to build to ../www folder NOT ../www_browser folder)

The --gruntfile directive builds to the proper folder for almost every task except grunt-usemin and gunt-contrib-htmlmin. I know this because of this output to the console here:

Running "usemin:css" (usemin) task
Processing as CSS - ../www_browser/styles/22f60055.main.css

Running "concurrent:dist" (concurrent) task

Running "htmlmin:dist" (htmlmin) task
File ../www_browser/404.html created.
File ../www_browser/index.html created.

You'll notice the ../www_browser, here in console output. Every other task runs in the expected ../www folder.

Is this a usemin cache thing? Grunt cache thing? Or do some tasks simply run from the default Gruntfile.js regardless of the --gruntfile Gruntfile2.js directive?

I have given up on trying to do multiple targets from a single Gruntfile for now. There are too many dependencies in Yeoman's Gruntfile.js that don't yet support multiple build targets, and I spent 12 hours to no avail with that approach.

Version Info

$: grunt --version
grunt-cli v0.1.9
grunt v0.4.1

$: npm --version
1.2.25

$: yo --version
1.0.3

package.json

{
  "name": "myapp",
  "version": "0.0.0",
  "dependencies": {},
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-copy": "~0.4.1",
    "grunt-contrib-concat": "~0.1.3",
    "grunt-contrib-coffee": "~0.6.5",
    "grunt-contrib-uglify": "~0.2.0",
    "grunt-contrib-compass": "~0.2.0",
    "grunt-contrib-jshint": "~0.4.1",
    "grunt-contrib-cssmin": "~0.6.0",
    "grunt-contrib-connect": "~0.2.0",
    "grunt-contrib-clean": "~0.4.0",
    "grunt-contrib-htmlmin": "~0.1.3",
    "grunt-bower-requirejs": "~0.4.1",
    "grunt-contrib-requirejs": "~0.4.0",
    "grunt-contrib-imagemin": "~0.1.3",
    "grunt-contrib-watch": "~0.4.0",
    "grunt-rev": "~0.1.0",
    "grunt-usemin": "~0.1.10",
    "grunt-mocha": "~0.3.0",
    "grunt-open": "~0.2.0",
    "grunt-svgmin": "~0.1.0",
    "grunt-concurrent": "~0.1.0",
    "matchdep": "~0.1.1",
    "connect-livereload": "~0.2.0"
  },
  "engines": {
    "node": ">=0.8.0"
  }
}

回答1:

It looks like you're using an older version of grunt-concurrent, which doesn't seem to be passing the grunt flags through to the child-processes.

See here: https://github.com/sindresorhus/grunt-concurrent/blob/v0.1.0/tasks/concurrent.js#L11

On the current version of the plugin, the flags are passed, see here: https://github.com/sindresorhus/grunt-concurrent/blob/v0.3.1/tasks/concurrent.js#L22

So, I suggest updating your version of grunt-concurrent:

npm install grunt-concurrent@latest --save-dev