Javascript 从远程GrunFile.js加载grunt任务

Javascript 从远程GrunFile.js加载grunt任务,javascript,node.js,gruntjs,Javascript,Node.js,Gruntjs,在项目的根目录中,我试图从远程Gruntfile.js加载Grunt任务以及文件的所有内容,该文件可通过网络(web或内部网络)访问 我已经尝试了几种方法(见下文),首先在本地Apache服务器上托管Gruntfile.js以进行测试 使用--gruntfile选项 > /project/path> grunt --gruntfile=http://localhost/Gruntfile.js build Reading "Gruntfile.js" Gruntfile...ERRO

在项目的根目录中,我试图从远程Gruntfile.js加载Grunt任务以及文件的所有内容,该文件可通过网络(web或内部网络)访问

我已经尝试了几种方法(见下文),首先在本地Apache服务器上托管Gruntfile.js以进行测试

使用
--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.
> /project/path> grunt --base=http://localhost/Gruntfile.js build
    process.chdir(grunt.option('base') || path.dirname(gruntfile));
Error: ENOENT, no such file or directory
使用
--base
选项

> /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.
> /project/path> grunt --base=http://localhost/Gruntfile.js build
    process.chdir(grunt.option('base') || path.dirname(gruntfile));
Error: ENOENT, no such file or directory
创建位于我的项目根目录下的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.
再次尝试使用grunfile.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文件加载任务。我会找到另一个解决方案。

需要一个单独的文件应该可以,请注意,您不应该通过
localhost
引用它,而是通过磁盘上的位置来引用它,例如
/somedir/yourtask.js
。下面是一个例子:

yourtask.js

module.exports = {
    // your task config goes here
};
module.exports = function(grunt) {
    var externalFile = require('./somedir/yourtask.js');

    grunt.initConfig({
        // some random tasks go here
        yourtask: externalFile
    });
};
您的grunfile.js

module.exports = {
    // your task config goes here
};
module.exports = function(grunt) {
    var externalFile = require('./somedir/yourtask.js');

    grunt.initConfig({
        // some random tasks go here
        yourtask: externalFile
    });
};

这应该可以工作,但我现在无法运行它,而且您还没有发布您在单独的文件中定义的信息。您也可以在其中使用函数。

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

  • 创建一个npm包来承载Gruntfile.js,并将其上载到存储库(我目前正在使用Bitbucket),这就是my package.json的外观:

    {
    “名称”:“我的远程GrunFile包”,
    “私人”:没错,
    “版本”:“0.0.1”
    }

  • 在当前项目的package.json中配置它,并使用
    npm install
    安装它,或者直接运行它

    npm安装git+ssh://git@org:user/my remote grunfile package.git#0.0.1

  • 创建bash脚本(或windows bash等效脚本)以解决更改Gruntfile.js位置和设置当前基本路径的问题。这是为了避免在每次执行时传递--gruntfile和--base参数(此步骤不是强制性的)

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


  • 注意:注意Grunfile.js上使用的相对路径

    谢谢您的回答。但我的问题的首要目的是找到一种从中访问文件的方法。我不希望Grunfile.js位于我的磁盘上。我希望它存储在远程服务器上,可以通过网络访问。我只是使用一个本地Apache服务器进行测试,然后它会变得有点复杂,因为您必须下载它,等待它下载,然后启动Grunt。不知道有了开箱即用的咕噜声会有多大可能。你到底为什么要这样做?也就是说,也许你想在需要钻孔的地方使用锤子?我想在由多个开发人员维护的多个项目之间共享一个唯一的gruntfile.js。必须可以从每个项目根目录访问此文件中定义的grunt任务。我的建议是:不要这样做。几个月后你会把头发拔出来的。最好使用某种模板/文件生成器系统来轻松地为每个项目创建它们。更好的办法是:完全停止呼噜声。我从一开始就在使用grunt,在过去的一年里,我把它从我所有的项目中删除,除了少数例外。您几乎从不需要它(因为npm脚本几乎总是足够的),丢弃grunt可能是一个解决方案。但我现在没有权力这么做。当我在构建任务中进行更改时,我已经在竭尽全力保持三十个gruntfile.js的最新状态。。。我想我会用它制作一个定制的grunt插件,并将其添加为我所有项目的一个依赖项。