Firstly, (as noted in the comments), naming folders with a .js
suffix is bad practice. You really need to change your projects folder nomenclature if you can. However, if that's not feasible, it is possible to get grunt-contrib-uglify to ignore folders whose name ends in .js
.
This entails dynamically generating the configuration object for the uglify
Task via the use of a custom helper function (namely configureUglify
in the example gist below). The resultant configuration will be very similar to this example shown in the uglify
documentation, whereby each .js
file is defined in an Array. The main difference being is that instead of manually defining each filepath we dynamically generate it instead.
Gruntfile.js
module.exports = function (grunt) {
grunt.initConfig({
uglify : {
// <-- // Intentionally empty, will be auto generated.
}
});
/**
* Helper function creates config object for uglify Task.
*/
function configureUglify() {
var glob = 'app/**/*.js',
config = {},
jsFiles = [];
grunt.file.expand({ filter: 'isFile' }, glob).forEach(function (filePath) {
jsFiles.push(filePath)
});
config.target = {
files : {
'build/dest.js' : jsFiles
}
}
grunt.config('uglify', config);
grunt.task.run(['uglify']);
}
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.registerTask('uglifyJs', configureUglify);
grunt.registerTask('default', ['uglifyJs']);
};
Notes
The configureUglify
helper function (above) utilizes grunt.file.expand to iterate over each path found via the glob
pattern 'app/**/*.js'
.
The { filter: 'isFile' }
part ensures that only paths to files ending in .js
are processed (i.e. The folder FileSaver.js
or any other folder whose name ends in .js
are simply ignored ).
The globbing pattern 'app/**/*.js'
will find all .js
files stored in the app
directory, including any found in sub-folders. If you want to uglify only the files stored in folder named FileSaver.js
then change the glob
pattern to:
var glob = 'app/assets/lib/amcharts/dist/amcharts/plugins/export/libs/FileSaver.js/**/*.js'
- Running
$ grunt
via your commandline using the Gruntfile.js
example gist provided will result in a single file named dest.js
saved to the build
folder/directory.
Important
When combining multiple files into one single .js
file (e.g. dest.js
) the order in which the files are combined is often important to ensure the application functions correctly. So you may find that explicitly defining the paths Array in your Gruntfile.js
, (instead of dynamically as above, which does not guarantee the correct order), better achieves your requirements. For example:
grunt.initConfig({
uglify: {
my_target: {
files: {
'build/dest.js': [
'app/assets/lib/foo.js',
'app/assets/lib/amcharts/dist/amcharts/plugins/export/libs/baz.js',
'app/assets/lib/amcharts/dist/amcharts/plugins/export/libs/FileSaver.js/a.js',
'app/assets/lib/amcharts/dist/amcharts/plugins/export/libs/FileSaver.js/b.js',
'app/assets/lib/amcharts/dist/amcharts/plugins/export/libs/FileSaver.js/c.js'
]
}
}
}
});
Also, typically the single resultant uglified/minified .js
file is named with a .min.js
suffix - this is just general good practice. So you may want to consider changing 'build/dest.js'
to 'build/dest.min.js'