Skip to content

Commit

Permalink
[INTERNAL] Refactor code to create moduleNameMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Mar 9, 2022
1 parent d61655e commit a3bcc5b
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 82 deletions.
30 changes: 4 additions & 26 deletions lib/tasks/bundlers/generateBundle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const moduleBundler = require("../../processors/bundlers/moduleBundler");
const ModuleName = require("../../lbt/utils/ModuleName");
const createModuleNameMapping = require("./utils/createModuleNameMapping");
const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritized;

/**
Expand Down Expand Up @@ -82,33 +82,11 @@ module.exports = function({
}

return combo.byGlob("/resources/**/*.{js,json,xml,html,properties,library,js.map}").then((resources) => {
const moduleNameMapping = {};
const options = {bundleDefinition, bundleOptions};
if (!optimize && taskUtil) {
// For "unoptimized" bundles, the non-debug files have already been filtered out above.
// Now we need to create a mapping from the debug-variant resource path to the respective module name,
// which is basically the non-debug resource path, minus the "/resources/"" prefix.
// This mapping overwrites internal logic of the LocatorResourcePool which would otherwise determine
// the module name from the resource path, which would contain "-dbg" in this case. That would be
// incorrect since debug-variants should still keep the original module name.
for (let i = resources.length - 1; i >= 0; i--) {
const resourcePath = resources[i].getPath();
if (taskUtil.getTag(resourcePath, taskUtil.STANDARD_TAGS.IsDebugVariant)) {
const nonDbgPath = ModuleName.getNonDebugName(resourcePath);
if (!nonDbgPath) {
throw new Error(`Failed to resolve non-debug name for ${resourcePath}`);
}
moduleNameMapping[resourcePath] = nonDbgPath.slice("/resources/".length);
}
}
options.moduleNameMapping = createModuleNameMapping({resources, taskUtil});
}
return moduleBundler({
options: {
bundleDefinition,
bundleOptions,
moduleNameMapping
},
resources
}).then((bundles) => {
return moduleBundler({options, resources}).then((bundles) => {
return Promise.all(bundles.map(({bundle, sourceMap} = {}) => {
if (!bundle) {
// Skip empty bundles
Expand Down
24 changes: 6 additions & 18 deletions lib/tasks/bundlers/generateLibraryPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const log = require("@ui5/logger").getLogger("builder:tasks:bundlers:generateLib
const moduleBundler = require("../../processors/bundlers/moduleBundler");
const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritized;
const {negateFilters} = require("../../lbt/resources/ResourceFilterList");
const ModuleName = require("../../lbt/utils/ModuleName");
const createModuleNameMapping = require("./utils/createModuleNameMapping");

function getDefaultLibraryPreloadFilters(namespace, excludes) {
const filters = [
Expand Down Expand Up @@ -311,7 +311,7 @@ module.exports = function({workspace, dependencies, taskUtil, options: {projectN
return resource.getPath() === "/resources/ui5loader.js";
});

const unoptimizedModuleNameMapping = {};
let unoptimizedModuleNameMapping;
let unoptimizedResources = resources;
if (taskUtil) {
unoptimizedResources = await new ReaderCollectionPrioritized({
Expand All @@ -322,22 +322,10 @@ module.exports = function({workspace, dependencies, taskUtil, options: {projectN
return !taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.HasDebugVariant);
}).byGlob("/**/*.{js,json,xml,html,properties,library,js.map}");

// For "unoptimized" bundles, the non-debug files have already been filtered out above.
// Now we need to create a mapping from the debug-variant resource path to the respective module
// name, which is basically the non-debug resource path, minus the "/resources/"" prefix.
// This mapping overwrites internal logic of the LocatorResourcePool which would otherwise determine
// the module name from the resource path, which would contain "-dbg" in this case. That would be
// incorrect since debug-variants should still keep the original module name.
for (let i = unoptimizedResources.length - 1; i >= 0; i--) {
const resourcePath = unoptimizedResources[i].getPath();
if (taskUtil.getTag(resourcePath, taskUtil.STANDARD_TAGS.IsDebugVariant)) {
const nonDbgPath = ModuleName.getNonDebugName(resourcePath);
if (!nonDbgPath) {
throw new Error(`Failed to resolve non-debug name for ${resourcePath}`);
}
unoptimizedModuleNameMapping[resourcePath] = nonDbgPath.slice("/resources/".length);
}
}
unoptimizedModuleNameMapping = createModuleNameMapping({
resources: unoptimizedResources,
taskUtil
});
}

let filters;
Expand Down
24 changes: 6 additions & 18 deletions lib/tasks/bundlers/generateStandaloneAppBundle.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const log = require("@ui5/logger").getLogger("builder:tasks:bundlers:generateStandaloneAppBundle");
const ReaderCollectionPrioritized = require("@ui5/fs").ReaderCollectionPrioritized;
const moduleBundler = require("../../processors/bundlers/moduleBundler");
const ModuleName = require("../../lbt/utils/ModuleName");
const createModuleNameMapping = require("./utils/createModuleNameMapping");

function getBundleDefinition(config) {
const bundleDefinition = {
Expand Down Expand Up @@ -101,7 +101,7 @@ module.exports = async function({workspace, dependencies, taskUtil, options: {pr
filters = ["jquery.sap.global.js"];
}

const unoptimizedModuleNameMapping = {};
let unoptimizedModuleNameMapping;
let unoptimizedResources = resources;
if (taskUtil) {
unoptimizedResources = await new ReaderCollectionPrioritized({
Expand All @@ -112,22 +112,10 @@ module.exports = async function({workspace, dependencies, taskUtil, options: {pr
return !taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.HasDebugVariant);
}).byGlob("/resources/**/*.{js,json,xml,html,properties,library,js.map}");

// For "unoptimized" bundles, the non-debug files have already been filtered out above.
// Now we need to create a mapping from the debug-variant resource path to the respective module name,
// which is basically the non-debug resource path, minus the "/resources/"" prefix.
// This mapping overwrites internal logic of the LocatorResourcePool which would otherwise determine
// the module name from the resource path, which would contain "-dbg" in this case. That would be
// incorrect since debug-variants should still keep the original module name.
for (let i = unoptimizedResources.length - 1; i >= 0; i--) {
const resourcePath = unoptimizedResources[i].getPath();
if (taskUtil.getTag(resourcePath, taskUtil.STANDARD_TAGS.IsDebugVariant)) {
const nonDbgPath = ModuleName.getNonDebugName(resourcePath);
if (!nonDbgPath) {
throw new Error(`Failed to resolve non-debug name for ${resourcePath}`);
}
unoptimizedModuleNameMapping[resourcePath] = nonDbgPath.slice("/resources/".length);
}
}
unoptimizedModuleNameMapping = createModuleNameMapping({
resources: unoptimizedResources,
taskUtil
});
}

await Promise.all([
Expand Down
30 changes: 30 additions & 0 deletions lib/tasks/bundlers/utils/createModuleNameMapping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const ModuleName = require("../../../lbt/utils/ModuleName");

/**
* For "unoptimized" bundles, the non-debug files have already been filtered out above.
* Now we need to create a mapping from the debug-variant resource path to the respective module
* name, which is basically the non-debug resource path, minus the "/resources/"" prefix.
* This mapping overwrites internal logic of the LocatorResourcePool which would otherwise determine
* the module name from the resource path, which would contain "-dbg" in this case. That would be
* incorrect since debug-variants should still keep the original module name.
*
* @private
* @param {object} parameters Parameters
* @param {module:@ui5/fs.Resource[]} parameters.resources List of resources
* @param {module:@ui5/builder.tasks.TaskUtil|object} parameters.taskUtil TaskUtil
* @returns {object} Module name mapping
*/
module.exports = function({resources, taskUtil}) {
const moduleNameMapping = {};
for (let i = resources.length - 1; i >= 0; i--) {
const resourcePath = resources[i].getPath();
if (taskUtil.getTag(resourcePath, taskUtil.STANDARD_TAGS.IsDebugVariant)) {
const nonDbgPath = ModuleName.getNonDebugName(resourcePath);
if (!nonDbgPath) {
throw new Error(`Failed to resolve non-debug name for ${resourcePath}`);
}
moduleNameMapping[resourcePath] = nonDbgPath.slice("/resources/".length);
}
}
return moduleNameMapping;
};
12 changes: 4 additions & 8 deletions test/lib/tasks/bundlers/generateBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ test.serial("generateBundle: No taskUtil, no bundleOptions", async (t) => {
t.deepEqual(moduleBundlerStub.getCall(0).args, [{
options: {
bundleDefinition,
bundleOptions: undefined,
moduleNameMapping: {}
bundleOptions: undefined
},
resources
}]);
Expand Down Expand Up @@ -156,8 +155,7 @@ test.serial("generateBundle: No bundleOptions, with taskUtil", async (t) => {
t.deepEqual(moduleBundlerStub.getCall(0).args, [{
options: {
bundleDefinition,
bundleOptions: undefined,
moduleNameMapping: {}
bundleOptions: undefined
},
resources
}]);
Expand Down Expand Up @@ -396,8 +394,7 @@ test.serial("generateBundle: bundleOptions: sourceMap=false, with taskUtil", asy
t.deepEqual(moduleBundlerStub.getCall(0).args, [{
options: {
bundleDefinition,
bundleOptions,
moduleNameMapping: {}
bundleOptions
},
resources
}]);
Expand Down Expand Up @@ -491,8 +488,7 @@ test.serial("generateBundle: Empty bundle (skipIfEmpty=true)", async (t) => {
t.deepEqual(moduleBundlerStub.getCall(0).args, [{
options: {
bundleDefinition,
bundleOptions,
moduleNameMapping: {}
bundleOptions
},
resources
}]);
Expand Down
18 changes: 6 additions & 12 deletions test/lib/tasks/bundlers/generateLibraryPreload.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,7 @@ test.serial("generateLibraryPreload for sap.ui.core (w/o ui5loader.js)", async (
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false
},
moduleNameMapping: {}
}
},
resources
}]);
Expand Down Expand Up @@ -345,8 +344,7 @@ test.serial("generateLibraryPreload for sap.ui.core (w/o ui5loader.js)", async (
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false
},
moduleNameMapping: {}
}
},
resources
}]);
Expand Down Expand Up @@ -515,8 +513,7 @@ test.serial("generateLibraryPreload for sap.ui.core (/w ui5loader.js)", async (t
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false
},
moduleNameMapping: {}
}
},
resources
}]);
Expand Down Expand Up @@ -607,8 +604,7 @@ test.serial("generateLibraryPreload for sap.ui.core (/w ui5loader.js)", async (t
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false
},
moduleNameMapping: {}
}
},
resources
}]);
Expand Down Expand Up @@ -835,8 +831,7 @@ test.serial("generateLibraryPreload for sap.ui.core with old specVersion defined
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false
},
moduleNameMapping: {}
}
},
resources
}]);
Expand Down Expand Up @@ -927,8 +922,7 @@ test.serial("generateLibraryPreload for sap.ui.core with old specVersion defined
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false
},
moduleNameMapping: {}
}
},
resources
}]);
Expand Down

0 comments on commit a3bcc5b

Please sign in to comment.