Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[INTERNAL] SourceMaps follow-up #709

Merged
merged 13 commits into from
Mar 10, 2022
Merged
4 changes: 0 additions & 4 deletions lib/lbt/resources/LocatorResource.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
const Resource = require("./Resource");

// function extractName(path) {
// return path.slice( "/resources/".length);
// }

class LocatorResource extends Resource {
constructor(pool, resource, moduleName) {
super(pool, moduleName, null, resource.getStatInfo());
Expand Down
7 changes: 4 additions & 3 deletions lib/processors/bundlers/moduleBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ const log = require("@ui5/logger").getLogger("builder:processors:bundlers:module
* @public
* @typedef {object} ModuleBundleOptions
* @property {boolean} [optimize=true] Whether the module bundle gets minified
* @property {boolean} [sourceMap] Whether to generate a source map file for the bundle.
* Defaults to true if <code>optimize</code> is set to true
* @property {boolean} [sourceMap=true] Whether to generate a source map file for the bundle
* @property {boolean} [decorateBootstrapModule=false] If set to 'false', the module won't be decorated
* with an optimization marker
* @property {boolean} [addTryCatchRestartWrapper=false] Whether to wrap bootable module bundles with
Expand Down Expand Up @@ -124,12 +123,14 @@ const log = require("@ui5/logger").getLogger("builder:processors:bundlers:module
Optional mapping of resource paths to module name in order to overwrite the default determination
* @param {ModuleBundleDefinition} parameters.options.bundleDefinition Module bundle definition
* @param {ModuleBundleOptions} [parameters.options.bundleOptions] Module bundle options
* @returns {Promise<module:@ui5/builder.processors.MinifierResult[]>} Promise resolving with module bundle resources
* @returns {Promise<module:@ui5/builder.processors.ModuleBundlerResult[]>}
* Promise resolving with module bundle resources
*/
module.exports = function({resources, options: {bundleDefinition, bundleOptions, moduleNameMapping}}) {
// Apply defaults without modifying the passed object
bundleOptions = Object.assign({}, {
optimize: true,
sourceMap: true,
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false,
Expand Down
5 changes: 3 additions & 2 deletions lib/processors/minifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ const debugFileRegex = /((?:\.view|\.fragment|\.controller|\.designtime|\.suppor
* @alias module:@ui5/builder.processors.minifier
* @param {object} parameters Parameters
* @param {module:@ui5/fs.Resource[]} parameters.resources List of resources to be processed
* @param {boolean} [parameters.addSourceMappingUrl=true]
* @param {object} [parameters.options] Options
* @param {boolean} [parameters.options.addSourceMappingUrl=true]
* Whether to add a sourceMappingURL reference to the end of the minified resource
* @returns {Promise<module:@ui5/builder.processors.MinifierResult[]>}
* Promise resolving with object of resource, dbgResource and sourceMap
*/
module.exports = async function({resources, addSourceMappingUrl = true}) {
module.exports = async function({resources, options: {addSourceMappingUrl = true} = {}}) {
return Promise.all(resources.map(async (resource) => {
const dbgPath = resource.getPath().replace(debugFileRegex, "-dbg$1");
const dbgResource = await resource.clone();
Expand Down
38 changes: 10 additions & 28 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 All @@ -21,7 +21,7 @@ module.exports = function({
workspace, dependencies, taskUtil, options: {projectName, bundleDefinition, bundleOptions}
}) {
let combo = new ReaderCollectionPrioritized({
name: `libraryBundler - prioritize workspace over dependencies: ${projectName}`,
name: `generateBundle - prioritize workspace over dependencies: ${projectName}`,
readers: [workspace, dependencies]
});

Expand Down Expand Up @@ -82,34 +82,16 @@ 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 Promise.all(bundles.map(({bundle, sourceMap}) => {
return moduleBundler({options, resources}).then((bundles) => {
return Promise.all(bundles.map(({bundle, sourceMap} = {}) => {
if (!bundle) {
// Skip empty bundles
return;
}
if (taskUtil) {
taskUtil.setTag(bundle, taskUtil.STANDARD_TAGS.IsBundle);
if (sourceMap) {
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;
};
4 changes: 3 additions & 1 deletion lib/tasks/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = async function({workspace, taskUtil, options: {pattern, omitSou
const resources = await workspace.byGlob(pattern);
const processedResources = await minifier({
resources,
addSourceMappingUrl: !omitSourceMapResources
options: {
addSourceMappingUrl: !omitSourceMapResources
}
});

return Promise.all(processedResources.map(async ({resource, dbgResource, sourceMapResource}) => {
Expand Down
7 changes: 7 additions & 0 deletions test/lib/processors/bundlers/moduleBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ test.serial("Builder returns single bundle", async (t) => {
t.deepEqual(builder.createBundle.getCall(0).args[1], {
// default bundleOptions
optimize: true,
sourceMap: true,
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false,
Expand Down Expand Up @@ -209,6 +210,7 @@ test.serial("Builder returns multiple bundles", async (t) => {
t.deepEqual(builder.createBundle.getCall(0).args[1], {
// default bundleOptions
optimize: true,
sourceMap: true,
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false,
Expand Down Expand Up @@ -297,6 +299,7 @@ test.serial("bundleOptions default (no options passed)", async (t) => {
t.deepEqual(builder.createBundle.getCall(0).args[1], {
// default bundleOptions
optimize: true,
sourceMap: true,
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false,
Expand Down Expand Up @@ -361,6 +364,7 @@ test.serial("bundleOptions default (empty options passed)", async (t) => {
t.deepEqual(builder.createBundle.getCall(0).args[1], {
// default bundleOptions
optimize: true,
sourceMap: true,
decorateBootstrapModule: false,
addTryCatchRestartWrapper: false,
usePredefineCalls: false,
Expand All @@ -383,6 +387,7 @@ test.serial("bundleOptions (all options passed)", async (t) => {
};
const bundleOptions = {
optimize: false,
sourceMap: false,
decorateBootstrapModule: true,
addTryCatchRestartWrapper: true,
usePredefineCalls: true,
Expand Down Expand Up @@ -440,6 +445,7 @@ test.serial("Passes ignoreMissingModules bundleOption to LocatorResourcePool", a
const effectiveBundleOptions = {
// Defaults
"optimize": true,
"sourceMap": true,
"decorateBootstrapModule": false,
"addTryCatchRestartWrapper": false,
"usePredefineCalls": false,
Expand Down Expand Up @@ -527,6 +533,7 @@ test.serial("Verbose Logging", async (t) => {
const effectiveBundleOptions = {
// Defaults
"optimize": true,
"sourceMap": true,
"decorateBootstrapModule": false,
"addTryCatchRestartWrapper": false,
"usePredefineCalls": false,
Expand Down
Loading