Skip to content

Commit

Permalink
refactor: remove the need for "processSourceMap" (#18187)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Sep 24, 2024
1 parent e59e2ca commit 08ff233
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
6 changes: 5 additions & 1 deletion docs/guide/api-environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,10 @@ export interface ModuleRunnerOptions {
```ts
export interface ModuleEvaluator {
/**
* Number of prefixed lines in the transformed code.
*/
startOffset?: number
/**
* Evaluate code that was transformed by Vite.
* @param context Function context
Expand All @@ -733,7 +737,7 @@ export interface ModuleEvaluator {
}
```
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.
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.
## RunnerTransport
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/module-runner/esmEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AsyncFunction } from '../shared/utils'
import {
AsyncFunction,
asyncFunctionDeclarationPaddingLineCount,
} from '../shared/utils'
import {
ssrDynamicImportKey,
ssrExportAllKey,
Expand All @@ -9,6 +12,8 @@ import {
import type { ModuleEvaluator, ModuleRunnerContext } from './types'

export class ESModulesEvaluator implements ModuleEvaluator {
startOffset = asyncFunctionDeclarationPaddingLineCount

async runInlinedModule(
context: ModuleRunnerContext,
code: string,
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ export class ModuleRunner {
? { externalize: url, type: 'builtin' }
: await this.transport.fetchModule(url, importer, {
cached: isCached,
startOffset: this.evaluator.startOffset,
})
) as ResolvedResult

Expand Down
5 changes: 5 additions & 0 deletions packages/vite/src/module-runner/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export interface ModuleRunnerContext {
}

export interface ModuleEvaluator {
/**
* Number of prefixed lines in the transformed code.
*/
startOffset?: number
/**
* Run code that was transformed by Vite.
* @param context Function context
Expand Down Expand Up @@ -138,6 +142,7 @@ export type FetchFunction = (

export interface FetchFunctionOptions {
cached?: boolean
startOffset?: number
}

export interface ModuleRunnerHmr {
Expand Down
15 changes: 1 addition & 14 deletions packages/vite/src/node/server/environments/nodeEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { ResolvedConfig } from '../../config'
import type { DevEnvironmentContext } from '../environment'
import { DevEnvironment } from '../environment'
import { asyncFunctionDeclarationPaddingLineCount } from '../../../shared/utils'

export function createNodeDevEnvironment(
name: string,
Expand All @@ -14,17 +13,5 @@ export function createNodeDevEnvironment(
)
}

return new DevEnvironment(name, config, {
...context,
runner: {
processSourceMap(map) {
// this assumes that "new AsyncFunction" is used to create the module
return Object.assign({}, map, {
mappings:
';'.repeat(asyncFunctionDeclarationPaddingLineCount) + map.mappings,
})
},
...context.runner,
},
})
return new DevEnvironment(name, config, context)
}
12 changes: 8 additions & 4 deletions packages/vite/src/node/ssr/fetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { normalizeResolvedIdToUrl } from '../plugins/importAnalysis'
export interface FetchModuleOptions {
cached?: boolean
inlineSourceMap?: boolean
processSourceMap?<T extends NonNullable<TransformResult['map']>>(map: T): T
startOffset?: number
}

/**
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function fetchModule(
}

if (options.inlineSourceMap !== false) {
result = inlineSourceMap(mod, result, options.processSourceMap)
result = inlineSourceMap(mod, result, options.startOffset)
}

// remove shebang
Expand All @@ -150,7 +150,7 @@ const OTHER_SOURCE_MAP_REGEXP = new RegExp(
function inlineSourceMap(
mod: EnvironmentModuleNode,
result: TransformResult,
processSourceMap?: FetchModuleOptions['processSourceMap'],
startOffset: number | undefined,
) {
const map = result.map
let code = result.code
Expand All @@ -167,7 +167,11 @@ function inlineSourceMap(
if (OTHER_SOURCE_MAP_REGEXP.test(code))
code = code.replace(OTHER_SOURCE_MAP_REGEXP, '')

const sourceMap = processSourceMap?.(map) || map
const sourceMap = startOffset
? Object.assign({}, map, {
mappings: ';'.repeat(startOffset) + map.mappings,
})
: map
result.code = `${code.trimEnd()}\n//# sourceURL=${
mod.id
}\n${MODULE_RUNNER_SOURCEMAPPING_SOURCE}\n//# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}\n`
Expand Down

0 comments on commit 08ff233

Please sign in to comment.