Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle minification failure gracefully #519

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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