Skip to content

Instantly share code, notes, and snippets.

@lusentis
Created October 20, 2012 15:46
Show Gist options
  • Save lusentis/3923658 to your computer and use it in GitHub Desktop.
Save lusentis/3923658 to your computer and use it in GitHub Desktop.
Grunt min task with support for minification without concatenation (one-to-one)
//.....
min: { // for production
vendor: {
src: ['public/js/vendor/*.js'],
dest: 'public_build/js/vendor.js',
separator: ';'
},
app: {
src: ['public/js/*.js'],
destDir: 'public_build/js/',
separator: ';'
}
},
//.....
grunt.registerMultiTask('min', 'Minify files with UglifyJS.', function() {
var that = this
, files = grunt.file.expandFiles(this.file.src)
, banner
, max
, min;
// Get banner, if specified. It would be nice if UglifyJS supported ignoring
// all comments matching a certain pattern, like /*!...*/, but it doesn't.
var banner = grunt.task.directive(files[0], function() { return null; });
if (banner === null) {
banner = '';
} else {
files.shift();
}
if (this.data.destDir) {
// uglify single files, without concatenating
files.forEach(function (file) {
var dest = path.join(that.data.destDir, file.split('/').pop());
var max = grunt.file.read(file);
var min = grunt.helper('uglify', max, grunt.config('uglify'));
grunt.file.write(dest, min);
grunt.log.writeln('File "' + dest + '" created.');
grunt.helper('min_max_info', min, max);
});
} else {
// concatenate and uglify
max = grunt.helper('concat', files, {separator: this.data.separator});
min = banner + grunt.helper('uglify', max, grunt.config('uglify'));
grunt.file.write(this.file.dest, min);
// Fail task if errors were logged.
if (this.errorCount) {
return false;
} else {
grunt.log.writeln('File "' + this.file.dest + '" created.');
grunt.helper('min_max_info', min, max);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment