Unverified Commit 3a41715c authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[refactor] Move sourcemapping logic to dedicated file (#81310)

Resolves https://github.com/vercel/next.js/pull/81231#discussion_r2185437480
parent 09c8af1c
......@@ -305,7 +305,8 @@
"patchedDependencies": {
"webpack-sources@3.2.3": "patches/webpack-sources@3.2.3.patch",
"stacktrace-parser@0.1.10": "patches/stacktrace-parser@0.1.10.patch",
"@ampproject/toolbox-optimizer@2.8.3": "patches/@ampproject__toolbox-optimizer@2.8.3.patch"
"@ampproject/toolbox-optimizer@2.8.3": "patches/@ampproject__toolbox-optimizer@2.8.3.patch",
"@types/node@20.17.6": "patches/@types__node@20.17.6.patch"
}
}
}
......@@ -77,12 +77,10 @@ import { FAST_REFRESH_RUNTIME_RELOAD } from './messages'
import { generateEncryptionKeyBase64 } from '../app-render/encryption-utils-server'
import { isAppPageRouteDefinition } from '../route-definitions/app-page-route-definition'
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths'
import type { ModernSourceMapPayload } from '../lib/source-maps'
import { getNodeDebugType } from '../lib/utils'
import { isMetadataRouteFile } from '../../lib/metadata/is-metadata-route'
import {
setBundlerFindSourceMapImplementation,
type ModernSourceMapPayload,
} from '../patch-error-inspect'
import { setBundlerFindSourceMapImplementation } from '../patch-error-inspect'
import { getNextErrorFeedbackMiddleware } from '../../next-devtools/server/get-next-error-feedback-middleware'
import {
formatIssue,
......
......@@ -12,12 +12,15 @@ import { openFileInEditor } from '../../next-devtools/server/launch-editor'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import {
SourceMapConsumer,
type BasicSourceMapConsumer,
type NullableMappedPosition,
} from 'next/dist/compiled/source-map08'
import type { Project, TurbopackStackFrame } from '../../build/swc/types'
import {
type ModernSourceMapPayload,
findApplicableSourceMapPayload,
} from '../lib/source-maps'
import { getSourceMapFromFile } from './get-source-map-from-file'
import { findSourceMap, type SourceMapPayload } from 'node:module'
import { findSourceMap } from 'node:module'
import { pathToFileURL } from 'node:url'
import { inspect } from 'node:util'
......@@ -170,61 +173,6 @@ function createStackFrame(
} satisfies TurbopackStackFrame
}
/**
* https://tc39.es/source-map/#index-map
*/
interface IndexSourceMapSection {
offset: {
line: number
column: number
}
map: ModernRawSourceMap
}
// TODO(veil): Upstream types
interface IndexSourceMap {
version: number
file: string
sections: IndexSourceMapSection[]
}
interface ModernRawSourceMap extends SourceMapPayload {
ignoreList?: number[]
}
type ModernSourceMapPayload = ModernRawSourceMap | IndexSourceMap
/**
* Finds the sourcemap payload applicable to a given frame.
* Equal to the input unless an Index Source Map is used.
*/
function findApplicableSourceMapPayload(
frame: TurbopackStackFrame,
payload: ModernSourceMapPayload
): ModernRawSourceMap | undefined {
if ('sections' in payload) {
const frameLine = frame.line ?? 0
const frameColumn = frame.column ?? 0
// Sections must not overlap and must be sorted: https://tc39.es/source-map/#section-object
// Therefore the last section that has an offset less than or equal to the frame is the applicable one.
// TODO(veil): Binary search
let section: IndexSourceMapSection | undefined = payload.sections[0]
for (
let i = 0;
i < payload.sections.length &&
payload.sections[i].offset.line <= frameLine &&
payload.sections[i].offset.column <= frameColumn;
i++
) {
section = payload.sections[i]
}
return section === undefined ? undefined : section.map
} else {
return payload
}
}
/**
* @returns 1-based lines and 0-based columns
*/
......@@ -244,7 +192,7 @@ async function nativeTraceSource(
}
if (sourceMapPayload !== undefined) {
let consumer: BasicSourceMapConsumer
let consumer: SourceMapConsumer
try {
consumer = await new SourceMapConsumer(sourceMapPayload)
} catch (cause) {
......@@ -282,7 +230,8 @@ async function nativeTraceSource(
if (traced !== null) {
const { originalPosition, sourceContent } = traced
const applicableSourceMap = findApplicableSourceMapPayload(
frame,
frame.line ?? 0,
frame.column ?? 0,
sourceMapPayload
)
......
import { findSourceMap, type SourceMap } from 'module'
import path from 'path'
import { fileURLToPath, pathToFileURL } from 'url'
import {
SourceMapConsumer,
type BasicSourceMapConsumer,
} from 'next/dist/compiled/source-map08'
import { SourceMapConsumer } from 'next/dist/compiled/source-map08'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import { getSourceMapFromFile } from './get-source-map-from-file'
import {
sourceMapIgnoreListsEverything,
type ModernSourceMapPayload,
} from '../lib/source-maps'
import { openFileInEditor } from '../../next-devtools/server/launch-editor'
import {
getOriginalCodeFrame,
......@@ -15,7 +16,6 @@ import {
type OriginalStackFramesResponse,
} from '../../next-devtools/server/shared'
import { middlewareResponse } from '../../next-devtools/server/middleware-response'
export { getSourceMapFromFile }
import type { IncomingMessage, ServerResponse } from 'http'
import type webpack from 'webpack'
......@@ -50,13 +50,13 @@ type SourceAttributes = {
type Source =
| {
type: 'file'
sourceMap: RawSourceMap
sourceMap: ModernSourceMapPayload
ignoredSources: IgnoredSources
moduleURL: string
}
| {
type: 'bundle'
sourceMap: RawSourceMap
sourceMap: ModernSourceMapPayload
ignoredSources: IgnoredSources
compilation: webpack.Compilation
moduleId: string
......@@ -87,10 +87,10 @@ function getSourcePath(source: string) {
* @returns 1-based lines and 0-based columns
*/
async function findOriginalSourcePositionAndContent(
sourceMap: RawSourceMap,
sourceMap: ModernSourceMapPayload,
position: { lineNumber: number | null; column: number | null }
): Promise<SourceAttributes | null> {
let consumer: BasicSourceMapConsumer
let consumer: SourceMapConsumer
try {
consumer = await new SourceMapConsumer(sourceMap)
} catch (cause) {
......@@ -309,7 +309,11 @@ async function getSource(
return {
type: 'file',
sourceMap: sourceMapPayload,
ignoredSources: getIgnoredSources(sourceMapPayload),
ignoredSources: getIgnoredSources(
// @ts-expect-error -- TODO: Support IndexSourceMap
sourceMapPayload
),
moduleURL: sourceURL,
}
}
......@@ -411,12 +415,6 @@ function getOriginalStackFrames({
)
}
function sourceMapIgnoreListsEverything(
sourceMap: RawSourceMap & { ignoreList?: number[] }
): boolean {
return sourceMap.sources.length === sourceMap.ignoreList?.length
}
async function getOriginalStackFrame({
isServer,
isEdgeServer,
......
/**
* https://tc39.es/source-map/#index-map
*/
interface IndexSourceMapSection {
offset: {
line: number
column: number
}
map: BasicSourceMapPayload
}
// TODO(veil): Upstream types
/** https://tc39.es/ecma426/#sec-index-source-map */
interface IndexSourceMap {
version: number
file: string
sections: IndexSourceMapSection[]
}
/** https://tc39.es/ecma426/#sec-source-map-format */
interface BasicSourceMapPayload {
version: number
// TODO: Move to https://github.com/jridgewell/sourcemaps which is actively maintained
/** WARNING: `file` is optional. */
file: string
sourceRoot?: string
// TODO: Move to https://github.com/jridgewell/sourcemaps which is actively maintained
/** WARNING: `sources[number]` can be `null`. */
sources: Array<string>
names: Array<string>
mappings: string
ignoreList?: number[]
}
export type ModernSourceMapPayload = BasicSourceMapPayload | IndexSourceMap
// TODO: This should take the BasicSourceMapPayload. Only the relevant section
// needs to ignore list everything making this check effectively O(1) multiplied
// by the complexity of finding the section.
export function sourceMapIgnoreListsEverything(
sourceMap: ModernSourceMapPayload
): boolean {
if ('sections' in sourceMap) {
return sourceMap.sections.every((section) => {
return sourceMapIgnoreListsEverything(section.map)
})
}
return (
sourceMap.ignoreList !== undefined &&
sourceMap.sources.length === sourceMap.ignoreList.length
)
}
/**
* Finds the sourcemap payload applicable to a given frame.
* Equal to the input unless an Index Source Map is used.
*/
export function findApplicableSourceMapPayload(
lineNumber: number,
columnNumber: number,
payload: ModernSourceMapPayload
): BasicSourceMapPayload | undefined {
if ('sections' in payload) {
// Sections must not overlap and must be sorted: https://tc39.es/source-map/#section-object
// Therefore the last section that has an offset less than or equal to the frame is the applicable one.
// TODO(veil): Binary search
let section: IndexSourceMapSection | undefined = payload.sections[0]
for (
let i = 0;
i < payload.sections.length &&
payload.sections[i].offset.line <= lineNumber &&
payload.sections[i].offset.column <= columnNumber;
i++
) {
section = payload.sections[i]
}
return section === undefined ? undefined : section.map
} else {
return payload
}
}
import {
findSourceMap as nativeFindSourceMap,
type SourceMapPayload,
} from 'module'
import { findSourceMap as nativeFindSourceMap } from 'module'
import * as path from 'path'
import * as url from 'url'
import type * as util from 'util'
import { SourceMapConsumer as SyncSourceMapConsumer } from 'next/dist/compiled/source-map'
import type { StackFrame } from 'next/dist/compiled/stacktrace-parser'
import {
type ModernSourceMapPayload,
findApplicableSourceMapPayload,
sourceMapIgnoreListsEverything,
} from './lib/source-maps'
import { parseStack } from './lib/parse-stack'
import { getOriginalCodeFrame } from '../next-devtools/server/shared'
import { workUnitAsyncStorage } from './app-render/work-unit-async-storage.external'
......@@ -27,30 +29,6 @@ export function setBundlerFindSourceMapImplementation(
bundlerFindSourceMapPayload = findSourceMapImplementation
}
/**
* https://tc39.es/source-map/#index-map
*/
interface IndexSourceMapSection {
offset: {
line: number
column: number
}
map: ModernRawSourceMap
}
// TODO(veil): Upstream types
interface IndexSourceMap {
version: number
file: string
sections: IndexSourceMapSection[]
}
interface ModernRawSourceMap extends SourceMapPayload {
ignoreList?: number[]
}
export type ModernSourceMapPayload = ModernRawSourceMap | IndexSourceMap
interface IgnoreableStackFrame extends StackFrame {
ignored: boolean
}
......@@ -116,37 +94,6 @@ function shouldIgnoreListOriginalFrame(file: string): boolean {
return file.includes('node_modules')
}
/**
* Finds the sourcemap payload applicable to a given frame.
* Equal to the input unless an Index Source Map is used.
*/
function findApplicableSourceMapPayload(
frame: StackFrame,
payload: ModernSourceMapPayload
): ModernRawSourceMap | undefined {
if ('sections' in payload) {
const frameLine = frame.lineNumber ?? 0
const frameColumn = frame.column ?? 0
// Sections must not overlap and must be sorted: https://tc39.es/source-map/#section-object
// Therefore the last section that has an offset less than or equal to the frame is the applicable one.
// TODO(veil): Binary search
let section: IndexSourceMapSection | undefined = payload.sections[0]
for (
let i = 0;
i < payload.sections.length &&
payload.sections[i].offset.line <= frameLine &&
payload.sections[i].offset.column <= frameColumn;
i++
) {
section = payload.sections[i]
}
return section === undefined ? undefined : section.map
} else {
return payload
}
}
interface SourcemappableStackFrame extends StackFrame {
file: NonNullable<StackFrame['file']>
}
......@@ -173,19 +120,6 @@ function createUnsourcemappedFrame(
}
}
function sourceMapIgnoreListsEverything(
sourceMap: ModernSourceMapPayload
): boolean {
if ('sections' in sourceMap) {
// If sections are present, the ignoreList is not used.
// This is because sections are used to ignore-list everything.
return sourceMap.sections.every((section) => {
return sourceMapIgnoreListsEverything(section.map)
})
}
return sourceMap.sources.length === sourceMap.ignoreList?.length
}
/**
* @param frame
* @param sourceMapCache
......@@ -290,7 +224,8 @@ function getSourcemappedFrameIfPossible(
}
const applicableSourceMap = findApplicableSourceMapPayload(
frame,
frame.lineNumber ?? 0,
frame.column ?? 0,
sourceMapPayload
)
// TODO(veil): Upstream a method to sourcemap consumer that immediately says if a frame is ignored or not.
......
diff --git a/module.d.ts b/module.d.ts
index 3b40061a67ccb5a65071d29a3a8d7134d8851158..d733cde7cee76a389379f187f08c455c1b67b1e8 100644
--- a/module.d.ts
+++ b/module.d.ts
@@ -49,7 +49,25 @@ declare module "module" {
* @return Returns `module.SourceMap` if a source map is found, `undefined` otherwise.
*/
function findSourceMap(path: string, error?: Error): SourceMap;
- interface SourceMapPayload {
+ /**
+ * https://tc39.es/source-map/#index-map
+ */
+ interface IndexSourceMapSection {
+ offset: {
+ line: number
+ column: number
+ }
+ map: BasicSourceMapPayload
+ }
+
+ // TODO(veil): Upstream types
+ /** https://tc39.es/ecma426/#sec-index-source-map */
+ interface IndexSourceMap {
+ version: number
+ file: string
+ sections: IndexSourceMapSection[]
+ }
+ interface BasicSourceMapPayload {
file: string;
version: number;
sources: string[];
@@ -58,6 +76,7 @@ declare module "module" {
mappings: string;
sourceRoot: string;
}
+ type SourceMapPayload = BasicSourceMapPayload | IndexSourceMap;
interface SourceMapping {
generatedLine: number;
generatedColumn: number;
This diff is collapsed.
import { nextTestSetup } from 'e2e-utils'
import { SourceMapPayload } from 'module'
describe('browser-chunks', () => {
const { next } = nextTestSetup({
......@@ -13,9 +12,7 @@ describe('browser-chunks', () => {
filename.endsWith('.js.map')
)
sources = sourcemaps.flatMap(
(sourcemap) => (JSON.parse(sourcemap) as SourceMapPayload).sources
)
sources = sourcemaps.flatMap((sourcemap) => JSON.parse(sourcemap).sources)
})
it('must not bundle any server modules into browser chunks', () => {
const serverSources = sources.filter(
......
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