From 7a3d92e661b36cb59e5624b8f2577f503a4e0bc5 Mon Sep 17 00:00:00 2001 From: Maximilian Lenkeit Date: Mon, 6 Jun 2016 11:42:25 +0200 Subject: [PATCH 1/2] fail gracefully when minification fails --- tasks/preload.js | 50 ++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/tasks/preload.js b/tasks/preload.js index 1cf30cd3..2671f220 100644 --- a/tasks/preload.js +++ b/tasks/preload.js @@ -199,7 +199,7 @@ module.exports = function (grunt) { var fileName = resourceMap[preloadFile].fullPath; var fileContent = grunt.file.read(fileName); var fileExtension = path.extname(fileName); - + var iOriginalSize, iCompressedSize; if (options.compress) { @@ -207,30 +207,34 @@ module.exports = function (grunt) { iOriginalSize = fileContent.length; iPreloadOriginalSize += iOriginalSize; - switch (fileExtension) { - case '.js': - // Javascript files are processed by Uglify - fileContent = uglify.minify(fileContent, { - fromString: true, - warnings: grunt.option('verbose') === true, - output: { - comments: copyrightCommentsPattern + try { + switch (fileExtension) { + case '.js': + // Javascript files are processed by Uglify + fileContent = uglify.minify(fileContent, { + fromString: true, + warnings: grunt.option('verbose') === true, + output: { + comments: copyrightCommentsPattern + } + }).code; + break; + case '.json': + // JSON is parsed and written to string again to remove unwanted white space + fileContent = JSON.stringify(JSON.parse(fileContent)); + break; + case '.xml': + // For XML we use the pretty data + + // Do not minify if XML(View) contains an <*:pre> tag because whitespace of HTML
 should be preserved (should only happen rarely)
+									if (!xmlHtmlPrePattern.test(fileContent)) {
+										fileContent = pd.xmlmin(fileContent, false);
 									}
-								}).code;
-								break;
-							case '.json':
-								// JSON is parsed and written to string again to remove unwanted white space
-								fileContent = JSON.stringify(JSON.parse(fileContent));
-								break;
-							case '.xml':
-								// For XML we use the pretty data
-
-								// Do not minify if XML(View) contains an <*:pre> tag because whitespace of HTML 
 should be preserved (should only happen rarely)
-								if (!xmlHtmlPrePattern.test(fileContent)) {
-									fileContent = pd.xmlmin(fileContent, false);
-								}
 
-								break;
+									break;
+								}
+							} catch (e) {
+								grunt.fail.warn('Failed to minify ' + fileName + '. This might be due to a syntax error in the file.');
 							}
 
 							iCompressedSize = fileContent.length;

From 8b7f548ac52993de622e6fa6b817989a8ba55800 Mon Sep 17 00:00:00 2001
From: Matthias Osswald 
Date: Mon, 29 Aug 2016 14:39:17 +0200
Subject: [PATCH 2/2] Adopt compression error handling

Make sure to provide the original error to be able to print the
stack trace with "--stack" option.
---
 tasks/preload.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tasks/preload.js b/tasks/preload.js
index ce84906c..c2bf23b5 100644
--- a/tasks/preload.js
+++ b/tasks/preload.js
@@ -246,7 +246,8 @@ module.exports = function (grunt) {
 									break;
 								}
 							} catch (e) {
-								grunt.fail.warn('Failed to minify ' + fileName + '. This might be due to a syntax error in the file.');
+								grunt.log.error('Failed to compress ' + fileName + '. This might be due to a syntax error in the file.');
+								grunt.fail.warn(e);
 							}
 
 							iCompressedSize = fileContent.length;