Skip to content

Commit

Permalink
webpack: Sourcemap externals when replayed in the browser (vercel#75863)
Browse files Browse the repository at this point in the history
This was already implemented in the middleware for Turbopack but absent when Webpack was used. We now always prefer the native `findSourceMap` over invoking Webpack's API.

This way stackframes from externals are now properly sourcemapped (and therefore ignore-listed)

Notice how the `react-server-dom-webpack...` stackframe (`console.error` method) wasn't ignore-listed before.

Before:

![CleanShot 2025-02-10 at 14.34.44.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/dLGs20hkgfWzupsPjSxb/2a8147d4-4b6f-4170-ae36-8b544ff0bb1a.png)

After:

![CleanShot 2025-02-10 at 14.36.15.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/dLGs20hkgfWzupsPjSxb/f713e36d-88f4-4185-8bf8-0293a7b7f4f1.png)
  • Loading branch information
eps1lon authored Feb 13, 2025
1 parent a2f52ed commit ed67600
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { constants as FS, promises as fs } from 'fs'
import { findSourceMap, type SourceMap } from 'module'
import path from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import {
Expand Down Expand Up @@ -128,8 +129,10 @@ async function findOriginalSourcePositionAndContent(
}
}

export function getIgnoredSources(sourceMap: RawSourceMap): IgnoredSources {
const ignoreList = new Set<number>()
export function getIgnoredSources(
sourceMap: RawSourceMap & { ignoreList?: number[] }
): IgnoredSources {
const ignoreList = new Set<number>(sourceMap.ignoreList ?? [])
const moduleFilenames = sourceMap?.sources ?? []

for (let index = 0; index < moduleFilenames.length; index++) {
Expand Down Expand Up @@ -289,6 +292,26 @@ async function getSource(
): Promise<Source | undefined> {
const { getCompilations } = options

let nativeSourceMap: SourceMap | undefined
try {
nativeSourceMap = findSourceMap(sourceURL)
} catch (cause) {
throw new Error(
`${sourceURL}: Invalid source map. Only conformant source maps can be used to find the original code.`,
{ cause }
)
}

if (nativeSourceMap !== undefined) {
const sourceMapPayload = nativeSourceMap.payload
return {
type: 'file',
sourceMap: sourceMapPayload,
ignoredSources: getIgnoredSources(sourceMapPayload),
moduleURL: sourceURL,
}
}

if (path.isAbsolute(sourceURL)) {
sourceURL = pathToFileURL(sourceURL).href
}
Expand Down Expand Up @@ -316,7 +339,7 @@ async function getSource(
.replace(/\?\d+$/, '')

// (rsc)/./src/hello.tsx => ./src/hello.tsx
const modulePath = moduleId.replace(/^(\(.*\)\/?)/, '')
const moduleURL = moduleId.replace(/^(\(.*\)\/?)/, '')

for (const compilation of getCompilations()) {
const sourceMap = await getSourceMapFromCompilation(moduleId, compilation)
Expand All @@ -328,7 +351,7 @@ async function getSource(
sourceMap,
compilation,
moduleId,
moduleURL: modulePath,
moduleURL,
ignoredSources,
}
}
Expand Down

0 comments on commit ed67600

Please sign in to comment.