Search code examples
javascriptgruntjsuglifyjs

How to uglify all javascript files in subfolders/subdirectory using Grunt?


I'm using Grunt to uglify javascipt files in my project.

Question:

How to uglify all javascript files in subfolders/subdirectory?

What I've done in Gruntfile.js:

Currently it will only uglify those javascript files in js folder since I'm using src: 'js/*.js'. However I have other directories in js folder that has javascript files:

├── CommonUtil.js
├── Paging
│   ├── dirPagination.js
│   └── dirPagination.tpl.html
├── angular-chart.js
├── angular-elastic-input.min.js
├── angularAlt.js
├── authenticationChallengeHandler
│   └── loginChallengeHandler.js

Gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        // define source files and their destinations
        uglify: {
            files: { 
                src: 'js/*.js',  // source files mask
                dest: 'jsm/',    // destination folder
                expand: true,    // allow dynamic building
                flatten: true,   // remove all unnecessary nesting
                ext: '.min.js'   // replace .js to .min.js
            }
        },
        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' ]);


};

Solution

  • You can use glob patterns like: src: '/**/*.js'