【发布时间】: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