Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ type NodeJsHmrPayload = {
instruction: EcmascriptMergedUpdate
}

/**
* Appends the module code with //# sourceURL and //# sourceMappingURL so
* that Node.js can resolve stack frames from `eval`ed server HMR modules back to
* their original source files. Mirrors the browser's _eval in dev-backend-dom.ts.
*/
function maybeInlineSourcemaps(entry: EcmascriptModuleEntry): string {
const [chunkPath, moduleId] = entry.url.split('?', 2)
const absolutePath = path.resolve(RUNTIME_ROOT, chunkPath)
const fileHref = url.pathToFileURL(absolutePath).href
const sourceURL = moduleId ? `${fileHref}?${moduleId}` : fileHref
let code = entry.code + '\n\n//# sourceURL=' + sourceURL
if (entry.map) {
code +=
'\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,' +
Buffer.from(entry.map).toString('base64')
Comment on lines +26 to +30
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have to inline it? can we use a file reference here? sourceMappingUrl=file://...

this will slow down eval times (and hmr times) since our sourcemaps can contain source content already

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oic, entry.map is just the literal contents...

this thread implies you can just append the contents directly: https://issues.chromium.org/issues/41177554

but this is fine

}
return code
}

let serverHmrUpdateHandler: ((msg: NodeJsHmrPayload) => void) | null = null

function initializeServerHmr(
Expand Down Expand Up @@ -75,10 +94,9 @@ function handleNodejsUpdate(
try {
const { entries = {}, chunks = {} } = instruction

// Node.js eval function (no source maps)
const evalModuleEntry = (entry: EcmascriptModuleEntry) => {
// eslint-disable-next-line no-eval
return (0, eval)(entry.code)
return (0, eval)(maybeInlineSourcemaps(entry))
}

const { added, modified } = computeChangedModules(
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Loading