Skip to content

Commit

Permalink
[FEATURE] Support build minification excludes (#653)
Browse files Browse the repository at this point in the history
Supports the build configuration "minification excludes"
introduced in specVersion 2.6. This allows to exclude
JavaScript resources from minification (tasks: uglify and
createDebugFiles).

JIRA: CPOUI5FOUNDATION-405
  • Loading branch information
larskissel authored Oct 18, 2021
1 parent b4d7763 commit 0aa2301
Show file tree
Hide file tree
Showing 41 changed files with 390 additions and 4 deletions.
23 changes: 23 additions & 0 deletions lib/types/AbstractBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,29 @@ class AbstractBuilder {
return taskFunction().then(() => this.taskLog.completeWork(1));
};
}

/**
* Appends the list of 'excludes' to the list of 'patterns'. To harmonize both lists, the 'excludes'
* are negated and the 'patternPrefix' is added to make them absolute.
*
* @private
* @param {string[]} patterns
* List of absolute default patterns.
* @param {string[]} excludes
* List of relative patterns to be excluded. Excludes with a leading "!" are meant to be re-included.
* @param {string} patternPrefix
* Prefix to be added to the excludes to make them absolute. The prefix must have a leading and a
* trailing "/".
*/
enhancePatternWithExcludes(patterns, excludes, patternPrefix) {
excludes.forEach((exclude) => {
if (exclude.startsWith("!")) {
patterns.push(`${patternPrefix}${exclude.slice(1)}`);
} else {
patterns.push(`!${patternPrefix}${exclude}`);
}
});
}
}

module.exports = AbstractBuilder;
12 changes: 10 additions & 2 deletions lib/types/application/ApplicationBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,20 @@ class ApplicationBuilder extends AbstractBuilder {
});
}

const minificationPattern = ["/**/*.js"];
if (["2.6"].includes(project.specVersion)) {
const minificationExcludes = project.builder && project.builder.minification &&
project.builder.minification.excludes;
if (minificationExcludes) {
this.enhancePatternWithExcludes(minificationPattern, minificationExcludes, "/");
}
}
this.addTask("createDebugFiles", async () => {
const createDebugFiles = getTask("createDebugFiles").task;
return createDebugFiles({
workspace: resourceCollections.workspace,
options: {
pattern: "/**/*.js"
pattern: minificationPattern
}
});
});
Expand All @@ -154,7 +162,7 @@ class ApplicationBuilder extends AbstractBuilder {
workspace: resourceCollections.workspace,
taskUtil,
options: {
pattern: "/**/*.js"
pattern: minificationPattern
}
});
});
Expand Down
12 changes: 10 additions & 2 deletions lib/types/library/LibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,19 @@ class LibraryBuilder extends AbstractBuilder {
});
});

const minificationPattern = ["/resources/**/*.js"];
if (["2.6"].includes(project.specVersion)) {
const minificationExcludes = project.builder && project.builder.minification &&
project.builder.minification.excludes;
if (minificationExcludes) {
this.enhancePatternWithExcludes(minificationPattern, minificationExcludes, "/resources/");
}
}
this.addTask("createDebugFiles", async () => {
return getTask("createDebugFiles").task({
workspace: resourceCollections.workspace,
options: {
pattern: "/resources/**/*.js"
pattern: minificationPattern
}
});
});
Expand All @@ -209,7 +217,7 @@ class LibraryBuilder extends AbstractBuilder {
workspace: resourceCollections.workspace,
taskUtil,
options: {
pattern: "/resources/**/*.js"
pattern: minificationPattern
}
});
});
Expand Down
11 changes: 11 additions & 0 deletions test/expected/build/application.l/dest/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Application L</title>
<script id="sap-ui-bootstrap" src="test.js">
</script>
</head>
<body>

