【问题标题】:Loading grunt tasks from a remote Gruntfile.js从远程 Gruntfile.js 加载 grunt 任务
【发布时间】:2017-02-03 23:48:06
【问题描述】:

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

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

使用--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

创建一个位于我项目根目录的 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 文件加载任务。我会找到另一个解决方案。

【问题讨论】:

  • 您究竟为什么要这样做? IE。也许您想在需要钻孔的地方使用锤子?
  • 我想在由多个开发人员维护的多个项目之间共享一个独特的 gruntfile.js。此文件中定义的 grunt 任务必须可从每个项目根目录访问。
  • 我的建议:不要这样做。几个月后你会拔掉头发。最好使用某种模板/文件生成器系统为每个项目轻松创建它们。更好的是:完全放弃咕噜声。我从一开始就一直在使用 grunt,并且在去年将它从我所有的项目中删除,除了一些例外。你几乎从不需要它(因为 npm 脚本几乎总是足够的)
  • 放弃咕噜声可能是一个解决方案。但我现在没有权力这样做。当我对构建任务进行更改时,我已经开始努力让三十个 gruntfile.js 保持最新......我想我会用它制作一个自定义的 grunt 插件,并将其添加为我所有项目的 devDependency。

标签: javascript node.js gruntjs


【解决方案1】:

我遇到了同样的问题,这是我的解决方案。我希望它对现在/未来的人有所帮助:

  1. 创建一个 npm 包来托管 Gruntfile.js 并将其上传到存储库(我目前正在使用 Bitbucket)这是我的 package.json 的样子:

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

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

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

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

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

注意:注意 Gruntfile.js 中使用的相对路径

【讨论】:

    【解决方案2】:

    需要一个单独的文件应该可以工作,注意你不应该通过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
        });
    };
    

    这应该可以,但我现在无法运行它,而且您还没有发布您在单独文件中定义的信息类型。你也可以在里面有一个函数。

    【讨论】:

    • 感谢您的回答。但是找到一种从localhost 访问文件的方法是我问题的第一个目的。我不希望 Gruntfile.js 位于我的磁盘上。我希望它存储在远程服务器上,可以通过网络访问。我只是使用本地 Apache 服务器进行测试。
    • 然后它变得有点复杂,因为你必须下载它,等待它下载然后启动 Grunt。不知道开箱即用的 Grunt 有多大可能。
    猜你喜欢
    • 1970-01-01
    • 2016-03-02
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多