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

不想你离开。 提交于 2020-01-06 08:27:07

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/33696297/how-to-install-grunt-outside-of-a-project-and-run-tasks-from-the-project-directo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!