Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert "fix: js fallback sourcemap content should be using original content (#15135)" #15178

Merged
merged 1 commit into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading