Skip to content

Commit 80d7060

Browse files
[ui5-builder][FEATURE] Bundle 'require' section with async flag for specVersion: 4.0 (#1042)
JIRA: CPOUI5FOUNDATION-835 This change completes SAP/ui5-builder#1033 with check for specVersion: 4.0 and enforcing default async flag for require section in 'bundleDefinitions' --------- Co-authored-by: Merlin Beutlberger <m.beutlberger@sap.com>
1 parent 7366fe3 commit 80d7060

File tree

10 files changed

+465
-169
lines changed

10 files changed

+465
-169
lines changed

packages/builder/lib/processors/bundlers/moduleBundler.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ const log = getLogger("builder:processors:bundlers:moduleBundler");
135135
* @param {string} [parameters.options.targetUi5CoreVersion] Optional semver compliant sap.ui.core project version, e.g '2.0.0'.
136136
This allows the bundler to make assumptions on available runtime APIs.
137137
Omit if the ultimate UI5 version at runtime is unknown or can't be determined.
138+
* @param {@ui5/project/build/helpers/TaskUtil|object} [parameters.taskUtil] TaskUtil
138139
* @returns {Promise<module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundlerResult[]>}
139140
* Promise resolving with module bundle resources
140141
*/
@@ -152,14 +153,7 @@ export default function({resources, options: {
152153
ignoreMissingModules: false
153154
}, bundleOptions);
154155

155-
// Apply defaults without modifying the passed object
156-
bundleDefinition = Object.assign({
157-
resolve: false,
158-
resolveConditional: false,
159-
renderer: false,
160-
sort: true,
161-
declareRawModules: false,
162-
}, bundleDefinition);
156+
// bundleDefinition's defaults get applied in the corresponding standard tasks
163157

164158
const pool = new LocatorResourcePool({
165159
ignoreMissingModules: bundleOptions.ignoreMissingModules

packages/builder/lib/tasks/bundlers/generateBundle.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
2+
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
23
import createModuleNameMapping from "./utils/createModuleNameMapping.js";
34
import ReaderCollectionPrioritized from "@ui5/fs/ReaderCollectionPrioritized";
45

@@ -96,7 +97,9 @@ export default async function({
9697
}
9798
const coreVersion = taskUtil?.getProject("sap.ui.core")?.getVersion();
9899
return combo.byGlob("/resources/**/*.{js,json,xml,html,properties,library,js.map}").then((resources) => {
99-
const options = {bundleDefinition, bundleOptions};
100+
const options = {
101+
bundleDefinition: applyDefaultsToBundleDefinition(bundleDefinition, taskUtil), bundleOptions
102+
};
100103
if (!optimize && taskUtil) {
101104
options.moduleNameMapping = createModuleNameMapping({resources, taskUtil});
102105
}

packages/builder/lib/tasks/bundlers/generateComponentPreload.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from "node:path";
22
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
3+
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
34
import {getLogger} from "@ui5/logger";
45
const log = getLogger("builder:tasks:bundlers:generateComponentPreload");
56
import {negateFilters} from "../../lbt/resources/ResourceFilterList.js";
@@ -148,7 +149,7 @@ export default async function({
148149
return Promise.all(bundleDefinitions.filter(Boolean).map((bundleDefinition) => {
149150
log.verbose(`Generating ${bundleDefinition.name}...`);
150151
const options = {
151-
bundleDefinition,
152+
bundleDefinition: applyDefaultsToBundleDefinition(bundleDefinition, taskUtil),
152153
bundleOptions: {
153154
ignoreMissingModules: true,
154155
optimize: true

packages/builder/lib/tasks/bundlers/generateLibraryPreload.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {getLogger} from "@ui5/logger";
22
const log = getLogger("builder:tasks:bundlers:generateLibraryPreload");
33
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
4+
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
45
import {negateFilters} from "../../lbt/resources/ResourceFilterList.js";
56
import createModuleNameMapping from "./utils/createModuleNameMapping.js";
67

@@ -265,6 +266,7 @@ export default async function({workspace, taskUtil, options: {skipBundles = [],
265266
if (coreVersion) {
266267
options.targetUi5CoreVersion = coreVersion;
267268
}
269+
options.bundleDefinition = applyDefaultsToBundleDefinition(options.bundleDefinition, taskUtil);
268270
return moduleBundler({options, resources});
269271
};
270272

packages/builder/lib/tasks/bundlers/generateStandaloneAppBundle.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {getLogger} from "@ui5/logger";
22
const log = getLogger("builder:tasks:bundlers:generateStandaloneAppBundle");
33
import ReaderCollectionPrioritized from "@ui5/fs/ReaderCollectionPrioritized";
44
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
5+
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
56
import createModuleNameMapping from "./utils/createModuleNameMapping.js";
67

78
function getBundleDefinition(config) {
@@ -139,24 +140,30 @@ export default async function({workspace, dependencies, taskUtil, options}) {
139140
}
140141

141142
const bundleOptions = {
142-
bundleDefinition: getBundleDefinition({
143-
name: "sap-ui-custom.js",
144-
filters,
145-
namespace,
146-
preloadSection: true
147-
})
143+
bundleDefinition: applyDefaultsToBundleDefinition(
144+
getBundleDefinition({
145+
name: "sap-ui-custom.js",
146+
filters,
147+
namespace,
148+
preloadSection: true,
149+
}),
150+
taskUtil
151+
),
148152
};
149153

150154
const bundleDbgOptions = {
151-
bundleDefinition: getBundleDefinition({
152-
name: "sap-ui-custom-dbg.js",
153-
filters,
154-
namespace
155-
}),
155+
bundleDefinition: applyDefaultsToBundleDefinition(
156+
getBundleDefinition({
157+
name: "sap-ui-custom-dbg.js",
158+
filters,
159+
namespace,
160+
}),
161+
taskUtil
162+
),
156163
bundleOptions: {
157-
optimize: false
164+
optimize: false,
158165
},
159-
moduleNameMapping: unoptimizedModuleNameMapping
166+
moduleNameMapping: unoptimizedModuleNameMapping,
160167
};
161168

162169
if (coreVersion) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Applies default values to bundleDefinitions
3+
*
4+
* @param {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleDefinition} bundleDefinition Module
5+
* bundle definition
6+
* @param {@ui5/project/build/helpers/TaskUtil|object} [taskUtil] TaskUtil
7+
*
8+
* @returns {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleDefinition}
9+
*/
10+
export function applyDefaultsToBundleDefinition(bundleDefinition, taskUtil) {
11+
bundleDefinition.sections = bundleDefinition?.sections?.map((section) => {
12+
const defaultValues = {
13+
resolve: false,
14+
resolveConditional: false,
15+
renderer: false,
16+
sort: true,
17+
declareRawModules: false,
18+
};
19+
20+
// Since specVersion: 4.0 "require" section starts loading asynchronously.
21+
// If specVersion cannot be determined, the latest spec is taken into account.
22+
// This is a breaking change in specVersion: 4.0
23+
if (section.mode === "require" && (!taskUtil || taskUtil.getProject().getSpecVersion().gte("4.0"))) {
24+
defaultValues.async = true;
25+
}
26+
27+
return Object.assign(defaultValues, section);
28+
});
29+
30+
return bundleDefinition;
31+
}

packages/builder/test/lib/processors/bundlers/moduleBundler.js

Lines changed: 7 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,6 @@ test.serial("Builder returns single bundle", async (t) => {
4545
const bundleOptions = {
4646
"some": "option"
4747
};
48-
const effectiveBundleDefinition = {
49-
// Defaults
50-
"resolve": false,
51-
"resolveConditional": false,
52-
"renderer": false,
53-
"sort": true,
54-
"declareRawModules": false,
55-
56-
"some": "definition"
57-
};
5848

5949
const createdBundle = {
6050
name: "BundleName.js",
@@ -103,7 +93,7 @@ test.serial("Builder returns single bundle", async (t) => {
10393

10494
t.is(builder.createBundle.callCount, 1, "builder.createBundle should be called once");
10595
t.is(builder.createBundle.getCall(0).args.length, 2);
106-
t.deepEqual(builder.createBundle.getCall(0).args[0], effectiveBundleDefinition,
96+
t.deepEqual(builder.createBundle.getCall(0).args[0], bundleDefinition,
10797
"builder.createBundle should be called with bundleDefinition");
10898
t.deepEqual(builder.createBundle.getCall(0).args[1], {
10999
// default bundleOptions
@@ -143,17 +133,6 @@ test.serial("Builder returns multiple bundles", async (t) => {
143133
"some": "option"
144134
};
145135

146-
const effectiveBundleDefinition = {
147-
// Defaults
148-
"resolve": false,
149-
"resolveConditional": false,
150-
"renderer": false,
151-
"sort": true,
152-
"declareRawModules": false,
153-
154-
"some": "definition"
155-
};
156-
157136
const createdBundles = [
158137
{
159138
name: "BundleName1.js",
@@ -226,7 +205,7 @@ test.serial("Builder returns multiple bundles", async (t) => {
226205

227206
t.is(builder.createBundle.callCount, 1, "builder.createBundle should be called once");
228207
t.is(builder.createBundle.getCall(0).args.length, 2);
229-
t.deepEqual(builder.createBundle.getCall(0).args[0], effectiveBundleDefinition,
208+
t.deepEqual(builder.createBundle.getCall(0).args[0], bundleDefinition,
230209
"builder.createBundle should be called with bundleDefinition");
231210
t.deepEqual(builder.createBundle.getCall(0).args[1], {
232211
// default bundleOptions
@@ -268,16 +247,6 @@ test.serial("bundleOptions default (no options passed)", async (t) => {
268247
const bundleDefinition = {
269248
"some": "definition"
270249
};
271-
const effectiveBundleDefinition = {
272-
// Defaults
273-
"resolve": false,
274-
"resolveConditional": false,
275-
"renderer": false,
276-
"sort": true,
277-
"declareRawModules": false,
278-
279-
"some": "definition"
280-
};
281250

282251
const createdBundle = {
283252
name: "BundleName.js",
@@ -325,7 +294,7 @@ test.serial("bundleOptions default (no options passed)", async (t) => {
325294

326295
t.is(builder.createBundle.callCount, 1, "builder.createBundle should be called once");
327296
t.is(builder.createBundle.getCall(0).args.length, 2);
328-
t.deepEqual(builder.createBundle.getCall(0).args[0], effectiveBundleDefinition,
297+
t.deepEqual(builder.createBundle.getCall(0).args[0], bundleDefinition,
329298
"builder.createBundle should be called with bundleDefinition");
330299
t.deepEqual(builder.createBundle.getCall(0).args[1], {
331300
// default bundleOptions
@@ -361,17 +330,6 @@ test.serial("bundleOptions default (empty options passed)", async (t) => {
361330
};
362331
const bundleOptions = {};
363332

364-
const effectiveBundleDefinition = {
365-
// Defaults
366-
"resolve": false,
367-
"resolveConditional": false,
368-
"renderer": false,
369-
"sort": true,
370-
"declareRawModules": false,
371-
372-
"some": "definition"
373-
};
374-
375333
const createdBundle = {
376334
name: "BundleName.js",
377335
content: "Bundle Content",
@@ -400,7 +358,7 @@ test.serial("bundleOptions default (empty options passed)", async (t) => {
400358

401359
t.is(builder.createBundle.callCount, 1, "builder.createBundle should be called once");
402360
t.is(builder.createBundle.getCall(0).args.length, 2);
403-
t.deepEqual(builder.createBundle.getCall(0).args[0], effectiveBundleDefinition,
361+
t.deepEqual(builder.createBundle.getCall(0).args[0], bundleDefinition,
404362
"builder.createBundle should be called with bundleDefinition");
405363
t.deepEqual(builder.createBundle.getCall(0).args[1], {
406364
// default bundleOptions
@@ -434,17 +392,6 @@ test.serial("bundleOptions (all options passed)", async (t) => {
434392
ignoreMissingModules: true
435393
};
436394

437-
const effectiveBundleDefinition = {
438-
// Defaults
439-
"resolve": false,
440-
"resolveConditional": false,
441-
"renderer": false,
442-
"sort": true,
443-
"declareRawModules": false,
444-
445-
"some": "definition"
446-
};
447-
448395
const createdBundle = {
449396
name: "BundleName.js",
450397
content: "Bundle Content",
@@ -473,7 +420,7 @@ test.serial("bundleOptions (all options passed)", async (t) => {
473420

474421
t.is(builder.createBundle.callCount, 1, "builder.createBundle should be called once");
475422
t.is(builder.createBundle.getCall(0).args.length, 2);
476-
t.deepEqual(builder.createBundle.getCall(0).args[0], effectiveBundleDefinition,
423+
t.deepEqual(builder.createBundle.getCall(0).args[0], bundleDefinition,
477424
"builder.createBundle should be called with bundleDefinition");
478425
t.deepEqual(builder.createBundle.getCall(0).args[1], bundleOptions,
479426
"builder.createBundle should be called with bundleOptions");
@@ -502,16 +449,6 @@ test.serial("Passes ignoreMissingModules bundleOption to LocatorResourcePool", a
502449

503450
"ignoreMissingModules": "foo"
504451
};
505-
const effectiveBundleDefinition = {
506-
// Defaults
507-
"resolve": false,
508-
"resolveConditional": false,
509-
"renderer": false,
510-
"sort": true,
511-
"declareRawModules": false,
512-
513-
"some": "definition"
514-
};
515452

516453
const createdBundle = {
517454
name: "BundleName.js",
@@ -560,7 +497,7 @@ test.serial("Passes ignoreMissingModules bundleOption to LocatorResourcePool", a
560497

561498
t.is(builder.createBundle.callCount, 1, "builder.createBundle should be called once");
562499
t.is(builder.createBundle.getCall(0).args.length, 2);
563-
t.deepEqual(builder.createBundle.getCall(0).args[0], effectiveBundleDefinition,
500+
t.deepEqual(builder.createBundle.getCall(0).args[0], bundleDefinition,
564501
"builder.createBundle should be called with bundleDefinition");
565502
t.deepEqual(builder.createBundle.getCall(0).args[1], effectiveBundleOptions,
566503
"builder.createBundle should be called with bundleOptions");
@@ -602,17 +539,6 @@ test.serial("Verbose Logging", async (t) => {
602539
"some": "option",
603540
};
604541

605-
const effectiveBundleDefinition = {
606-
// Defaults
607-
"resolve": false,
608-
"resolveConditional": false,
609-
"renderer": false,
610-
"sort": true,
611-
"declareRawModules": false,
612-
613-
"some": "definition"
614-
};
615-
616542
const createdBundle = {
617543
name: "Bundle Name",
618544
content: "Bundle Content",
@@ -645,6 +571,6 @@ test.serial("Verbose Logging", async (t) => {
645571

646572
t.deepEqual(log.verbose.getCall(0).args, ["Generating bundle:"]);
647573
t.deepEqual(log.verbose.getCall(1).args,
648-
["bundleDefinition: " + JSON.stringify(effectiveBundleDefinition, null, 2)]);
574+
["bundleDefinition: " + JSON.stringify(bundleDefinition, null, 2)]);
649575
t.deepEqual(log.verbose.getCall(2).args, ["bundleOptions: " + JSON.stringify(effectiveBundleOptions, null, 2)]);
650576
});

0 commit comments

Comments
 (0)