Skip to content

Commit

Permalink
[FEATURE] Tag bundles and ignore them in uglify task
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomByte committed Oct 22, 2020
1 parent 750b43e commit b4c16a7
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 14 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
12 changes: 9 additions & 3 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,8 +32,11 @@ module.exports = function({workspace, dependencies, options: {projectName, bundl
},
resources
}).then((bundles) => {
bundles.forEach((bundle) => {
workspace.write(bundle);
return bundles.map((bundle) => {
if (taskUtil) {
taskUtil.setTag(bundle, taskUtil.STANDARD_TAGS.IsBundle);
}
return workspace.write(bundle);
});
});
});
Expand Down
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
12 changes: 10 additions & 2 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 @@ -346,7 +347,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
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 b4c16a7

Please sign in to comment.