Skip to content

Commit 8a19d59

Browse files
BenceSzalaisapphi-red
authored and
Clay Smith
committed
fix: missing js sourcemaps with rewritten imports broke debugging (vitejs#7767) (vitejs#9476)
Co-authored-by: sapphi-red <green@sapphi.red>
1 parent 4fe0a78 commit 8a19d59

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import getEtag from 'etag'
55
import convertSourceMap from 'convert-source-map'
66
import type { SourceDescription, SourceMap } from 'rollup'
77
import colors from 'picocolors'
8+
import MagicString from 'magic-string'
89
import type { ViteDevServer } from '..'
910
import {
1011
blankReplacer,
@@ -18,6 +19,8 @@ import {
1819
} from '../utils'
1920
import { checkPublicFile } from '../plugins/asset'
2021
import { getDepsOptimizer } from '../optimizer'
22+
import { isCSSRequest } from '../plugins/css'
23+
import { SPECIAL_QUERY_RE } from '../constants'
2124
import { injectSourcesContent } from './sourcemap'
2225
import { isFileServingAllowed } from './middlewares/static'
2326

@@ -254,11 +257,26 @@ async function loadAndTransform(
254257
isDebug && debugTransform(`${timeFrom(transformStart)} ${prettyUrl}`)
255258
code = transformResult.code!
256259
map = transformResult.map
260+
261+
// To enable IDE debugging, add a minimal sourcemap for modified JS files without one
262+
if (
263+
!map &&
264+
mod.file &&
265+
mod.type === 'js' &&
266+
code !== originalCode &&
267+
!(isCSSRequest(id) && !SPECIAL_QUERY_RE.test(id)) // skip CSS : #9914
268+
) {
269+
map = new MagicString(code).generateMap({ source: mod.file })
270+
}
257271
}
258272

259273
if (map && mod.file) {
260274
map = (typeof map === 'string' ? JSON.parse(map) : map) as SourceMap
261-
if (map.mappings && !map.sourcesContent) {
275+
if (
276+
map.mappings &&
277+
(!map.sourcesContent ||
278+
(map.sourcesContent as Array<string | null>).includes(null))
279+
) {
262280
await injectSourcesContent(map, mod.file, logger)
263281
}
264282
}

playground/js-sourcemap/__tests__/js-sourcemap.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,34 @@ import {
99
} from '~utils'
1010

1111
if (!isBuild) {
12-
test('js', async () => {
12+
test('js without import', async () => {
1313
const res = await page.request.get(new URL('./foo.js', page.url()).href)
1414
const js = await res.text()
1515
const lines = js.split('\n')
1616
expect(lines[lines.length - 1].includes('//')).toBe(false) // expect no sourcemap
1717
})
1818

19+
test('js', async () => {
20+
const res = await page.request.get(new URL('./qux.js', page.url()).href)
21+
const js = await res.text()
22+
const map = extractSourcemap(js)
23+
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
24+
{
25+
"mappings": "AAAA;AACA;AACA;",
26+
"sources": [
27+
"/root/qux.js",
28+
],
29+
"sourcesContent": [
30+
"import { foo } from './foo'
31+
32+
export const qux = 'qux'
33+
",
34+
],
35+
"version": 3,
36+
}
37+
`)
38+
})
39+
1940
test('ts', async () => {
2041
const res = await page.request.get(new URL('./bar.ts', page.url()).href)
2142
const js = await res.text()

playground/js-sourcemap/qux.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { foo } from './foo'
2+
3+
export const qux = 'qux'

0 commit comments

Comments
 (0)