如何在项目外部安装grunt并从项目目录运行任务 [英] How to install grunt outside of a project and run tasks from the project directory

查看:325
本文介绍了如何在项目外部安装grunt并从项目目录运行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看这里的这一系列教程,我可以看到咕噜声(而不是grunt-cli)文件存储在项目根目录中。



我发现这通常是这种情况,但是希望将其他文件放到其他位置。



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

解决方案

你的结构:

  main-folder 
├──package.json
├──Gruntfile.js
├──grunt
│├──config.js
│├──config
│││├──copy.js
│││├─ ─sass.js
│││├──sync.js
│││├──linkAsset.js
│││└──uglify.js
│├─ ─寄存器
│││├──compileAssets.js
│││├──linkAssets.js
│││├──build.js
│││└─ ─buildProd.js
├──项目-1
│└──文件夹和文件...
├ ─项目-2
│└──文件夹和档案...




  • package.json :包含您所有的grunt插件依赖项和其他有用的npm模块 Gruntfile.js :load并配置 grunt / config grunt / register 文件夹中的所有任务



如果您想获得更多信息来设置此grunt的配置,请阅读本文:如何创建和组织配置并注册grunt任务


$ b 配置文件

我建议你也使用一个配置文件( Eg main-folder / grunt / config.js )来注册一些快捷方式,变量让你的咕噜声更加动态。



示例:

  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(' ../配置'); 。如果您重命名文件夹 project1 ,那么重构代码将很容易。

运行任务

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



示例:

  grunt -b ../ build 

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


I'm watching this series of tutorials here and I can see that the grunt ( not the grunt-cli ) files are stored in the project root.

I see that this is normally the case but wanted to put my files elsewhere.

Is there a way to do this. Obviously I could run the init to create the package.json file in say /grunt and do the install there, but would it be relatively easy to tell it how to interface with my project /root/project.

解决方案

Here is an potential example of your structure :

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 : contains all your grunt-plugins dependencies and other useful npm modules
  • Gruntfile.js : load and configure all tasks in grunt/config and grunt/register folder

Read this if you want to have more information to setup this configuration of grunt : How to create and organise config and register grunt tasks

Configuration file :

I recommend you also to use a configuration file (E.g : main-folder/grunt/config.js) file to register some shortcut, variables to make your grunt tasks more dynamic.

Example :

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;

And import this config in each task with : var config = require('../config');. It will be easy to refactor the code if you rename for example the folder project1.

Run tasks :

Now when you are working in your directory (main-folder/project1or main-folder/project2) and entering your grunt command, use b flag to tell to grunt where is your Gruntfile.js file.

Example :

grunt -b ../ build

You can also configure in the code this behavior. Read Grunt documentation for more information : Grunt CLI - b flag

这篇关于如何在项目外部安装grunt并从项目目录运行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