DarthVader
DarthVader

Reputation: 55022

Grunt uglify in place recursively

I have several JS, CSS and HTML files in my app. It is a huge app and I m trying to recursively minify and uglify these files inplace.

I have a build server that I fetch origin integration into it. Before throwing the files onto the servers I d like to minify in place under the same directory structure.

ie: /app/sales/checkout.js (normal file) minifed version should be in the same place: /app/sales/checkout.js

My grunt file is as follows:

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

    // define source files and their destinations
    uglify: {
        files: {
            src: 'com.foo.web.online.portal/**/*.js',  // source files mask
            expand: true,    // allow dynamic building
            flatten: true,   // remove all unnecessary nesting
            dest: ''
        }
    },
    watch: {
        js:  { files: 'js/*.js', tasks: [ 'uglify' ] },
    }
});

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

// register at least this one task
grunt.registerTask('default', [ 'uglify' ]);


};

Leaving destination empty didnt work.

I have many folders and subfolders: /checkout//several js files /sales//several js files /search/**/several js files

How can I minify in place and recursively?

Thanks.

Upvotes: 0

Views: 682

Answers (1)

RobC
RobC

Reputation: 24952

Utilize the grunt rename function to build the files object dynamically. This can enable files to be minified in place under the same directory structure (i.e. overwrite the original files data with newly minified data).

Gruntfile.js

The uglify Task can be configured as follows:

// ...
uglify: {
  dev: { // <-- include a target object
    files: [{
      expand: true,
      src: ['path/to/online/portal/**/*.js'],

      // The dest value should be whatever the src glob
      // pattern is, without the trailing /**/*.js part 
      dest: 'path/to/online/portal',

      cwd: '.',
      rename: function (dst, src) {
        return src;
      }
    }]
  }
},
//...

See the this section of the grunt-contrib-uglify documentation for further information.

Upvotes: 2

Related Questions