Skip to content

Commit 227d56d

Browse files
authored
fix: js fallback sourcemap content should be using original content (#15135)
1 parent 0654d1b commit 227d56d

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

packages/vite/src/node/server/middlewares/transform.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from '../../utils'
2121
import { send } from '../send'
2222
import { ERR_LOAD_URL, transformRequest } from '../transformRequest'
23-
import { applySourcemapIgnoreList } from '../sourcemap'
23+
import { applySourcemapIgnoreList, getOriginalContent } from '../sourcemap'
2424
import { isHTMLProxy } from '../../plugins/html'
2525
import {
2626
DEP_VERSION_RE,
@@ -205,12 +205,21 @@ export function transformMiddleware(
205205
const type = isDirectCSSRequest(url) ? 'css' : 'js'
206206
const isDep =
207207
DEP_VERSION_RE.test(url) || depsOptimizer?.isOptimizedDepUrl(url)
208+
let originalContent: string | undefined
209+
if (type === 'js' && result.map == null) {
210+
const filepath = (
211+
await server.moduleGraph.getModuleByUrl(url, false)
212+
)?.file
213+
originalContent =
214+
filepath != null ? await getOriginalContent(filepath) : undefined
215+
}
208216
return send(req, res, result.code, type, {
209217
etag: result.etag,
210218
// allow browser to cache npm deps!
211219
cacheControl: isDep ? 'max-age=31536000,immutable' : 'no-cache',
212220
headers: server.config.server.headers,
213221
map: result.map,
222+
originalContent,
214223
})
215224
}
216225
}

packages/vite/src/node/server/send.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export interface SendOptions {
2727
cacheControl?: string
2828
headers?: OutgoingHttpHeaders
2929
map?: SourceMap | { mappings: '' } | null
30+
/** only used when type === 'js' && map == null (when the fallback sourcemap is used) */
31+
originalContent?: string
3032
}
3133

3234
export function send(
@@ -71,23 +73,25 @@ export function send(
7173
}
7274
// inject fallback sourcemap for js for improved debugging
7375
// https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
74-
else if (type === 'js' && (!map || map.mappings !== '')) {
76+
// for { mappings: "" }, we don't inject fallback sourcemap
77+
// because it indicates generating a sourcemap is meaningless
78+
else if (type === 'js' && map == null) {
7579
const code = content.toString()
7680
// if the code has existing inline sourcemap, assume it's correct and skip
7781
if (convertSourceMap.mapFileCommentRegex.test(code)) {
7882
debug?.(`Skipped injecting fallback sourcemap for ${req.url}`)
7983
} else {
8084
const urlWithoutTimestamp = removeTimestampQuery(req.url!)
8185
const ms = new MagicString(code)
82-
content = getCodeWithSourcemap(
83-
type,
84-
code,
85-
ms.generateMap({
86-
source: path.basename(urlWithoutTimestamp),
87-
hires: 'boundary',
88-
includeContent: true,
89-
}),
90-
)
86+
const map = ms.generateMap({
87+
source: path.basename(urlWithoutTimestamp),
88+
hires: 'boundary',
89+
includeContent: !options.originalContent,
90+
})
91+
if (options.originalContent != null) {
92+
map.sourcesContent = [options.originalContent]
93+
}
94+
content = getCodeWithSourcemap(type, code, map)
9195
}
9296
}
9397

packages/vite/src/node/server/sourcemap.ts

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ export async function injectSourcesContent(
6565
}
6666
}
6767

68+
export async function getOriginalContent(
69+
filepath: string,
70+
): Promise<string | undefined> {
71+
if (virtualSourceRE.test(filepath)) return undefined
72+
return await fsp.readFile(filepath, 'utf-8').catch(() => undefined)
73+
}
74+
6875
export function genSourceMapUrl(map: SourceMap | string): string {
6976
if (typeof map !== 'string') {
7077
map = JSON.stringify(map)

0 commit comments

Comments
 (0)