Skip to content

Commit 7d80a47

Browse files
fix: don't inject CSS sourcemap for direct requests (#13115)
Co-authored-by: sapphi-red <[email protected]>
1 parent 88c855e commit 7d80a47

File tree

4 files changed

+20
-19
lines changed

4 files changed

+20
-19
lines changed

packages/vite/src/node/plugins/css.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
404404
}
405405

406406
if (isDirectCSSRequest(id)) {
407-
return await getContentWithSourcemap(css)
407+
return null
408408
}
409409
// server only
410410
if (options?.ssr) {

packages/vite/src/node/server/middlewares/indexHtml.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {
3636
wrapId,
3737
} from '../../utils'
3838
import { checkPublicFile } from '../../plugins/asset'
39+
import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap'
3940

4041
interface AssetNode {
4142
start: number
@@ -268,7 +269,22 @@ const devHtmlHook: IndexHtmlTransformHook = async (
268269
ensureWatchedFile(watcher, mod.file, config.root)
269270

270271
const result = await server!.pluginContainer.transform(code, mod.id!)
271-
s.overwrite(start, end, result?.code || '')
272+
let content = ''
273+
if (result) {
274+
if (result.map) {
275+
if (result.map.mappings && !result.map.sourcesContent) {
276+
await injectSourcesContent(
277+
result.map,
278+
proxyModulePath,
279+
config.logger,
280+
)
281+
}
282+
content = getCodeWithSourcemap('css', result.code, result.map)
283+
} else {
284+
content = result.code
285+
}
286+
}
287+
s.overwrite(start, end, content)
272288
}),
273289
)
274290

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export interface PluginContainer {
117117
inMap?: SourceDescription['map']
118118
ssr?: boolean
119119
},
120-
): Promise<SourceDescription | null>
120+
): Promise<{ code: string; map: SourceMap | null }>
121121
load(
122122
id: string,
123123
options?: {

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

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,7 @@ describe.runIf(isServe)('serve', () => {
3737
},
3838
)
3939
const css = await res.text()
40-
const map = extractSourcemap(css)
41-
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
42-
{
43-
"mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACT,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACb,CAAC;",
44-
"sources": [
45-
"/root/linked.css",
46-
],
47-
"sourcesContent": [
48-
".linked {
49-
color: red;
50-
}
51-
",
52-
],
53-
"version": 3,
54-
}
55-
`)
40+
expect(css).not.toContain('sourceMappingURL')
5641
})
5742

5843
test('linked css with import', async () => {

0 commit comments

Comments
 (0)