Skip to content

Commit

Permalink
[FEATURE] Tag bundles and ignore them in uglify task (#535)
Browse files Browse the repository at this point in the history
Bundles already take care of minification through their bundle configuration.
We should always ignore them in the dedicated uglify task.

Co-authored-by: Matthias Osswald <mat.osswald@sap.com>
  • Loading branch information
RandomByte and matz3 authored Nov 2, 2020
1 parent be29520 commit b487366
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/builder/ProjectBuildContext.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const ResourceTagCollection = require("@ui5/fs").ResourceTagCollection;

const STANDARD_TAGS = Object.freeze({
OmitFromBuildResult: "ui5:OmitFromBuildResult"
OmitFromBuildResult: "ui5:OmitFromBuildResult",
IsBundle: "ui5:IsBundle"
});

/**
Expand Down
14 changes: 10 additions & 4 deletions lib/tasks/bundlers/generateBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritiz
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/fs.ReaderCollection} parameters.dependencies Collection to read dependency files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @param {string} parameters.options.projectName Project name
* @param {ModuleBundleDefinition} parameters.options.bundleDefinition Module bundle definition
* @param {ModuleBundleOptions} parameters.options.bundleOptions Module bundle options
* @returns {Promise} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = function({workspace, dependencies, options: {projectName, bundleDefinition, bundleOptions}}) {
module.exports = function({
workspace, dependencies, taskUtil, options: {projectName, bundleDefinition, bundleOptions}
}) {
const combo = new ReaderCollectionPrioritized({
name: `libraryBundler - prioritize workspace over dependencies: ${projectName}`,
readers: [workspace, dependencies]
Expand All @@ -29,9 +32,12 @@ module.exports = function({workspace, dependencies, options: {projectName, bundl
},
resources
}).then((bundles) => {
bundles.forEach((bundle) => {
workspace.write(bundle);
});
return Promise.all(bundles.map((bundle) => {
if (taskUtil) {
taskUtil.setTag(bundle, taskUtil.STANDARD_TAGS.IsBundle);
}
return workspace.write(bundle);
}));
});
});
};
6 changes: 5 additions & 1 deletion lib/tasks/bundlers/generateComponentPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritiz
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @param {string} parameters.options.projectName Project name
* @param {Array} [parameters.options.paths] Array of paths (or glob patterns) for component files
* @param {Array} [parameters.options.namespaces] Array of component namespaces
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = function({workspace, dependencies, options: {projectName, paths, namespaces}}) {
module.exports = function({workspace, dependencies, taskUtil, options: {projectName, paths, namespaces}}) {
const combo = new ReaderCollectionPrioritized({
name: `generateComponentPreload - prioritize workspace over dependencies: ${projectName}`,
readers: [workspace, dependencies]
Expand Down Expand Up @@ -91,6 +92,9 @@ module.exports = function({workspace, dependencies, options: {projectName, paths
})
.then((processedResources) => {
return Promise.all(processedResources.map((resource) => {
if (taskUtil) {
taskUtil.setTag(resource[0], taskUtil.STANDARD_TAGS.IsBundle);
}
return workspace.write(resource[0]);
}));
});
Expand Down
19 changes: 16 additions & 3 deletions lib/tasks/bundlers/generateLibraryPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,12 @@ function getSapUiCoreBunDef(name, filters, preload) {
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @param {string} parameters.options.projectName Project name
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = function({workspace, dependencies, options: {projectName}}) {
module.exports = function({workspace, dependencies, taskUtil, options: {projectName}}) {
const combo = new ReaderCollectionPrioritized({
name: `libraryBundler - prioritize workspace over dependencies: ${projectName}`,
readers: [workspace, dependencies]
Expand Down Expand Up @@ -311,7 +312,12 @@ module.exports = function({workspace, dependencies, options: {projectName}}) {
}),
]).then((results) => {
const bundles = Array.prototype.concat.apply([], results);
return Promise.all(bundles.map((bundle) => workspace.write(bundle)));
return Promise.all(bundles.map((bundle) => {
if (taskUtil) {
taskUtil.setTag(bundle, taskUtil.STANDARD_TAGS.IsBundle);
}
return workspace.write(bundle);
}));
});
} else {
p = Promise.resolve();
Expand Down Expand Up @@ -346,7 +352,14 @@ module.exports = function({workspace, dependencies, options: {projectName}}) {
return createLibraryBundles(libraryNamespace, resources)
.then((results) => {
const bundles = Array.prototype.concat.apply([], results);
return Promise.all(bundles.map((bundle) => bundle && workspace.write(bundle)));
return Promise.all(bundles.map((bundle) => {
if (bundle) {
if (taskUtil) {
taskUtil.setTag(bundle, taskUtil.STANDARD_TAGS.IsBundle);
}
return workspace.write(bundle);
}
}));
});
} else {
log.verbose(`Could not determine library namespace from file "${libraryIndicatorPath}" for project ${projectName}. Skipping library preload bundling.`);
Expand Down
10 changes: 8 additions & 2 deletions lib/tasks/bundlers/generateStandaloneAppBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ function getBundleDefinition(config) {
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/fs.AbstractReader} parameters.dependencies Reader or Collection to read dependency files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @param {string} parameters.options.projectName Project name
* @param {string} [parameters.options.namespace] Project namespace
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = async function({workspace, dependencies, options: {projectName, namespace}}) {
module.exports = async function({workspace, dependencies, taskUtil, options: {projectName, namespace}}) {
if (!namespace) {
log.warn(`Namespace of project ${projectName} is not known. Self contained bundling is currently ` +
`unable to generate complete bundles for such projects.`);
Expand Down Expand Up @@ -108,6 +109,11 @@ module.exports = async function({workspace, dependencies, options: {projectName,
})
]).then((results) => {
const bundles = Array.prototype.concat.apply([], results);
return Promise.all(bundles.map((resource) => workspace.write(resource)));
return Promise.all(bundles.map((resource) => {
if (taskUtil) {
taskUtil.setTag(resource, taskUtil.STANDARD_TAGS.IsBundle);
}
return workspace.write(resource);
}));
});
};
11 changes: 9 additions & 2 deletions lib/tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,22 @@ const uglifyProcessor = require("../processors/uglifier");
* @alias module:@ui5/builder.tasks.uglify
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @param {string} parameters.options.pattern Pattern to locate the files to be processed
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = function({workspace, options: {pattern}}) {
module.exports = function({workspace, taskUtil, options: {pattern}}) {
return workspace.byGlobSource(pattern)
.then((allResources) => {
let resources = allResources;
if (taskUtil) {
resources = allResources.filter((resource) => {
return !taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.IsBundle);
});
}
return uglifyProcessor({
resources: allResources
resources
});
})
.then((processedResources) => {
Expand Down
5 changes: 5 additions & 0 deletions lib/types/application/ApplicationBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class ApplicationBuilder extends AbstractBuilder {
return getTask("generateComponentPreload").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
paths: componentPreload.paths,
Expand All @@ -86,6 +87,7 @@ class ApplicationBuilder extends AbstractBuilder {
return getTask("generateComponentPreload").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
namespaces: [project.metadata.namespace]
Expand All @@ -98,6 +100,7 @@ class ApplicationBuilder extends AbstractBuilder {
return getTask("generateStandaloneAppBundle").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
namespace: project.metadata.namespace
Expand All @@ -122,6 +125,7 @@ class ApplicationBuilder extends AbstractBuilder {
return getTask("generateBundle").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
bundleDefinition: bundle.bundleDefinition,
Expand All @@ -146,6 +150,7 @@ class ApplicationBuilder extends AbstractBuilder {
const uglify = getTask("uglify").task;
return uglify({
workspace: resourceCollections.workspace,
taskUtil,
options: {
pattern: "/**/*.js"
}
Expand Down
7 changes: 6 additions & 1 deletion lib/types/library/LibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ class LibraryBuilder extends AbstractBuilder {
}

