Skip to content

Commit

Permalink
Attempt to read .map file for source maps by default, for bytecode …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
TooTallNate committed Jul 7, 2024
1 parent 97f810a commit 90ab2e3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-schools-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nxjs-runtime": patch
---

Attempt to read `.map` file for source maps by default, for bytecode files
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ endif
all: $(BUILD)

$(SOURCES)/runtime.c: packages/runtime/runtime.js
@qjsc -o $(SOURCES)/runtime.c packages/runtime/runtime.js
@qjsc -o $(SOURCES)/runtime.c -n "romfs:/runtime.js" packages/runtime/runtime.js
@echo "compiled '$(SOURCES)/runtime.c' with qjsc"

$(ROMFS)/runtime.js.map: packages/runtime/runtime.js.map
Expand Down
49 changes: 30 additions & 19 deletions packages/runtime/src/source-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,39 @@ function filenameToTracer(filename: string) {
// `null` means the source map could not be retrieved for this file
tracer = null;

const contentsBuffer = readFileSync(filename);
if (contentsBuffer) {
const contents = decoder.decode(contentsBuffer).trimEnd();
const lastNewline = contents.lastIndexOf('\n');
const lastLine = contents.slice(lastNewline + 1);
if (lastLine.startsWith(SOURCE_MAPPING_URL_PREFIX)) {
const sourceMappingURL = lastLine.slice(SOURCE_MAPPING_URL_PREFIX.length);
let sourceMapBuffer: ArrayBuffer | null;
if (sourceMappingURL.startsWith('data:')) {
sourceMapBuffer = dataUriToBuffer(sourceMappingURL).buffer;
} else {
sourceMapBuffer = readFileSync(new URL(sourceMappingURL, filename));
}
if (sourceMapBuffer) {
const sourceMap: EncodedSourceMap = JSON.parse(
decoder.decode(sourceMapBuffer),
);
tracer = new TraceMap(sourceMap);
// Check for the `.map` file based on the filename as a shortcut / default
let sourceMappingURL: string | URL = new URL(`${filename}.map`, filename);
let sourceMapBuffer = readFileSync(sourceMappingURL);

if (!sourceMapBuffer) {
// When the `.map` file is not found, try to find it based on the
// `sourceMappingURL` embedded in the source code, which handles
// source maps embedded in as data URIs or in a non-standard location
const contentsBuffer = readFileSync(filename);
if (contentsBuffer) {
const contents = decoder.decode(contentsBuffer).trimEnd();
const lastNewline = contents.lastIndexOf('\n');
const lastLine = contents.slice(lastNewline + 1);
if (lastLine.startsWith(SOURCE_MAPPING_URL_PREFIX)) {
sourceMappingURL = lastLine.slice(SOURCE_MAPPING_URL_PREFIX.length);
if (sourceMappingURL.startsWith('data:')) {
sourceMapBuffer = dataUriToBuffer(sourceMappingURL).buffer;
} else {
sourceMapBuffer = readFileSync(new URL(sourceMappingURL, filename));
}
}
}
sourceMapCache.set(filename, tracer);
}

if (sourceMapBuffer) {
const sourceMap: EncodedSourceMap = JSON.parse(
decoder.decode(sourceMapBuffer),
);
tracer = new TraceMap(sourceMap);
}

sourceMapCache.set(filename, tracer);

return tracer;
}

Expand Down

0 comments on commit 90ab2e3

Please sign in to comment.