From 93b7eb015309363a90db72c9b530d0c7b553f4c2 Mon Sep 17 00:00:00 2001 From: Merlin Beutlberger Date: Tue, 16 Nov 2021 15:02:51 +0100 Subject: [PATCH] lbt/bundle/Builder: Resolve sourceMappingURL for locating input source maps --- lib/lbt/bundle/Builder.js | 56 ++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/lib/lbt/bundle/Builder.js b/lib/lbt/bundle/Builder.js index bfe3cf135..754aed86a 100644 --- a/lib/lbt/bundle/Builder.js +++ b/lib/lbt/bundle/Builder.js @@ -18,7 +18,7 @@ const {SectionType} = require("./BundleDefinition"); const BundleWriter = require("./BundleWriter"); const log = require("@ui5/logger").getLogger("lbt:bundle:Builder"); -const sourcemapPattern = /\/\/# sourceMappingURL=data:application\/json;charset=utf-8;base64,(.+)/i; +const sourceMapUrlPattern = /\/\/# sourceMappingURL=(.+)\s*$/; const xmlHtmlPrePattern = /<(?:\w+:)?pre\b/; const strReplacements = { @@ -224,7 +224,7 @@ class BundleBuilder { this.outW.writeln(`}`); } if (this.options.sourceMap) { - this.outW.writeln(`//# sourceMappingURL=${path.basename(resolvedModule.name)}.map`); + this.outW.writeln(`//# sourceMappingURL=${path.posix.basename(resolvedModule.name)}.map`); } } @@ -367,7 +367,7 @@ class BundleBuilder { } map.sourceRoot = path.posix.relative( - path.dirname(this._bundleName), path.dirname(moduleName)); + path.posix.dirname(this._bundleName), path.posix.dirname(moduleName)); this._sourceMap.sections.push({ offset: { @@ -543,25 +543,43 @@ class BundleBuilder { async retrieveSourceMapForModule(moduleName, moduleContent) { let moduleSourceMap = null; - try { - const sourceMapResource = await this.pool.findResource(`${moduleName}.map`); - moduleSourceMap = (await sourceMapResource.buffer()).toString(); - } catch (e) { - // No input source map - // TODO: Differentiate real errors from file not found - } + let newModuleContent = moduleContent; + + const sourceMapUrlMatch = moduleContent.match(sourceMapUrlPattern); + if (sourceMapUrlMatch) { + const sourceMapUrl = sourceMapUrlMatch[1]; + + if (sourceMapUrl.startsWith("data:")) { + // Data-URL indicates an inline source map + const expectedTypeAndEncoding = "data:application/json;charset=utf-8;base64,"; + if (sourceMapUrl.startsWith(expectedTypeAndEncoding)) { + const base64Content = sourceMapUrl.slice(expectedTypeAndEncoding.length); + moduleSourceMap = Buffer.from(base64Content, "base64").toString(); + } else { + log.warn("TODO"); + } + } else { + if (path.posix.isAbsolute(sourceMapUrl)) { + log.warn("Unexpected absolute source map path"); + } - if (!moduleSourceMap) { - const inlineSourcemap = moduleContent.match(sourcemapPattern); - if (inlineSourcemap) { - moduleSourceMap = Buffer.from(inlineSourcemap[1], "base64").toString(); - } - } + // TODO: Check for actual URL, which is not supported - // Strip sourceMappingURL from module code to be bundled - // It has no effect and might be cause for confusion - const newModuleContent = moduleContent.replace(/\/\/# sourceMappingURL=.*/, ""); + const sourceMapPath = path.posix.join(path.posix.dirname(moduleName), sourceMapUrl); + try { + const sourceMapResource = await this.pool.findResource(sourceMapPath); + moduleSourceMap = (await sourceMapResource.buffer()).toString(); + } catch (e) { + // No input source map + // TODO: Differentiate real errors from file not found + } + } + + // Strip sourceMappingURL from module code to be bundled + // It has no effect and might be cause for confusion + newModuleContent = moduleContent.replace(sourceMapUrlPattern, ""); + } return { moduleSourceMap, moduleContent: newModuleContent