diff --git a/lib/tasks/bundlers/generateBundle.js b/lib/tasks/bundlers/generateBundle.js index b6922b47b..02b9040fd 100644 --- a/lib/tasks/bundlers/generateBundle.js +++ b/lib/tasks/bundlers/generateBundle.js @@ -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; /** @@ -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 diff --git a/lib/tasks/bundlers/generateLibraryPreload.js b/lib/tasks/bundlers/generateLibraryPreload.js index 654aa65ca..0a027620a 100644 --- a/lib/tasks/bundlers/generateLibraryPreload.js +++ b/lib/tasks/bundlers/generateLibraryPreload.js @@ -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 = [ @@ -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({ @@ -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; diff --git a/lib/tasks/bundlers/generateStandaloneAppBundle.js b/lib/tasks/bundlers/generateStandaloneAppBundle.js index 7621c447c..59ad6c0c2 100644 --- a/lib/tasks/bundlers/generateStandaloneAppBundle.js +++ b/lib/tasks/bundlers/generateStandaloneAppBundle.js @@ -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 = { @@ -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({ @@ -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([ diff --git a/lib/tasks/bundlers/utils/createModuleNameMapping.js b/lib/tasks/bundlers/utils/createModuleNameMapping.js new file mode 100644 index 000000000..23544ab37 --- /dev/null +++ b/lib/tasks/bundlers/utils/createModuleNameMapping.js @@ -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; +}; diff --git a/test/lib/tasks/bundlers/generateBundle.js b/test/lib/tasks/bundlers/generateBundle.js index fb2619cdc..e29478769 100644 --- a/test/lib/tasks/bundlers/generateBundle.js +++ b/test/lib/tasks/bundlers/generateBundle.js @@ -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 }]); @@ -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 }]); @@ -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 }]); @@ -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 }]); diff --git a/test/lib/tasks/bundlers/generateLibraryPreload.js b/test/lib/tasks/bundlers/generateLibraryPreload.js index 30f87a6ae..4130f7098 100644 --- a/test/lib/tasks/bundlers/generateLibraryPreload.js +++ b/test/lib/tasks/bundlers/generateLibraryPreload.js @@ -253,8 +253,7 @@ test.serial("generateLibraryPreload for sap.ui.core (w/o ui5loader.js)", async ( decorateBootstrapModule: false, addTryCatchRestartWrapper: false, usePredefineCalls: false - }, - moduleNameMapping: {} + } }, resources }]); @@ -345,8 +344,7 @@ test.serial("generateLibraryPreload for sap.ui.core (w/o ui5loader.js)", async ( decorateBootstrapModule: false, addTryCatchRestartWrapper: false, usePredefineCalls: false - }, - moduleNameMapping: {} + } }, resources }]); @@ -515,8 +513,7 @@ test.serial("generateLibraryPreload for sap.ui.core (/w ui5loader.js)", async (t decorateBootstrapModule: false, addTryCatchRestartWrapper: false, usePredefineCalls: false - }, - moduleNameMapping: {} + } }, resources }]); @@ -607,8 +604,7 @@ test.serial("generateLibraryPreload for sap.ui.core (/w ui5loader.js)", async (t decorateBootstrapModule: false, addTryCatchRestartWrapper: false, usePredefineCalls: false - }, - moduleNameMapping: {} + } }, resources }]); @@ -835,8 +831,7 @@ test.serial("generateLibraryPreload for sap.ui.core with old specVersion defined decorateBootstrapModule: false, addTryCatchRestartWrapper: false, usePredefineCalls: false - }, - moduleNameMapping: {} + } }, resources }]); @@ -927,8 +922,7 @@ test.serial("generateLibraryPreload for sap.ui.core with old specVersion defined decorateBootstrapModule: false, addTryCatchRestartWrapper: false, usePredefineCalls: false - }, - moduleNameMapping: {} + } }, resources }]);