Giter Site home page Giter Site logo

load-grunt-parent-tasks's Introduction

load-grunt-parent-tasks

Loads de-duped grunt tasks from parent or sibling modules.

TL;DR

load-grunt-parent-tasks is a Grunt library that allows you to load all of your Grunt tasks even if they are in a parent folder. This library was heavily inspired by this StackOverflow post: "Centralise Node Modules In Project With Subproject", and and utilizes the gruntcollection feature of grunt.loadNpmTasks.

Description

If Grunt task is loaded as a submodule then create a grunt-collection module in your local node_modules folder to trick grunt into loading dependencies from a parent folder. This might be necessary if project A contains a Gruntfile.js with dependencies X, Y, and Z, and is loaded as a npm dependency of project B which contains dependencies A, X and Y. In that scenario Npm will de-dupe project A's Npm dependencies when executing npm install in project B and will look like this:

B
├── node_modules
│   ├── A
│   │   ├── Gruntfile.js
│   │   ├── node_modules
│   │   │   └── Z
│   │   └── package.json (has depencencies X,Y,Z)
│   ├── X
│   └── Y
└── package.json (has dependencies A,X,Y)

If project A's package.json contains an install or postinstall script that executes it's Gruntfile.js the Grunt task will throw 2 errors:

Local Npm module "X" not found. Is it installed?
Local Npm module "Y" not found. Is it installed?

Project A's Grunt task will not be able to load the de-duped dependencies because they reside in the parents node_modules folder. This can be fixed by dropping in a new module which contains project A's package.json with the addition of a keywords property that contains "gruntcollection" in it's array.

B
├── node_modules
│   ├── A
│   │   ├── Gruntfile.js
│   │   ├── node_modules
│   │   │   ├── grunt-collection
│   │   │   │   └── package.json (has dependencies X,Y,Z and 'keywords: ["gruntcollection"]')
│   │   │   └── Z
│   │   └── package.json (has depencencies X,Y,Z)
│   ├── X
│   └── Y
└── package.json (has dependencies A,X,Y)

Then at the beginning of your Gruntfile.js you call grunt.loadNpmTasks('grunt-collection'). Now Grunt will search up for the current directory to find the modules to load.

Features

  • Checks if the main Grunt task is a submodule of another project.
  • Creates a grunt-collection module in your local node_modules folder.
  • Creates a grunt-collection/package.json that is a mirror of your projects package.json file defined by options.config.
  • Filters out npm modules to load using globbing patterns defined in options.pattern.
  • Filters out which dependencies key to load from defined by options.scope.

##Installation

npm install --save load-grunt-parent-tasks

##Example

Basic Gruntfile.js

module.exports = function(grunt) {

  require('load-grunt-parent-tasks')(grunt);

};

Creates the following:

A
├── Gruntfile.js
├── node_modules
│   └── grunt-collection
│       └── package.json
└── package.json

Gruntfile.js with options

module.exports = function(grunt) {

  require('load-grunt-parent-tasks')(grunt, {
    config: 'package.json',
    pattern: 'grunt-*',
    scope: 'dependencies',
    module: 'abc-def'
  });

};

// Can also be written as:
module.exports = function(grunt) {

  require('load-grunt-parent-tasks')(grunt, {
    config: require('package.json'),
    pattern: ['grunt-*'],
    scope: ['dependencies'],
    module: 'abc-def'
  });

};

Creates the following:

A
├── Gruntfile.js
├── node_modules
│   └── abc-def
│       └── package.json
└── package.json

Options

config

Type: String, Object
Default: Path to nearest package.json

pattern

Type: String, Array
Default: 'grunt-*' (globbing pattern)

scope

Type: String, Array
Default: ['dependencies', 'optionalDependencies']

module

Type: String
Default: 'grunt-collection'

The module option can be changed in case grunt-collection ever conflicts with any other package name.

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

License

MIT © Larry Gordon

load-grunt-parent-tasks's People

Contributors

frankcortes avatar psyrendust avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

load-grunt-parent-tasks's Issues

configuration

Hi I tried the configuration you specified however it didn't quite work. Perhaps I am missing something? If you can kindly point it out it would be much appreciated.

My folder hierarchy is as follows:

  • node_modules // containing all the grunt plugins
  • package.json // (1) please refer to below for actual code
  • Project A
    • node_modules
      • grunt-collection
        • package.json // (2) please refer to below for actual code
      • load-grunt-parent-tasks
    • GruntFile.js // please refer to below for actual code
    • package.json // (3) please refer to below for actual code

package.json (1)

{
"name": "template",
"version": "0.0.0",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-sass": "~0.8.1",
"grunt-contrib-watch": "~0.6.1",
"grunt-contrib-uglify": "~0.6.0",
"grunt-contrib-concat": "~0.5.0",
"grunt-autoprefixer": "~2.0.0",
"grunt-banner": "~0.2.3",
"grunt-browser-sync": "~1.5.3",
"grunt-ftp-upload": "~0.1.1",
"grunt-ftp-deploy": "~0.1.9",
"grunt-contrib-jasmine": "~0.6.5"
}
}

package.json (2)

{
"name": "template",
"version": "0.0.0",
"description": "",
"devDependencies": {
"load-grunt-parent-tasks": "~0.1.1"
}
}

package.json (3)

{
"version": "0.0.0",
"description": "",
"devDependencies": {
"grunt": "~0.4.5",
"grunt-contrib-sass": "~0.8.1",
"grunt-contrib-watch": "~0.6.1",
"grunt-contrib-uglify": "~0.6.0",
"grunt-contrib-concat": "~0.5.0",
"grunt-autoprefixer": "~2.0.0",
"grunt-banner": "~0.2.3",
"grunt-browser-sync": "~1.5.3",
"grunt-ftp-upload": "~0.1.1",
"grunt-ftp-deploy": "~0.1.9",
"grunt-contrib-jasmine": "~0.6.5"
},
"keywords": ["gruntcollection"]
}

GruntFile.js

module.exports = function(grunt){

require('load-grunt-parent-tasks')(grunt, {
    config: 'package.json',
    pattern: 'grunt-*',
    scope: 'dependencies',
    module: 'grunt-collection'
});

grunt.initConfig({
     pkg : grunt.file.readJSON('package.json'),
     /*
     * grunt plugin config in here
     */
}); 

grunt.loadNpmTasks("grunt-collection");

grunt.registerTask("mon", ["browserSync", "watch"]);

};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.