如何在项目外安装 g运行t 和项目目录中的 运行 任务

How to install grunt outside of a project and run tasks from the project directory

我正在观看这一系列教程here,我可以看到 g运行t(不是 g运行t-cli)文件存储在项目中根.

我看到通常是这种情况,但想将我的文件放在别处。

有没有办法做到这一点。显然我可以 运行 init 在 say /grunt 中创建 package.json 文件并在那里进行安装,但是告诉它如何与我的项目交互会相对容易吗 /root/project.

这是您的结构的潜在示例:

main-folder
├── package.json
├── Gruntfile.js
├── grunt
│   ├── config.js
│   ├── config
│   │   │   ├── copy.js
│   │   │   ├── sass.js
│   │   │   ├── sync.js
│   │   │   ├── linkAsset.js
│   │   │   └── uglify.js
│   ├── register
│   │   │   ├── compileAssets.js
│   │   │   ├── linkAssets.js
│   │   │   ├── build.js
│   │   │   └── buildProd.js
├── project-1
│   └── folders and files ...
├── project-2
│   └── folders and files ...
  • package.json :包含你所有的 grunt-plugins 依赖和其他有用的 npm 模块
  • Gruntfile.js : 加载和配置 grunt/configgrunt/register 文件夹中的所有任务

如果您想获得更多信息来设置此 grunt 配置,请阅读此内容:

配置文件

我建议您也使用配置文件(例如 : main-folder/grunt/config.js)文件来注册一些快捷方式、变量以使您的 grunt 任务更加动态。

示例:

var version = '0.1.0';
var project1Dir = 'project-1';
var project2Dir = 'project-2';

module.exports.version = version;
module.exports.project1Dir = project1Dir;
module.exports.project2Dir = project2Dir;

并在每个任务中导入此配置:var config = require('../config');。例如,如果重命名文件夹 project1.

,重构代码将很容易

运行 任务 :

现在,当您在您的目录(main-folder/project1main-folder/project2)中工作并输入您的 grunt 命令时,使用 b 标志告诉 grunt 您的 Gruntfile.js 文件。

示例:

grunt -b ../ build

您也可以在代码中配置此行为。阅读 Grunt 文档以获取更多信息:Grunt CLI - b flag