Commit 65e209ac authored by Will Binns-Smith's avatar Will Binns-Smith

test sourcemaps

parent 57713d37
......@@ -155,12 +155,43 @@ type ServerHmrSubscriptions = Map<
AsyncIterableIterator<TurbopackResult<NodeJsHmrUpdate>>
>
type HmrModuleEntry = NodeJsPartialHmrUpdate['instruction']['entries'][string]
/**
* Annotates an HMR module entry's code with //# sourceURL and
* //# sourceMappingURL so Node.js can resolve stack frames from eval'd
* server HMR modules back to their original source files.
*
* This is a temporary workaround until the Turbopack binary is rebuilt
* with the updated hmr-client.ts which does this annotation inside
* evalModuleEntry using RUNTIME_ROOT. Once the binary is updated,
* this pre-annotation can be removed (duplicate annotations are harmless
* since V8 uses the last //# sourceURL comment).
*/
function annotateHmrEntry(
entry: HmrModuleEntry,
distDir: string
): HmrModuleEntry {
const [chunkPath, moduleId] = entry.url.split('?', 2)
const fileHref = pathToFileURL(join(distDir, chunkPath)).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')
}
return { ...entry, code }
}
function setupServerHmr(
project: Project,
{
onUpdateFailed,
distDir,
}: {
onUpdateFailed: () => void | Promise<void>
distDir: string
}
) {
const serverHmrSubscriptions: ServerHmrSubscriptions = new Map()
......@@ -194,7 +225,18 @@ function setupServerHmr(
}
if (typeof __turbopack_server_hmr_apply__ === 'function') {
const applied = __turbopack_server_hmr_apply__(update)
const annotatedEntries: NodeJsPartialHmrUpdate['instruction']['entries'] =
{}
for (const [key, entry] of Object.entries(
update.instruction.entries
)) {
annotatedEntries[key] = annotateHmrEntry(entry, distDir)
}
const annotatedUpdate: NodeJsPartialHmrUpdate = {
...update,
instruction: { ...update.instruction, entries: annotatedEntries },
}
const applied = __turbopack_server_hmr_apply__(annotatedUpdate)
if (!applied) {
await onUpdateFailed()
}
......@@ -1806,6 +1848,7 @@ export async function createHotReloaderTurbopack(
})
serverHmrSubscriptions = setupServerHmr(project, {
distDir,
onUpdateFailed: async () => {
if (typeof __next__clear_chunk_cache__ === 'function') {
__next__clear_chunk_cache__()
......
......@@ -413,7 +413,7 @@ describe('Error recovery app', () => {
'index.js',
outdent`
import Child from './child'
export default function Index() {
return (
<main>
......@@ -492,7 +492,7 @@ describe('Error recovery app', () => {
'index.js',
outdent`
import Child from './child'
export default function Index() {
return (
<main>
......
......@@ -13,6 +13,28 @@ type NodeJsHmrPayload = {
instruction: EcmascriptMergedUpdate
}
/**
* Annotates module entry code with //# sourceURL and //# sourceMappingURL so
* that Node.js can resolve stack frames from eval'd server HMR modules back to
* their original source files. Mirrors the browser's _eval in dev-backend-dom.ts.
*
* RUNTIME_ROOT, path, and url are available from earlier files in the
* concatenated runtime bundle (node-externals-utils.ts, runtime-base.ts).
*/
function annotateEntryCode(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')
}
return code
}
let serverHmrUpdateHandler: ((msg: NodeJsHmrPayload) => void) | null = null
function initializeServerHmr(
......@@ -75,10 +97,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)(annotateEntryCode(entry))
}
const { added, modified } = computeChangedModules(
......
......@@ -1466,7 +1466,25 @@ module.exports = (sourcePath)=>({
});
/// <reference path="../../shared/runtime/dev-protocol.d.ts" />
/// <reference path="../../shared/runtime/hmr-runtime.ts" />
/* eslint-disable @typescript-eslint/no-unused-vars */ let serverHmrUpdateHandler = null;
/* eslint-disable @typescript-eslint/no-unused-vars */ /**
* Annotates module entry code with //# sourceURL and //# sourceMappingURL so
* that Node.js can resolve stack frames from eval'd server HMR modules back to
* their original source files. Mirrors the browser's _eval in dev-backend-dom.ts.
*
* RUNTIME_ROOT, path, and url are available from earlier files in the
* concatenated runtime bundle (node-externals-utils.ts, runtime-base.ts).
*/ function annotateEntryCode(entry) {
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');
}
return code;
}
let serverHmrUpdateHandler = null;
function initializeServerHmr(moduleFactories, devModuleCache) {
if (serverHmrUpdateHandler != null) {
throw new Error('[Server HMR] Server HMR client is already initialized');
......@@ -1508,10 +1526,9 @@ function initializeServerHmr(moduleFactories, devModuleCache) {
}
try {
const { entries = {}, chunks = {} } = instruction;
// Node.js eval function (no source maps)
const evalModuleEntry = (entry)=>{
// eslint-disable-next-line no-eval
return (0, eval)(entry.code);
return (0, eval)(annotateEntryCode(entry));
};
const { added, modified } = computeChangedModules(entries, chunks, undefined // no chunkModulesMap for Node.js
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment