从远程Gruntfile.js加载grunt任务

在项目的根目录中,我试图从远程的Gruntfile.js加载Grunt任务和文件的所有内容,通过networking(networking或内部networking)访问。

我已经尝试了几件事(见下文),首先在本地Apache服务器上托pipeGruntfile.js以进行testing。

使用--gruntfile选项

 > /project/path> grunt --gruntfile=http://localhost/Gruntfile.js build Reading "Gruntfile.js" Gruntfile...ERROR Fatal error: Unable to find "/project/path/http://localhost/Gruntfile.js" Gruntfile. 

使用--base选项

 > /project/path> grunt --base=http://localhost/Gruntfile.js build process.chdir(grunt.option('base') || path.dirname(gruntfile)); Error: ENOENT, no such file or directory 

在我的项目的根目录下创build一个Gruntfile.js ,它只包含以下代码:

 module.exports = function (grunt) { grunt.loadTasks( 'http://localhost/Gruntfile.js' ); }; 

结果是:

 > /project/path> grunt build >> Tasks directory "http://localhost/Gruntfile.js" not found. Warning: Task "build" not found. Use --force to continue. Aborted due to warnings. 

另一个尝试与Gruntfile.js

 module.exports = function (grunt) { require( 'http://localhost/Gruntfile.js' )( grunt ); }; 

给我这个:

 > /project/path> grunt build Loading "Gruntfile.js" tasks...ERROR >> Error: Cannot find module 'http://localhost/Gruntfile.js' Warning: Task "build" not found. Use --force to continue. Aborted due to warnings. 

编辑:似乎没有办法从一个不在磁盘上的grunt文件加载任务。 我会find另一个解决scheme。

需要一个单独的文件应该工作,请注意,你不应该通过localhost引用它,而是磁盘上的位置,例如./somedir/yourtask.js 。 这是一个例子:

yourtask.js

 module.exports = { // your task config goes here }; 

你的Gruntfile.js

 module.exports = function(grunt) { var externalFile = require('./somedir/yourtask.js'); grunt.initConfig({ // some random tasks go here yourtask: externalFile }); }; 

这应该工作,但我现在不能运行它,你还没有发布你在单独的文件中定义了什么样的信息。 你也可以在那里有一个function。

我遇到了同样的问题,这是我的解决scheme。 我希望它能帮助现在/将来的人:

  1. 创build一个npm包来托pipeGruntfile.js并将其上传到一个仓库(我目前使用的是Bitbucket)这就是我的package.json的样子:

    { "name": "my-remote-gruntfile-package", "private": true, "version": "0.0.1" }

  2. 在当前项目的package.json中进行configuration,并使用npm install或者直接运行

    npm install git+ssh://git@bitbucket.org:user/my-remote-gruntfile-package.git#0.0.1

  3. 创build一个bash脚本(或者相当于windows bash)来覆盖改变Gruntfile.js位置和设置当前基本path的问题。 这是为了避免在每次执行时传递–gruntfile和–base参数(这一步不是强制性的)

    #!/bin/bash grunt --base ./ --gruntfile ./node_modules/my-remote-gruntfile-package/GruntFile.js $1

注意:照顾你的Gruntfile.js上使用的相对path