return getTask("generateJsdoc").task({
taskUtil,
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
namespace: project.metadata.namespace,
Expand Down Expand Up @@ -86,6 +86,7 @@ class LibraryBuilder extends AbstractBuilder {
return getTask("generateComponentPreload").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
paths: componentPreload.paths,
Expand All @@ -99,6 +100,7 @@ class LibraryBuilder extends AbstractBuilder {
return getTask("generateLibraryManifest").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name
}
Expand All @@ -122,6 +124,7 @@ class LibraryBuilder extends AbstractBuilder {
return getTask("generateLibraryPreload").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name
}
Expand All @@ -136,6 +139,7 @@ class LibraryBuilder extends AbstractBuilder {
return getTask("generateBundle").task({
workspace: resourceCollections.workspace,
dependencies: resourceCollections.dependencies,
taskUtil,
options: {
projectName: project.metadata.name,
bundleDefinition: bundle.bundleDefinition,
Expand Down Expand Up @@ -172,6 +176,7 @@ class LibraryBuilder extends AbstractBuilder {
this.addTask("uglify", async () => {
return getTask("uglify").task({
workspace: resourceCollections.workspace,
taskUtil,
options: {
pattern: "/resources/**/*.js"
}
Expand Down
10 changes: 7 additions & 3 deletions test/lib/builder/ProjectBuildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,19 @@ test("STANDARD_TAGS constant", (t) => {
});

t.deepEqual(projectBuildContext.STANDARD_TAGS, {
OmitFromBuildResult: "ui5:OmitFromBuildResult"
OmitFromBuildResult: "ui5:OmitFromBuildResult",
IsBundle: "ui5:IsBundle"
}, "Exposes correct STANDARD_TAGS constant");
});

test.serial("getResourceTagCollection", (t) => {
class DummyResourceTagCollection {
constructor({allowedTags}) {
t.deepEqual(allowedTags, ["ui5:OmitFromBuildResult"],
"Correct allowedTags parameter supplied");
t.deepEqual(allowedTags, [
"ui5:OmitFromBuildResult",
"ui5:IsBundle"
],
"Correct allowedTags parameter supplied");
}
}
mock("@ui5/fs", {
Expand Down

0 comments on commit b487366

Please sign in to comment.