Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the configuration of the task uglify dynamically

Tags:

gruntjs

I need to change configuration of my uglify task for only minify file as needed (as explained here for the jshint task : https://github.com/gruntjs/grunt-contrib-watch#compiling-files-as-needed)

The modification works well for the jshint task but not for uglify, i think the problem is the property path...

Any help would be appreciated ;)

Here is my Gruntfile.js :

module.exports = function (grunt) {
    grunt.initConfig({

        // define source files and their destinations
        jshint: {
            all: ['dev/**/*.js'],
        },
        uglify: {
            dynamic_mappings: {
              // Grunt will search for "**/*.js" under "dev/" when the "minify" task
              // runs and build the appropriate src-dest file mappings then, so you
              // don't need to update the Gruntfile when files are added or removed.
            files: [{
                  expand: true,     // Enable dynamic expansion.
                  cwd: 'dev/',      // Src matches are relative to this path.
                  src: ['**/*.js'], // Actual pattern(s) to match.
                  dest: 'build',   // Destination path prefix.
                  ext: '.min.js',   // Dest filepaths will have this extension.
                },
              ],
            }
        }
        watch: {
        options: { spawn: false },
            js:  { files: 'dev/**/*.js', tasks: [ 'uglify' ] },
        }
    });

    // load plugins
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    // default task
    grunt.registerTask('default', [ 'watch' ]);

    grunt.event.on('watch', function(action, filepath) {
        grunt.config(['jshint', 'all'], filepath);
        grunt.config('uglify.dynamic_mappings.files', [{src: filepath }]);
    });

};
like image 778
Fab Avatar asked Aug 08 '13 14:08

Fab


1 Answers

The line

grunt.config('uglify.dynamic_mappings.files', [{src: filepath }]);

Is completely replacing the original configuration for uglify.dynamic_mappings.files

Instead try including the other original parameters along with the new src: filepath

like image 177
dc5 Avatar answered Nov 20 '22 03:11

dc5