@@ -5,6 +5,7 @@ import getEtag from 'etag'
5
5
import convertSourceMap from 'convert-source-map'
6
6
import type { SourceDescription , SourceMap } from 'rollup'
7
7
import colors from 'picocolors'
8
+ import MagicString from 'magic-string'
8
9
import type { ViteDevServer } from '..'
9
10
import {
10
11
blankReplacer ,
@@ -18,6 +19,8 @@ import {
18
19
} from '../utils'
19
20
import { checkPublicFile } from '../plugins/asset'
20
21
import { getDepsOptimizer } from '../optimizer'
22
+ import { isCSSRequest } from '../plugins/css'
23
+ import { SPECIAL_QUERY_RE } from '../constants'
21
24
import { injectSourcesContent } from './sourcemap'
22
25
import { isFileServingAllowed } from './middlewares/static'
23
26
@@ -254,11 +257,26 @@ async function loadAndTransform(
254
257
isDebug && debugTransform ( `${ timeFrom ( transformStart ) } ${ prettyUrl } ` )
255
258
code = transformResult . code !
256
259
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
+ }
257
271
}
258
272
259
273
if ( map && mod . file ) {
260
274
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
+ ) {
262
280
await injectSourcesContent ( map , mod . file , logger )
263
281
}
264
282
}
0 commit comments