Repository: vercel/next.js
File: turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/dev/hmr-client.ts (TypeScript)

Code before:

type NodeJsHmrPayload = {
  resource: {
    path: string
    headers?: Record<string, string>
  }
  issues: Issue[]
  type: 'partial'
  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')
  }
  return code
}

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

function initializeServerHmr(
  moduleFactories: ModuleFactories,
  devModuleCache: ModuleCache<HotModule>
): void {
  if (serverHmrUpdateHandler != null) {
    throw new Error('[Server HMR] Server HMR client is already initialized')
  }

  // Register the update handler for the server runtime
  serverHmrUpdateHandler = (msg: NodeJsHmrPayload) => {
    handleNodejsUpdate(msg, moduleFactories, devModuleCache)
  }
}

/**
 * Emits an HMR message to the registered update handler.
 * Node uses a simpler listener pattern than the browser's websocket connection.
 *
 * Note: This is only called via __turbopack_server_hmr_apply__ which ensures

Review comment (performance):
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

Code after:

type NodeJsHmrPayload = {
  resource: {
    path: string
    headers?: Record<string, string>
  }
  issues: Issue[]
  type: 'partial'
  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 inlineSourcemaps(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(
  moduleFactories: ModuleFactories,
  devModuleCache: ModuleCache<HotModule>
): void {
  if (serverHmrUpdateHandler != null) {
    throw new Error('[Server HMR] Server HMR client is already initialized')
  }

  // Register the update handler for the server runtime
  serverHmrUpdateHandler = (msg: NodeJsHmrPayload) => {
    handleNodejsUpdate(msg, moduleFactories, devModuleCache)
  }
}

/**
 * Emits an HMR message to the registered update handler.
 * Node uses a simpler listener pattern than the browser's websocket connection.
 *
 * Note: This is only called via __turbopack_server_hmr_apply__ which ensures