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

fail gracefully when minification fails #42

Merged
merged 3 commits into from
Aug 29, 2016
Merged
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
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