Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Created May 19, 2016 15:46
Show Gist options
  • Save ricston-git/037527387511852ffcdda33c1c54f04d to your computer and use it in GitHub Desktop.
Save ricston-git/037527387511852ffcdda33c1c54f04d to your computer and use it in GitHub Desktop.
Gist for "Java Build Tools, JavaScript Package Managers and Task Runners (Part 3)" blog-post.
/* File: Gruntfile.js */
//wrapper function
module.exports = function(grunt) {
//Project and task configuration.
grunt.initConfig({
//this task is responsible for deleting all files and sub-folders under the folder "/prod".
clean: {
//here we use a globbing pattern to match everything inside the `prod` folder
prod: ['src/main/resources/prod/**/*']
},
//this task is responsible for minifying all JS files under "/dev"; with the result output in the "/prod" folder.
uglify: {
js: {
files: [{
//Option to create a one-to-one mapping of 'src' and 'dest' files.
expand: true,
//Source files: all files that match the wildcard are selected; excluding those with a trailing '.combo.js', '-min.js' and '.min.js'.
src: ['**/*.js', '!**/*.combo.js', '!**/*-min.js', '!**/*.min.js'],
//Destination folder: where the processed files will be output.
dest: 'src/main/resources/prod',
//Current working directory: from where the source files will be extracted.
cwd: 'src/main/resources/dev',
//the minified source is output with a trailing ".js"; after minification.
ext: '.js'
}]
}
}
});
//Load plugins and tasks (installed using npm).
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-uglify');
//create 'clean-log' task. This task is executed as per the task chain order below (see 'default' task). Its purpose is to output a log stating that the 'clean' task is executed next.
grunt.registerTask('clean-log', function(){
//log a message using grunt's "log" API.
grunt.log.writeln('Cleaning \'prod\' folder from previously generated files.');
});
//create 'uglify-log' task. This task is executed as per the task chain order below (see 'default' task). Its purpose is to output a log stating that the 'uglify' task is executed next.
grunt.registerTask('uglify-log', function(){
//log a message using grunt's "log" API.
grunt.log.writeln('Minifying custom JS files.')
});
//register task/s with 'default'. Every specified task in 'taskList' will be executed, in the order specified; when 'grunt' is run from termeinal.
grunt.registerTask('default', ['clean-log', 'clean', 'uglify-log', 'uglify:js']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment