本文介绍了Grunt / Yeoman多个Gruntfile.js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是能够将我的项目构建到两个独立的构建文件夹中,每个文件夹都有其自己的Grunt任务。我注意到了 grunt-cli 具有 - gruntfile 选项,它允许您指定另一个要使用的Gruntfile。到目前为止,我有一个 Gruntfile.js 完美的工作(来自Yeoman的库存)。另外,我还有另外一个 Gruntfile2.js


$ b Gruntfile.js

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

Gruntfile2.js

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

grunt build 用于运行Gruntfile。 js,并且完美无缺。

grunt build --gruntfile Gruntfile2.js 是为了运行 Gruntfile2.js ,并且这样做有一些打嗝。 (应该建立为 ../ www 文件夹NOT ../ www_browser 文件夹)



除了 grunt-usemin gunt-contrib-htmlmin 之外,--gruntfile指令几乎可以构建到适当的文件夹中。我知道这是因为输出到控制台:

$ p $ 运行usemin:css(usemin)任务
处理为CSS - ../www_browser/styles/22f60055.main.css

运行concurrent:dist(并发)任务

运行htmlmin:dist(htmlmin )任务
文件../www_browser/404.html创建。
文件../www_browser/index.html创建。

您将在控制台输出中看到 ../ www_browser 。每个其他任务都在预期的 ../ www 文件夹中运行。

这是一个usemin缓存的东西吗?咕噜缓存的东西?或者执行一些任务,只需从默认的Gruntfile.js运行,而不管 --gruntfile Gruntfile2.js 伪指令?



现在我已经放弃尝试从单个Gruntfile中完成多个目标。 Yeoman的Gruntfile.js中有太多的依赖关系,它们还不支持多个构建目标,我花了12个小时无法使用这种方法。

版本信息

  $: 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-干净:〜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
}
}


解决方案

看起来您正在使用旧版本的grunt-concurrent ,它似乎没有通过grunt标志传递给子进程。



请参阅:



在插件的当前版本中,传递了标志,请参阅:



所以,我建议更新你的grunt-concurrent版本:

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


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"
  }
}
解决方案

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

这篇关于Grunt / Yeoman多个Gruntfile.js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 01:26