Skip to content

Commit

Permalink
lbt/bundle/Builder: Resolve sourceMappingURL for locating input sourc…
Browse files Browse the repository at this point in the history
…e maps
  • Loading branch information
RandomByte committed Nov 16, 2021
1 parent 04935e0 commit 93b7eb0
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions lib/lbt/bundle/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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`);
}
}

Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 93b7eb0

Please sign in to comment.