From cf1b6fd33ff35ac4966b3755b579b2ee9e98df67 Mon Sep 17 00:00:00 2001 From: ChenKS12138 Date: Wed, 5 Apr 2023 20:08:51 +0800 Subject: [PATCH] fix: stack track point to incorrect file. (#3004) --- packages/vite-node/src/server.ts | 4 +++- packages/vite-node/src/source-map.ts | 12 +++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/vite-node/src/server.ts b/packages/vite-node/src/server.ts index b50b3cf049c8..fefe8e8bf805 100644 --- a/packages/vite-node/src/server.ts +++ b/packages/vite-node/src/server.ts @@ -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') { diff --git a/packages/vite-node/src/source-map.ts b/packages/vite-node/src/source-map.ts index 581c197f0d6d..4ab6c0065538 100644 --- a/packages/vite-node/src/source-map.ts +++ b/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 @@ -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))