diff --git a/Gruntfile.js b/Gruntfile.js index baa2086d..785b57df 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -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'] + } } }, diff --git a/tasks/uglify.js b/tasks/uglify.js index 37ffcc87..b8f856f5 100644 --- a/tasks/uglify.js +++ b/tasks/uglify.js @@ -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); @@ -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); diff --git a/test/fixtures/expected/fail_to_minify.js b/test/fixtures/expected/fail_to_minify.js new file mode 100644 index 00000000..5af858e3 --- /dev/null +++ b/test/fixtures/expected/fail_to_minify.js @@ -0,0 +1,8 @@ +// Ideally this comment should get removed + +function failToMinify() { + var @@name = 1; +} + + +/* This too */ diff --git a/test/fixtures/src/fail_to_minify.js b/test/fixtures/src/fail_to_minify.js new file mode 100644 index 00000000..5af858e3 --- /dev/null +++ b/test/fixtures/src/fail_to_minify.js @@ -0,0 +1,8 @@ +// Ideally this comment should get removed + +function failToMinify() { + var @@name = 1; +} + + +/* This too */ diff --git a/test/uglify_test.js b/test/uglify_test.js index 6eea0102..63bba9f6 100644 --- a/test/uglify_test.js +++ b/test/uglify_test.js @@ -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',