Skip to content

Commit 08ff233

Browse files
authored
refactor: remove the need for "processSourceMap" (#18187)
1 parent e59e2ca commit 08ff233

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

docs/guide/api-environment.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,10 @@ export interface ModuleRunnerOptions {
714714
715715
```ts
716716
export interface ModuleEvaluator {
717+
/**
718+
* Number of prefixed lines in the transformed code.
719+
*/
720+
startOffset?: number
717721
/**
718722
* Evaluate code that was transformed by Vite.
719723
* @param context Function context
@@ -733,7 +737,7 @@ export interface ModuleEvaluator {
733737
}
734738
```
735739
736-
Vite exports `ESModulesEvaluator` that implements this interface by default. It uses `new AsyncFunction` to evaluate code, so if the code has inlined source map it should contain an [offset of 2 lines](https://tc39.es/ecma262/#sec-createdynamicfunction) to accommodate for new lines added. This is done automatically in the server node environment. If your runner implementation doesn't have this constraint, you should use `fetchModule` (exported from `vite`) directly.
740+
Vite exports `ESModulesEvaluator` that implements this interface by default. It uses `new AsyncFunction` to evaluate code, so if the code has inlined source map it should contain an [offset of 2 lines](https://tc39.es/ecma262/#sec-createdynamicfunction) to accommodate for new lines added. This is done automatically by the `ESModulesEvaluator`. Custom evaluators will not add additional lines.
737741
738742
## RunnerTransport
739743

packages/vite/src/module-runner/esmEvaluator.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { AsyncFunction } from '../shared/utils'
1+
import {
2+
AsyncFunction,
3+
asyncFunctionDeclarationPaddingLineCount,
4+
} from '../shared/utils'
25
import {
36
ssrDynamicImportKey,
47
ssrExportAllKey,
@@ -9,6 +12,8 @@ import {
912
import type { ModuleEvaluator, ModuleRunnerContext } from './types'
1013

1114
export class ESModulesEvaluator implements ModuleEvaluator {
15+
startOffset = asyncFunctionDeclarationPaddingLineCount
16+
1217
async runInlinedModule(
1318
context: ModuleRunnerContext,
1419
code: string,

packages/vite/src/module-runner/runner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ export class ModuleRunner {
265265
? { externalize: url, type: 'builtin' }
266266
: await this.transport.fetchModule(url, importer, {
267267
cached: isCached,
268+
startOffset: this.evaluator.startOffset,
268269
})
269270
) as ResolvedResult
270271

packages/vite/src/module-runner/types.ts

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ export interface ModuleRunnerContext {
4646
}
4747

4848
export interface ModuleEvaluator {
49+
/**
50+
* Number of prefixed lines in the transformed code.
51+
*/
52+
startOffset?: number
4953
/**
5054
* Run code that was transformed by Vite.
5155
* @param context Function context
@@ -138,6 +142,7 @@ export type FetchFunction = (
138142

139143
export interface FetchFunctionOptions {
140144
cached?: boolean
145+
startOffset?: number
141146
}
142147

143148
export interface ModuleRunnerHmr {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ResolvedConfig } from '../../config'
22
import type { DevEnvironmentContext } from '../environment'
33
import { DevEnvironment } from '../environment'
4-
import { asyncFunctionDeclarationPaddingLineCount } from '../../../shared/utils'
54

65
export function createNodeDevEnvironment(
76
name: string,
@@ -14,17 +13,5 @@ export function createNodeDevEnvironment(
1413
)
1514
}
1615

17-
return new DevEnvironment(name, config, {
18-
...context,
19-
runner: {
20-
processSourceMap(map) {
21-
// this assumes that "new AsyncFunction" is used to create the module
22-
return Object.assign({}, map, {
23-
mappings:
24-
';'.repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings,
25-
})
26-
},
27-
...context.runner,
28-
},
29-
})
16+
return new DevEnvironment(name, config, context)
3017
}

packages/vite/src/node/ssr/fetchModule.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { normalizeResolvedIdToUrl } from '../plugins/importAnalysis'
1515
export interface FetchModuleOptions {
1616
cached?: boolean
1717
inlineSourceMap?: boolean
18-
processSourceMap?<T extends NonNullable<TransformResult['map']>>(map: T): T
18+
startOffset?: number
1919
}
2020

2121
/**
@@ -127,7 +127,7 @@ export async function fetchModule(
127127
}
128128

129129
if (options.inlineSourceMap !== false) {
130-
result = inlineSourceMap(mod, result, options.processSourceMap)
130+
result = inlineSourceMap(mod, result, options.startOffset)
131131
}
132132

133133
// remove shebang
@@ -150,7 +150,7 @@ const OTHER_SOURCE_MAP_REGEXP = new RegExp(
150150
function inlineSourceMap(
151151
mod: EnvironmentModuleNode,
152152
result: TransformResult,
153-
processSourceMap?: FetchModuleOptions['processSourceMap'],
153+
startOffset: number | undefined,
154154
) {
155155
const map = result.map
156156
let code = result.code
@@ -167,7 +167,11 @@ function inlineSourceMap(
167167
if (OTHER_SOURCE_MAP_REGEXP.test(code))
168168
code = code.replace(OTHER_SOURCE_MAP_REGEXP, '')
169169

170-
const sourceMap = processSourceMap?.(map) || map
170+
const sourceMap = startOffset
171+
? Object.assign({}, map, {
172+
mappings: ';'.repeat(startOffset) + map.mappings,
173+
})
174+
: map
171175
result.code = `${code.trimEnd()}\n//# sourceURL=${
172176
mod.id
173177
}\n${MODULE_RUNNER_SOURCEMAPPING_SOURCE}\n//# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}\n`

0 commit comments

Comments
 (0)