Skip to content

Commit

Permalink
Move applyDefaults to external util & adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Jul 2, 2024
1 parent bb33e24 commit 31c2bca
Show file tree
Hide file tree
Showing 10 changed files with 511 additions and 211 deletions.
26 changes: 1 addition & 25 deletions lib/processors/bundlers/moduleBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ export default function({resources, taskUtil, options: {
ignoreMissingModules: false
}, bundleOptions);

// Apply defaults without modifying the passed object
bundleDefinition = applyDefaultsToBundleDefinition(bundleDefinition, taskUtil);
// bundleDefinition's defaults get applied in the corresponding standard tasks

const pool = new LocatorResourcePool({
ignoreMissingModules: bundleOptions.ignoreMissingModules
Expand Down Expand Up @@ -196,26 +195,3 @@ export default function({resources, taskUtil, options: {
}));
});
}

function applyDefaultsToBundleDefinition(bundleDefinition, taskUtil) {
bundleDefinition.sections = bundleDefinition?.sections?.map((section) => {
const defaultValues = {
resolve: false,
resolveConditional: false,
renderer: false,
sort: true,
declareRawModules: false,
};

// Since specVersion: 4.0 "require" section starts loading asynchronously.
// If specVersion cannot be determined, the latest spec is taken into account.
// This is a breaking change in specVersion: 4.0
if (section.mode === "require" && (!taskUtil || taskUtil.getProject().getSpecVersion().gte("4.0"))) {
defaultValues.async = true;
}

return Object.assign(defaultValues, section);
});

return bundleDefinition;
}
7 changes: 5 additions & 2 deletions lib/tasks/bundlers/generateBundle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
import createModuleNameMapping from "./utils/createModuleNameMapping.js";
import ReaderCollectionPrioritized from "@ui5/fs/ReaderCollectionPrioritized";

