Skip to content

Commit

Permalink
handle minification failure gracefully
Browse files Browse the repository at this point in the history
Fixes: gruntjs#409

Whenever the minification of a file fails, the task fails with the
error 'min' is not available in 'undefined'.

This patch allows the task to continue to the next file without
making any changes to the contents of the file in the destination.
Also, this introduces a new configuration option `skipErrors`, so
that the users can specify that in the task level options to override
the default behaviour.
  • Loading branch information
thefourtheye committed Mar 21, 2018
1 parent e7915ce commit de86db1
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
8 changes: 8 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,14 @@ module.exports = function(grunt) {
max_line_len: 55
}
}
},
fail_to_minify: {
options: {
skipErrors: true
},
files: {
'tmp/fail_to_minify.js': ['test/fixtures/src/fail_to_minify.js']
}
}
},

Expand Down
38 changes: 24 additions & 14 deletions tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ module.exports = function(grunt) {
mangle: {},
beautify: false,
report: 'min',
ie8: false
ie8: false,
skipErrors: grunt.option('force') || false
});

var footer = normalizeLf(options.footer);
Expand Down Expand Up @@ -138,29 +139,38 @@ module.exports = function(grunt) {

// Minify files, warn and fail on error.
var result;
var isFailed = false;

try {
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 ' + availableFiles + '\n';
}
}
err = new Error(e.message + ' in line ' + e.line + '\n');
err.origError = e;
grunt.log.warn('Uglifying source ' + chalk.cyan(availableFiles) + ' failed.');
grunt.fail.warn(err);
if (options['skipErrors']) {
grunt.log.error('Uglifying source ' + chalk.cyan(availableFiles) + ' failed.' + (options['skipErrors'] ? ' Skipping...' : ''));
grunt.log.error(err);
} else {
grunt.log.error('Uglifying source ' + chalk.cyan(availableFiles) + ' failed.');
grunt.fail.fatal(err);
}
isFailed = true;
}

// Concat minified source + footer
var output = result.min + footer;

var unCompiledJSString = availableFiles.map(function (file) {
return grunt.file.read(file);
}).join('');

// If the minifying fails, use the actual string as it is
if (isFailed) {
result = {
min: unCompiledJSString,
max: unCompiledJSString
};
}

// Concat minified source + footer
var output = isFailed ? unCompiledJSString : (result.min + footer);

// Write the destination file.
grunt.file.write(f.dest, output);

Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/expected/fail_to_minify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Ideally this comment should get removed

function failToMinify() {
var @@name = 1;
}


/* This too */
8 changes: 8 additions & 0 deletions test/fixtures/src/fail_to_minify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Ideally this comment should get removed

function failToMinify() {
var @@name = 1;
}


/* This too */
1 change: 1 addition & 0 deletions test/uglify_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exports.contrib_uglify = {
'compress_mangle_banner.js',
'compress_mangle_beautify.js',
'compress_mangle_except.js',
'fail_to_minify.js',
'multifile.js',
'wrap.js',
'maxLineLen.js',
Expand Down

0 comments on commit de86db1

Please sign in to comment.