Skip to content

Instantly share code, notes, and snippets.

@ricston-git
Created May 18, 2016 13:26
Show Gist options
  • Save ricston-git/4dddf16f8e53ef50c8de9fd63bbbe019 to your computer and use it in GitHub Desktop.
Save ricston-git/4dddf16f8e53ef50c8de9fd63bbbe019 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: gulpfile.js */
//import our gulp packages
var gulp = require('gulp'),
gutil = require('gulp-util')
minify = require('gulp-minify')
del = require('del');
//create a 'default' task - to be executed when 'gulp' is run from terminal. The 'default' task depends on 'clean-prod' and 'minify-js' tasks; in that order.
gulp.task('default', ['minify-js']);
//this task is responsible for deleting all files and sub-folders under the folder "/prod"; essentially cleaning the parent folder before generating minified code from "/dev".
gulp.task('clean-prod', function(){
//log a message using "gutil".
gutil.log('Cleaning \'prod\' folder from previously generated files.')
return del([
//here we use a globbing pattern to match everything inside the `prod` folder
'src/main/resources/prod/**/*',
]);
});
//this task is responsible for minifying all JS files under "/dev"; with the result output in the "/prod" folder.
gulp.task('minify-js', ['clean-prod'], function() {
//log a message using "gutil".
gutil.log('Minifying custom JS files.')
//set every ".js" file under "/dev" as source.
gulp.src('src/main/resources/dev/**/*.js')
.pipe(minify({
ext:{
//the original source is output with a trailing "-debug.js".
src:'-debug.js',
//the minified source is output with a trailing ".js"; after minification.
min:'.js'
},
//will not output the source in the destination directory.
noSource: 'true',
//ignore files with the following trails:
ignoreFiles: ['.combo.js', '-min.js', '.min.js']
}))
//the destination for the above implementation is defined next:
.pipe(gulp.dest('src/main/resources/prod'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment