diff --git a/docs/uglify-options.md b/docs/uglify-options.md index 4d623147..48d4252a 100644 --- a/docs/uglify-options.md +++ b/docs/uglify-options.md @@ -31,12 +31,12 @@ Default: `false` Parse a single expression, rather than a program (for parsing JSON) ## report -Choices: `'none'`, `'min'`, `'gzip'` +Type: `string` +Choices: `'min'`, `'gzip'` Default: `'min'` -Either report only minification result or report minification and gzip results. -This is useful to see exactly how well UglifyJS is performing but using `'gzip'` will make the task take 5-10x longer to complete. [Example output](https://github.com/sindresorhus/maxmin#readme). -If `'none'` is used the report will be generated on the verbose output. +Report minification result or both minification and gzip results. +This is useful to see exactly how well uglify-js is performing but using `'gzip'` will make the task take 5-10x longer to complete. [Example output](https://github.com/sindresorhus/maxmin#readme). ## sourceMap Type: `Boolean` diff --git a/tasks/uglify.js b/tasks/uglify.js index 9814517d..0e502067 100644 --- a/tasks/uglify.js +++ b/tasks/uglify.js @@ -24,20 +24,6 @@ function relativePath(file1, file2) { return ''; } -function reportFacility(grunt, options) { - var reporter; - switch (options.report) { - case 'none': - reporter = grunt.verbose; - break; - default: - case 'min': - case 'gzip': - reporter = grunt.log; - } - return reporter; -} - // Converts \r\n to \n function normalizeLf(string) { return string.replace(/\r\n/g, '\n'); @@ -47,6 +33,16 @@ module.exports = function(grunt) { // Internal lib. var uglify = require('./lib/uglify').init(grunt); + var getAvailableFiles = function (filesArray) { + return filesArray.filter(function (filepath) { + if (!grunt.file.exists(filepath)) { + grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found'); + return false; + } + return true; + }); + }; + grunt.registerMultiTask('uglify', 'Minify files with UglifyJS.', function() { // Merge task-specific and/or target-specific options with these defaults. var options = this.options({ @@ -64,25 +60,23 @@ module.exports = function(grunt) { screwIE8: true, quoteStyle: 0 }); - var log = reportFacility(grunt, options); var footer = normalizeLf(options.footer); var mapNameGenerator, mapInNameGenerator; - var createdFiles = 0; - var createdMaps = 0; + var created = { + maps: 0, + files: 0 + }; + var size = { + before: 0, + after: 0 + }; // Iterate over all src-dest file pairs. this.files.forEach(function (f) { - var src = f.src.filter(function (filepath) { - // Warn on and remove invalid source files (if nonull was set). - if (!grunt.file.exists(filepath)) { - grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found.'); - return false; - } - return true; - }); + var availableFiles = getAvailableFiles(f.src); - if (src.length === 0) { + if (availableFiles.length === 0) { grunt.log.warn('Destination ' + chalk.cyan(f.dest) + ' not written because src files were empty.'); return; } @@ -101,7 +95,7 @@ module.exports = function(grunt) { // function to get the name of the sourceMapIn file if (typeof options.sourceMapIn === 'function') { - if (src.length !== 1) { + if (availableFiles.length !== 1) { grunt.fail.warn('Cannot generate `sourceMapIn` for multiple source files.'); } mapInNameGenerator = options.sourceMapIn; @@ -126,7 +120,7 @@ module.exports = function(grunt) { // Dynamically create incoming sourcemap names if (mapInNameGenerator) { try { - options.sourceMapIn = mapInNameGenerator(src[0]); + options.sourceMapIn = mapInNameGenerator(availableFiles[0]); } catch (e) { err = new Error('SourceMapInName failed.'); err.origError = e; @@ -151,46 +145,53 @@ module.exports = function(grunt) { // Minify files, warn and fail on error. var result; try { - result = uglify.minify(src, f.dest, options); + result = uglify.minify(availableFiles, f.dest, options); } catch (e) { console.log(e); err = new Error('Uglification failed.'); if (e.message) { err.message += '\n' + e.message + '. \n'; if (e.line) { - err.message += 'Line ' + e.line + ' in ' + src + '\n'; + err.message += 'Line ' + e.line + ' in ' + availableFiles + '\n'; } } err.origError = e; - grunt.log.warn('Uglifying source ' + chalk.cyan(src) + ' failed.'); + grunt.log.warn('Uglifying source ' + chalk.cyan(availableFiles) + ' failed.'); grunt.fail.warn(err); } // Concat minified source + footer var output = result.min + footer; + var unCompiledJSString = availableFiles.map(function (file) { + return grunt.file.read(file); + }).join(''); + // Write the destination file. grunt.file.write(f.dest, output); + size.before += unCompiledJSString.length; + // Write source map if (options.sourceMap) { grunt.file.write(options.generatedSourceMapName, result.sourceMap); - log.writeln('File ' + chalk.cyan(options.generatedSourceMapName) + ' created (source map).'); - createdMaps++; + grunt.verbose.writeln('File ' + chalk.cyan(options.generatedSourceMapName) + ' created (source map).'); + created.maps++; } var outputSize = maxmin(result.max, output, options.report === 'gzip'); - log.writeln('File ' + chalk.cyan(f.dest) + ' created: ' + outputSize); + grunt.verbose.writeln('File ' + chalk.cyan(f.dest) + ' created: ' + chalk.dim(outputSize)); - createdFiles++; + created.files++; + size.after += output.length; }); - if (createdMaps > 0) { - grunt.log.ok(createdMaps + ' source' + grunt.util.pluralize(createdMaps, 'map/maps') + ' created.'); + if (created.maps > 0) { + grunt.log.ok(created.maps + ' source' + grunt.util.pluralize(created.maps, 'map/maps') + ' created.'); } - if (createdFiles > 0) { - grunt.log.ok(createdFiles + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created.'); + if (created.files > 0) { + grunt.log.ok(created.files + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created ' + chalk.dim(maxmin(size.before, size.after))); } else { grunt.log.warn('No files created.'); }