Skip to content

Commit

Permalink
fix: revert "fix: js fallback sourcemap content should be using origi…
Browse files Browse the repository at this point in the history
…nal content (#15135)" (#15178)
  • Loading branch information
cpojer authored Nov 29, 2023
1 parent 4a111aa commit d2a2493
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 31 deletions.
11 changes: 1 addition & 10 deletions 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, getOriginalContent } from '../sourcemap'
import { applySourcemapIgnoreList } from '../sourcemap'
import { isHTMLProxy } from '../../plugins/html'
import {
DEP_VERSION_RE,
Expand Down Expand Up @@ -205,21 +205,12 @@ 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: 10 additions & 14 deletions packages/vite/src/node/server/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ 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 @@ -73,25 +71,23 @@ export function send(
}
// inject fallback sourcemap for js for improved debugging
// https://github.com/vitejs/vite/pull/13514#issuecomment-1592431496
// for { mappings: "" }, we don't inject fallback sourcemap
// because it indicates generating a sourcemap is meaningless
else if (type === 'js' && map == null) {
else if (type === 'js' && (!map || map.mappings !== '')) {
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)
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)
content = getCodeWithSourcemap(
type,
code,
ms.generateMap({
source: path.basename(urlWithoutTimestamp),
hires: 'boundary',
includeContent: true,
}),
)
}
}

Expand Down
7 changes: 0 additions & 7 deletions packages/vite/src/node/server/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ 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 d2a2493

Please sign in to comment.