</body>
</html>
5 changes: 5 additions & 0 deletions test/expected/build/application.l/dest/subdir/index-dbg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
1 change: 1 addition & 0 deletions test/expected/build/application.l/dest/subdir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function hello(l){console.log("hello "+l)}hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function hello(l){console.log("hello "+l)}hello("world");
7 changes: 7 additions & 0 deletions test/expected/build/application.l/dest/test-dbg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sap.ui.define([], function() {
function test(paramA) {
var variableA = paramA;
console.log(variableA);
}
test();
});
1 change: 1 addition & 0 deletions test/expected/build/application.l/dest/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sap.ui.define([],function(){function n(n){var o=n;console.log(o)}n()});
5 changes: 5 additions & 0 deletions test/expected/build/application.l/dest/thirdparty/File0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
5 changes: 5 additions & 0 deletions test/expected/build/application.l/dest/thirdparty/File1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
11 changes: 11 additions & 0 deletions test/expected/build/library.l/dest/resources/library/l/.library
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<library xmlns="http://www.sap.com/sap.ui.library.xsd" >

<name>library.l</name>
<vendor>SAP SE</vendor>
<copyright>Some fancy copyright</copyright>
<version>1.0.0</version>

<documentation>Library L</documentation>

</library>
16 changes: 16 additions & 0 deletions test/expected/build/library.l/dest/resources/library/l/some-dbg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*!
* Some fancy copyright
*/

sap.ui.define([],
function() {
"use strict";

/**
* @alias library.l
* @namespace
* @public
*/
var SomeFunction = function() {};

}, /* bExport= */ true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/*!
* Some fancy copyright
*/
sap.ui.define([],function(){"use strict";var i=function(){}},true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function hello(l){console.log("hello "+l)}hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function hello(l){console.log("hello "+l)}hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
10 changes: 10 additions & 0 deletions test/fixtures/application.l/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "application.l",
"version": "1.0.0",
"description": "Simple SAPUI5 based application",
"main": "index.html",
"dependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
11 changes: 11 additions & 0 deletions test/fixtures/application.l/webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Application L</title>
<script id="sap-ui-bootstrap" src="test.js">
</script>
</head>
<body>

</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/application.l/webapp/subdir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
5 changes: 5 additions & 0 deletions test/fixtures/application.l/webapp/subdir/thirdparty/File0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
5 changes: 5 additions & 0 deletions test/fixtures/application.l/webapp/subdir/thirdparty/File1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
7 changes: 7 additions & 0 deletions test/fixtures/application.l/webapp/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sap.ui.define([], function() {
function test(paramA) {
var variableA = paramA;
console.log(variableA);
}
test();
});
5 changes: 5 additions & 0 deletions test/fixtures/application.l/webapp/thirdparty/File0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
5 changes: 5 additions & 0 deletions test/fixtures/application.l/webapp/thirdparty/File1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
11 changes: 11 additions & 0 deletions test/fixtures/library.l/main/src/library/l/.library
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" ?>
<library xmlns="http://www.sap.com/sap.ui.library.xsd" >

<name>library.l</name>
<vendor>SAP SE</vendor>
<copyright>${copyright}</copyright>
<version>${version}</version>

<documentation>Library L</documentation>

</library>
16 changes: 16 additions & 0 deletions test/fixtures/library.l/main/src/library/l/some.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*!
* ${copyright}
*/

sap.ui.define([],
function() {
"use strict";

/**
* @alias library.l
* @namespace
* @public
*/
var SomeFunction = function() {};

}, /* bExport= */ true);
5 changes: 5 additions & 0 deletions test/fixtures/library.l/main/src/library/l/subdir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello world
function hello(name) {
console.log("hello " + name);
}
hello("world");
9 changes: 9 additions & 0 deletions test/fixtures/library.l/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "library.l",
"version": "1.0.0",
"description": "Simple SAPUI5 based library for testing minification excludes",
"dependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
}
}
Loading

0 comments on commit 0aa2301

Please sign in to comment.