-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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: breakpoints in JS not working #13514
Changes from all commits
60f1552
95ffb78
06c3d3b
1690dc3
cc70dd4
9d27b56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,8 @@ import type { | |
} from 'node:http' | ||
import getEtag from 'etag' | ||
import type { SourceMap } from 'rollup' | ||
import MagicString from 'magic-string' | ||
import { removeTimestampQuery } from '../utils' | ||
import { getCodeWithSourcemap } from './sourcemap' | ||
|
||
const alias: Record<string, string | undefined> = { | ||
|
@@ -18,7 +20,7 @@ export interface SendOptions { | |
etag?: string | ||
cacheControl?: string | ||
headers?: OutgoingHttpHeaders | ||
map?: SourceMap | null | ||
map?: SourceMap | { mappings: '' } | null | ||
} | ||
|
||
export function send( | ||
|
@@ -56,10 +58,20 @@ export function send( | |
} | ||
|
||
// inject source map reference | ||
if (map && map.mappings) { | ||
if (map && 'version' in map && map.mappings) { | ||
if (type === 'js' || type === 'css') { | ||
content = getCodeWithSourcemap(type, content.toString(), map) | ||
} | ||
} else { | ||
if (type === 'js' && (!map || map.mappings !== '')) { | ||
const urlWithoutTimestamp = removeTimestampQuery(req.url!) | ||
const ms = new MagicString(content.toString()) | ||
content = getCodeWithSourcemap( | ||
type, | ||
content.toString(), | ||
ms.generateMap({ source: urlWithoutTimestamp, hires: 'boundary' }), | ||
) | ||
} | ||
Comment on lines
+66
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part is the main part of the change. Other changes are to pass |
||
} | ||
|
||
res.statusCode = 200 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ const debugCache = createDebugger('vite:cache') | |
|
||
export interface TransformResult { | ||
code: string | ||
map: SourceMap | null | ||
map: SourceMap | { mappings: '' } | null | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is a breaking change. |
||
etag?: string | ||
deps?: string[] | ||
dynamicDeps?: string[] | ||
|
@@ -286,15 +286,23 @@ async function loadAndTransform( | |
map = transformResult.map | ||
} | ||
|
||
if (map && mod.file) { | ||
map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap | ||
if (map.mappings) { | ||
await injectSourcesContent(map, mod.file, logger) | ||
let normalizedMap: SourceMap | { mappings: '' } | null | ||
if (typeof map === 'string') { | ||
normalizedMap = JSON.parse(map) | ||
} else if (map) { | ||
normalizedMap = map as SourceMap | { mappings: '' } | ||
} else { | ||
normalizedMap = null | ||
} | ||
|
||
if (normalizedMap && 'version' in normalizedMap && mod.file) { | ||
if (normalizedMap.mappings) { | ||
await injectSourcesContent(normalizedMap, mod.file, logger) | ||
} | ||
|
||
const sourcemapPath = `${mod.file}.map` | ||
applySourcemapIgnoreList( | ||
map, | ||
normalizedMap, | ||
sourcemapPath, | ||
config.server.sourcemapIgnoreList, | ||
logger, | ||
|
@@ -303,16 +311,16 @@ async function loadAndTransform( | |
if (path.isAbsolute(mod.file)) { | ||
for ( | ||
let sourcesIndex = 0; | ||
sourcesIndex < map.sources.length; | ||
sourcesIndex < normalizedMap.sources.length; | ||
++sourcesIndex | ||
) { | ||
const sourcePath = map.sources[sourcesIndex] | ||
const sourcePath = normalizedMap.sources[sourcesIndex] | ||
if (sourcePath) { | ||
// Rewrite sources to relative paths to give debuggers the chance | ||
// to resolve and display them in a meaningful way (rather than | ||
// with absolute paths). | ||
if (path.isAbsolute(sourcePath)) { | ||
map.sources[sourcesIndex] = path.relative( | ||
normalizedMap.sources[sourcesIndex] = path.relative( | ||
path.dirname(mod.file), | ||
sourcePath, | ||
) | ||
|
@@ -326,12 +334,12 @@ async function loadAndTransform( | |
|
||
const result = | ||
ssr && !server.config.experimental.skipSsrTransform | ||
? await server.ssrTransform(code, map as SourceMap, url, originalCode) | ||
? await server.ssrTransform(code, normalizedMap, url, originalCode) | ||
: ({ | ||
code, | ||
map, | ||
map: normalizedMap, | ||
etag: getEtag(code, { weak: true }), | ||
} as TransformResult) | ||
} satisfies TransformResult) | ||
|
||
// Only cache the result if the module wasn't invalidated while it was | ||
// being processed, so it is re-processed next time if it is stale | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part was not correct.
{ mappings: '' }
is different fromnull
. So it shouldn't be changed tonull
.For example, if
this.sourcemapChain
is[{ mappings: '' }, validSourcemap]
, the combined result should be{ mappings: '' }
instead ofvalidSourcemap
.