tschaub / grunt-newer

Configure Grunt tasks to run with newer files only.
https://npmjs.org/package/grunt-newer
MIT License
945 stars 61 forks source link

Introduce delegate task #88

Closed tfrommen closed 8 years ago

tfrommen commented 8 years ago

As stated in https://github.com/tschaub/grunt-newer/issues/29#issuecomment-160223515, I added a simple delegation task (named delegate). This allows to specifiy for individual tasks src (and also dest) for newer to check, and then actually run the task with its own src ( and dest). This is useful for SASS/LESS.

With the following config:

grunt.initConfig( {
    delegate: {
        sass: {
            src: [ 'resources/scss/**/*.scss' ]
        }
    },
    sass: {
        styles: {
            expand: true,
            cwd: 'resources/scss/',
            src: [ '*.scss' ],
            dest: 'assets/css/',
            ext: '.css'
        }
    }
} );

one could simply run grunt newer:delegate:sass:styles to actually have sass:styles be run when there are new .scss files (even in subfolders).

tfrommen commented 8 years ago

I just improved the new delegate task.

As we need at least src specified for the task/target, the delegate task is now a multi task, requiring a config.

I also integrated the possibility to specify a custom task (with an optional target).

Her's the above example, a little bit expanded:

grunt.initConfig( {
    delegate: {
        sass: {
            src: [ 'resources/scss/**/*.scss' ]
        },
        sass_foo: {
            task: 'sass:foo',
            src: [ 'resources/scss/foo/*.scss' ]
        },
        'sass-styles': {
            task: 'sass:styles',
            src: [ 'resources/scss/**/*.scss' ]
        }
    },
    sass: {
        foo: {
            expand: true,
            cwd: 'resources/scss/',
            src: [ 'foo.scss' ],
            dest: 'assets/css/',
            ext: '.css'
        },
        styles: {
            expand: true,
            cwd: 'resources/scss/',
            src: [ '*.scss' ],
            dest: 'assets/css/',
            ext: '.css'
        }
    }
} );

When calling $ grunt newer:delegate:sass, the sass task will be executed (with all available targets). Calling $ grunt newer:delegate:sass-foo only runs sass:foo, while $ grunt newer:delegate:sass-styles results in running sass:styles.

All runs are only performed if there are changed files wrt. the src given in the according delegate target.

Of course, you can still call a task with a target directly via command line, for instance $ grunt newer:delegate:sass:styles.

tfrommen commented 8 years ago

I just published this (in a more condensed form) as new Grunt plugin: grunt-delegate.