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

Commit

Permalink
Compression options for UglifyJS (#41)
Browse files Browse the repository at this point in the history
* Handle compression parameters for UglifyJS
* Merge defaults into given object
  • Loading branch information
Nikolay Kim authored and matz3 committed Aug 26, 2016
1 parent 8ef82a5 commit b2e6c0e
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 7 deletions.
16 changes: 16 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ module.exports = function(grunt) {
dest: 'test/preload/fixtures/library-same-dest'
},
libraries: '**'
},

'library_custom_uglify_params': {
options: {
resources: 'test/preload/fixtures/library-custom-uglify-params',
dest: 'tmp/preload/library_custom_uglify_params',
compress: {
uglifyjs: {
mangle: false,
output: {
ascii_only: true
}
}
}
},
libraries: '**'
}

},
Expand Down
26 changes: 19 additions & 7 deletions tasks/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,28 @@ module.exports = function (grunt) {
iOriginalSize = fileContent.length;
iPreloadOriginalSize += iOriginalSize;

// Convert default compression to empty configuration object
if (options.compress === true) {
options.compress = {};
}

// Make sure to have an object
options.compress.uglifyjs = options.compress.uglifyjs || {};

// Always override given options, override shouldn't be possible
options.compress.uglifyjs.fromString = true;
options.compress.uglifyjs.warnings = grunt.option('verbose') === true;

// Set default "comments" option if not given already
options.compress.uglifyjs.output = options.compress.uglifyjs.output || {};
if (!options.compress.uglifyjs.output.hasOwnProperty("comments")) {
options.compress.uglifyjs.output.comments = copyrightCommentsPattern;
}

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;
fileContent = uglify.minify(fileContent, options.compress.uglifyjs).code;
break;
case '.json':
// JSON is parsed and written to string again to remove unwanted white space
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version": "2.0",
"name": "my.ui.lib.library-preload",
"modules": {
"my/ui/lib/library.js": "jQuery.sap.require(\"sap.ui.core.library\"),jQuery.sap.declare(\"my.ui.lib.library\"),sap.ui.getCore().initLibrary({name:\"my.ui.lib\",version:\"0.0.0\",dependencies:[\"sap.ui.core\"]});",
"my/ui/lib/myJS.js": "/* © */\n\"use strict\";function myFunction(longVariableName,longerVariableName){var sample=\"\\u518d\\u6309\\u4e00\\u6b21\\u9000\\u51fa\\u4f19\\u62fc\";return longVariableName+longerVariableName+sample}/**\n* This is a copyright comment\n*/\n/* (c) */\n/* released under */\n/* license */\nconsole.log(\"myJS\");",
"my/ui/lib/my.view.xml": "<my>XML</my>\n",
"my/ui/lib/myHtmlPre.view.xml": "<my>XML</my>\n<!-- A comment in XML is the same as in HTML -->\n<html:pre> </html:pre>",
"my/ui/lib/foo.properties": "FOO=BAR\n"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FOO=BAR
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
jQuery.sap.require("sap.ui.core.library");
jQuery.sap.declare("my.ui.lib.library");

sap.ui.getCore().initLibrary({
name : "my.ui.lib",
version: "0.0.0",
dependencies : ["sap.ui.core"]
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<my>XML</my>
<!-- A comment in XML is the same as in HTML -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<my>XML</my>
<!-- A comment in XML is the same as in HTML -->
<html:pre> </html:pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* © */
'use strict';

/**
* This is a copyright comment
*/

/* (c) */

/* released under */
/* normal comment */
/* license */


console.log('myJS');

/**
* This is a little comment
*/
function myFunction(longVariableName, longerVariableName) {
var sample = '\u518d\u6309\u4e00\u6b21\u9000\u51fa\u4f19\u62fc';
return longVariableName + longerVariableName + sample;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"my": "json"
}
8 changes: 8 additions & 0 deletions test/preload_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ describe('openui5_preload', function() {
});
});

it('custom_uglify_params', function() {
fileContent.equal({
sActualFileSource: 'tmp/preload/library_custom_uglify_params/my/ui/lib/library-preload.json',
sExpectedFileSource: 'test/preload/expected/library_custom_uglify_params/my/ui/lib/library-preload.json',
sMessage: 'library preload JSON should be correctly created.'
});
})

it('library_same_dest', function() {
fileContent.equal({
sActualFileSource: 'test/preload/fixtures/library-same-dest/my/ui/lib/library-preload.json',
Expand Down

0 comments on commit b2e6c0e

Please sign in to comment.