Skip to content

Commit

Permalink
fix: js fallback sourcemap content should be using original content (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red authored Nov 24, 2023
1 parent 0654d1b commit 227d56d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
11 changes: 10 additions & 1 deletion packages/vite/src/node/server/middlewares/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '../../utils'
import { send } from '../send'
import { ERR_LOAD_URL, transformRequest } from '../transformRequest'
import { applySourcemapIgnoreList } from '../sourcemap'
import { applySourcemapIgnoreList, getOriginalContent } from '../sourcemap'
import { isHTMLProxy } from '../../plugins/html'
import {
DEP_VERSION_RE,
Expand Down Expand Up @@ -205,12 +205,21 @@ export function transformMiddleware(
const type = isDirectCSSRequest(url) ? 'css' : 'js'
const isDep =
DEP_VERSION_RE.test(url) || depsOptimizer?.isOptimizedDepUrl(url)
let originalContent: string | undefined
if (type === 'js' && result.map == null) {
const filepath = (
await server.moduleGraph.getModuleByUrl(url, false)
)?.file
originalContent =
filepath != null ? await getOriginalContent(filepath) : undefined
}
return send(req, res, result.code, type, {
etag: result.etag,
// allow browser to cache npm deps!
cacheControl: isDep ? 'max-age=31536000,immutable' : 'no-cache',
headers: server.config.server.headers,
map: result.map,
originalContent,
})
}
}
Expand Down
24 changes: 14 additions & 10 deletions packages/vite/src/node/server/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface SendOptions {
cacheControl?: string
headers?: OutgoingHttpHeaders
map?: SourceMap | { mappings: '' } | null
/** only used when type === 'js' && map == null (when the fallback sourcemap is used) */
originalContent?: string
}

export function send(
Expand Down Expand Up @@ -71,23 +73,25 @@ export function send(
}
// inject fallback sourcemap for js for improved debugging
// https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
else if (type === 'js' && (!map || map.mappings !== '')) {
// for { mappings: "" }, we don't inject fallback sourcemap
// because it indicates generating a sourcemap is meaningless
else if (type === 'js' && map == null) {
const code = content.toString()
// if the code has existing inline sourcemap, assume it's correct and skip
if (convertSourceMap.mapFileCommentRegex.test(code)) {
debug?.(`Skipped injecting fallback sourcemap for ${req.url}`)
} else {
const urlWithoutTimestamp = removeTimestampQuery(req.url!)
const ms = new MagicString(code)
content = getCodeWithSourcemap(
type,
code,
ms.generateMap({
source: path.basename(urlWithoutTimestamp),
hires: 'boundary',
includeContent: true,
}),
)
const map = ms.generateMap({
source: path.basename(urlWithoutTimestamp),
hires: 'boundary',
includeContent: !options.originalContent,
})
if (options.originalContent != null) {
map.sourcesContent = [options.originalContent]
}
content = getCodeWithSourcemap(type, code, map)
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ export async function injectSourcesContent(
}
}

export async function getOriginalContent(
filepath: string,
): Promise<string | undefined> {
if (virtualSourceRE.test(filepath)) return undefined
return await fsp.readFile(filepath, 'utf-8').catch(() => undefined)
}

export function genSourceMapUrl(map: SourceMap | string): string {
if (typeof map !== 'string') {
map = JSON.stringify(map)
Expand Down

0 comments on commit 227d56d

Please sign in to comment.