Skip to content

Commit

Permalink
Log fixes. (#454) r=vladikoff
Browse files Browse the repository at this point in the history
Now the output is similar to grunt-contrib-cssmin.
  • Loading branch information
XhmikosR authored and vladikoff committed Apr 4, 2017
1 parent 48fab36 commit 7dc5e1d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
8 changes: 4 additions & 4 deletions docs/uglify-options.md
Expand Up @@ -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`
Expand Down
79 changes: 40 additions & 39 deletions tasks/uglify.js
Expand Up @@ -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');
Expand All @@ -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({
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.');
}
Expand Down

0 comments on commit 7dc5e1d

Please sign in to comment.