firstandthird / load-grunt-config

Grunt plugin that lets you break up your Gruntfile config by task
firstandthird.github.io/load-grunt-config/
MIT License
374 stars 64 forks source link

Question: How to keep part of the overridden tasks. #123

Closed brunano21 closed 9 years ago

brunano21 commented 9 years ago

Hi all,

Let's start saying I'm into a multi project folder, where each folder contains css, js, html files. The folder structure is almost the following:

+ root\
    + .grunt\
        - jshint.js
    + js\
    + css\
    + app1\
        + .grunt\
            jshint.js
        + js\
        + css\
        - Gruntfile.js
    + app2\
        + .grunt\
            jshint.js
        + js\
        + css\
        - Gruntfile.js

Basically, I'm using this plugin to override the shared and common tasks as following: (from app1\Gruntfile.js)

var options = {
    configPath: path.join(process.cwd(), '..', '.grunt'),
    overridePath: path.join(process.cwd(), '.grunt'),
    init: true,
    data: {
    },
    ...
    ...
};

With the above code (as explained into the documentation) I'll load the "shared" grunt tasks which live under root/.grunt/ and if then a certain task (let's say root\app1.grunt\jshint.js for example) exists, the jshint task will be overridden. That's really awesome! However, I would like to keep the original src files of the "shared" jshint task (root.grunt\jshint.js), and add a supplementary src folders set defined into the jshint subtask (root\app1.grunt\jshint.js). How may I sort that out? Thanks!

SolomoN-ua commented 9 years ago

Hi @brunano21, if I correctly understand your question then you can use postProcess functionality (or mergeFunction, preMerge).

brunano21 commented 9 years ago

Could you provide an example? Even if it is not working. Thanks.

SolomoN-ua commented 9 years ago

Example from documentation.

require('load-grunt-config')(grunt, {
    // use different function to merge config files
    mergeFunction: require('recursive-merge'),

    //can post process config object before it gets passed to grunt
    postProcess: function(config) {},

    //allows to manipulate the config object before it gets merged with the data object
    preMerge: function(config, data) {}
});

so in case of posProcess you will get result config as an function param, and you can change any data in it before it will be passed to grunt.

brunano21 commented 9 years ago

Well, I found a possible solution, different from your:

var options = {
    configPath: path.join(process.cwd(), '.grunt', 'tasks'),
    init: true,
    data: {
        sharedConfig: grunt.file.readJSON(path.join(process.cwd(), '..', '.grunt' ,'config.json')),
        componentConfig: grunt.file.readJSON(path.join(process.cwd(), '.grunt' ,'config.json')),
    },

    jitGrunt: {
        pluginsRoot: '../node_modules',
        staticMappings: {
            availabletasks: 'grunt-available-tasks'
        },
    },
};
require('load-grunt-config')(grunt, options);

And then in my less.js task:

module.exports = function (grunt, options) {

    var path = require('path');
    var projectRoot = options.sharedConfig.cirrusRoot;
    var lessSrc = options.sharedConfig.bowerComp.lessSrc;
    var lessFiles = [];

    // loading shared less files into lessFiles array
    lessSrc.forEach(function(filePath) {
        lessFiles.push(path.join(projectRoot, filePath));
    });

    // loading component less files into lessFiles array
    lessFiles.push(options.componentConfig.resources.css);

    return {
        development: {
            files: [
                {
                    expand: true,
                    cwd: ".",
                    src: lessFiles,
                    ext: ".css"
                }
            ]
        }
    };
};

I know this is not best solution, but what you think?

SolomoN-ua commented 9 years ago

Another option, you can pass original src as data: {} and then use it in subtasks. For example:

// load-grunt-config
data: {
    shared: {
        jshint: {
            src: '../../..'
        }
    }
}

// in task config
{
   src: '<%= shared.jshint.src %>'
}
SolomoN-ua commented 9 years ago

@brunano21 we had similar idea at the same time ;)

brunano21 commented 9 years ago

Lol. In my case I'm using a config.json file containing the configuration stuff I want to share. However yep, we had the same idea :8ball:

SolomoN-ua commented 9 years ago

It actually doesn't matter how you will pass it to data: {} object. :)