Skip to content

Commit

Permalink
fix: stack track point to incorrect file. (#3004)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChenKS12138 committed Apr 5, 2023
1 parent 2b74e6f commit cf1b6fd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/vite-node/src/server.ts
Expand Up @@ -192,7 +192,9 @@ export class ViteNodeServer {
}

protected async processTransformResult(result: TransformResult) {
return withInlineSourcemap(result)
return withInlineSourcemap(result, {
root: this.server.config.root,
})
}

private async _transformRequest(id: string, customTransformMode?: 'web' | 'ssr') {
Expand Down
12 changes: 11 additions & 1 deletion packages/vite-node/src/source-map.ts
@@ -1,6 +1,7 @@
import type { TransformResult } from 'vite'
import type { EncodedSourceMap } from '@jridgewell/trace-mapping'
import { install } from './source-map-handler'
import { toFilePath } from './utils'

interface InstallSourceMapSupportOptions {
getSourceMap: (source: string) => EncodedSourceMap | null | undefined
Expand All @@ -13,13 +14,22 @@ const VITE_NODE_SOURCEMAPPING_SOURCE = '//# sourceMappingSource=vite-node'
const VITE_NODE_SOURCEMAPPING_URL = `${SOURCEMAPPING_URL}=data:application/json;charset=utf-8`
const VITE_NODE_SOURCEMAPPING_REGEXP = new RegExp(`//# ${VITE_NODE_SOURCEMAPPING_URL};base64,(.+)`)

export function withInlineSourcemap(result: TransformResult) {
export function withInlineSourcemap(result: TransformResult, options: {
root: string // project root path of this resource
}) {
const map = result.map
let code = result.code

if (!map || code.includes(VITE_NODE_SOURCEMAPPING_SOURCE))
return result

// sources path from `ViteDevServer` may be not a valid filesystem path (eg. /src/main.js),
// so we try to convert them to valid filesystem path
map.sources = map.sources.map((one) => {
const { exists, path } = toFilePath(one, options.root)
return exists ? path : one
})

// to reduce the payload size, we only inline vite node source map, because it's also the only one we use
const OTHER_SOURCE_MAP_REGEXP = new RegExp(`//# ${SOURCEMAPPING_URL}=data:application/json[^,]+base64,(.+)`, 'g')
while (OTHER_SOURCE_MAP_REGEXP.test(code))
Expand Down

0 comments on commit cf1b6fd

Please sign in to comment.