Expand Down Expand Up @@ -96,14 +97,16 @@ export default async function({
}
const coreVersion = taskUtil?.getProject("sap.ui.core")?.getVersion();
return combo.byGlob("/resources/**/*.{js,json,xml,html,properties,library,js.map}").then((resources) => {
const options = {bundleDefinition, bundleOptions};
const options = {
bundleDefinition: applyDefaultsToBundleDefinition(bundleDefinition, taskUtil), bundleOptions
};
if (!optimize && taskUtil) {
options.moduleNameMapping = createModuleNameMapping({resources, taskUtil});
}
if (coreVersion) {
options.targetUi5CoreVersion = coreVersion;
}
return moduleBundler({options, resources, taskUtil}).then((bundles) => {
return moduleBundler({options, resources}).then((bundles) => {
return Promise.all(bundles.map(({bundle, sourceMap} = {}) => {
if (!bundle) {
// Skip empty bundles
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/bundlers/generateComponentPreload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "node:path";
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
import {getLogger} from "@ui5/logger";
const log = getLogger("builder:tasks:bundlers:generateComponentPreload");
import {negateFilters} from "../../lbt/resources/ResourceFilterList.js";
Expand Down Expand Up @@ -148,7 +149,7 @@ export default async function({
return Promise.all(bundleDefinitions.filter(Boolean).map((bundleDefinition) => {
log.verbose(`Generating ${bundleDefinition.name}...`);
const options = {
bundleDefinition,
bundleDefinition: applyDefaultsToBundleDefinition(bundleDefinition, taskUtil),
bundleOptions: {
ignoreMissingModules: true,
optimize: true
Expand All @@ -159,8 +160,7 @@ export default async function({
}
return moduleBundler({
resources,
options,
taskUtil
options
});
}));
})
Expand Down
4 changes: 3 additions & 1 deletion lib/tasks/bundlers/generateLibraryPreload.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {getLogger} from "@ui5/logger";
const log = getLogger("builder:tasks:bundlers:generateLibraryPreload");
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
import {negateFilters} from "../../lbt/resources/ResourceFilterList.js";
import createModuleNameMapping from "./utils/createModuleNameMapping.js";

Expand Down Expand Up @@ -265,7 +266,8 @@ export default async function({workspace, taskUtil, options: {skipBundles = [],
if (coreVersion) {
options.targetUi5CoreVersion = coreVersion;
}
return moduleBundler({options, resources, taskUtil});
options.bundleDefinition = applyDefaultsToBundleDefinition(options.bundleDefinition, taskUtil);
return moduleBundler({options, resources});
};

return nonDbgWorkspace.byGlob("/**/*.{js,json,xml,html,properties,library,js.map}").then(async (resources) => {
Expand Down
33 changes: 20 additions & 13 deletions lib/tasks/bundlers/generateStandaloneAppBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {getLogger} from "@ui5/logger";
const log = getLogger("builder:tasks:bundlers:generateStandaloneAppBundle");
import ReaderCollectionPrioritized from "@ui5/fs/ReaderCollectionPrioritized";
import moduleBundler from "../../processors/bundlers/moduleBundler.js";
import {applyDefaultsToBundleDefinition} from "./utils/applyDefaultsToBundleDefinition.js";
import createModuleNameMapping from "./utils/createModuleNameMapping.js";

function getBundleDefinition(config) {
Expand Down Expand Up @@ -139,24 +140,30 @@ export default async function({workspace, dependencies, taskUtil, options}) {
}

const bundleOptions = {
bundleDefinition: getBundleDefinition({
name: "sap-ui-custom.js",
filters,
namespace,
preloadSection: true
})
bundleDefinition: applyDefaultsToBundleDefinition(
getBundleDefinition({
name: "sap-ui-custom.js",
filters,
namespace,
preloadSection: true,
}),
taskUtil
),
};

const bundleDbgOptions = {
bundleDefinition: getBundleDefinition({
name: "sap-ui-custom-dbg.js",
filters,
namespace
}),
bundleDefinition: applyDefaultsToBundleDefinition(
getBundleDefinition({
name: "sap-ui-custom-dbg.js",
filters,
namespace,
}),
taskUtil
),
bundleOptions: {
optimize: false
optimize: false,
},
moduleNameMapping: unoptimizedModuleNameMapping
moduleNameMapping: unoptimizedModuleNameMapping,
};

if (coreVersion) {
Expand Down
31 changes: 31 additions & 0 deletions lib/tasks/bundlers/utils/applyDefaultsToBundleDefinition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Applies default values to bundleDefinitions
*
* @param {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleDefinition} bundleDefinition Module
* bundle definition
* @param {@ui5/project/build/helpers/TaskUtil|object} [taskUtil] TaskUtil
*
* @returns {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleDefinition}
*/
export function applyDefaultsToBundleDefinition(bundleDefinition, taskUtil) {
bundleDefinition.sections = bundleDefinition?.sections?.map((section) => {
const defaultValues = {
resolve: false,
resolveConditional: false,
renderer: false,
sort: true,
declareRawModules: false,
};

// Since specVersion: 4.0 "require" section starts loading asynchronously.
// If specVersion cannot be determined, the latest spec is taken into account.
// This is a breaking change in specVersion: 4.0
if (section.mode === "require" && (!taskUtil || taskUtil.getProject().getSpecVersion().gte("4.0"))) {
defaultValues.async = true;
}

return Object.assign(defaultValues, section);
});

return bundleDefinition;
}
18 changes: 6 additions & 12 deletions test/lib/tasks/bundlers/generateBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ test.serial("generateBundle: No taskUtil, no bundleOptions", async (t) => {
bundleDefinition,
bundleOptions: undefined
},
resources,
taskUtil: undefined
resources
}]);

t.is(combo.byGlob.callCount, 1,
Expand Down Expand Up @@ -172,8 +171,7 @@ test.serial("generateBundle: No bundleOptions, with taskUtil", async (t) => {
bundleOptions: undefined,
targetUi5CoreVersion: "1.120.0",
},
resources,
taskUtil
resources
}]);

t.is(combo.byGlob.callCount, 0,
Expand Down Expand Up @@ -294,8 +292,7 @@ test.serial("generateBundle: bundleOptions: optimize=false, with taskUtil", asyn
},
targetUi5CoreVersion: "1.120.0",
},
resources,
taskUtil
resources
}]);

t.is(combo.byGlob.callCount, 0,
Expand Down Expand Up @@ -418,8 +415,7 @@ test.serial("generateBundle: bundleOptions: sourceMap=false, with taskUtil", asy
bundleOptions,
targetUi5CoreVersion: "1.120.0"
},
resources,
taskUtil
resources
}]);

t.is(combo.byGlob.callCount, 0,
Expand Down Expand Up @@ -517,8 +513,7 @@ test.serial("generateBundle: Empty bundle (skipIfEmpty=true)", async (t) => {
bundleOptions,
targetUi5CoreVersion: "1.120.0"
},
resources,
taskUtil
resources
}]);

t.is(combo.byGlob.callCount, 0,
Expand Down Expand Up @@ -645,8 +640,7 @@ test.serial("generateBundle: No bundleOptions, with taskUtil, UI5 Version >= 2",
bundleOptions: undefined,
targetUi5CoreVersion: "2.0.0",
},
resources,
taskUtil
resources
}]);

t.is(combo.byGlob.callCount, 0,
Expand Down
Loading

0 comments on commit 31c2bca

Please sign in to comment.