简体   繁体   中英

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

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 ( Eg : 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/project1 or 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM