Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #42 from SAP/fix-fail-gracefully
Browse files Browse the repository at this point in the history
* fail gracefully when minification fails

* Adopt compression error handling

Make sure to provide the original error to be able to print the
stack trace with "--stack" option.

Fixes #36
  • Loading branch information
matz3 authored Aug 29, 2016
2 parents b2e6c0e + 8b7f548 commit 52b6c78
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions tasks/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -225,24 +225,29 @@ module.exports = function (grunt) {
options.compress.uglifyjs.output.comments = copyrightCommentsPattern;
}

switch (fileExtension) {
case '.js':
// Javascript files are processed by Uglify
fileContent = uglify.minify(fileContent, options.compress.uglifyjs).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 <pre> should be preserved (should only happen rarely)
if (!xmlHtmlPrePattern.test(fileContent)) {
fileContent = pd.xmlmin(fileContent, false);
try {
switch (fileExtension) {
case '.js':
// Javascript files are processed by Uglify
fileContent = uglify.minify(fileContent, options.compress.uglifyjs).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 <pre> should be preserved (should only happen rarely)
if (!xmlHtmlPrePattern.test(fileContent)) {
fileContent = pd.xmlmin(fileContent, false);
}

break;
}

break;
} catch (e) {
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;
Expand Down

0 comments on commit 52b6c78

Please sign in to comment.