Skip to content

Commit

Permalink
fix: don't inject CSS sourcemap for direct requests (#13115)
Browse files Browse the repository at this point in the history
Co-authored-by: sapphi-red <green@sapphi.red>
  • Loading branch information
ArnaudBarre and sapphi-red committed May 15, 2023
1 parent 88c855e commit 7d80a47
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -404,7 +404,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
}

if (isDirectCSSRequest(id)) {
return await getContentWithSourcemap(css)
return null
}
// server only
if (options?.ssr) {
Expand Down
18 changes: 17 additions & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Expand Up @@ -36,6 +36,7 @@ import {
wrapId,
} from '../../utils'
import { checkPublicFile } from '../../plugins/asset'
import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap'

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

const result = await server!.pluginContainer.transform(code, mod.id!)
s.overwrite(start, end, result?.code || '')
let content = ''
if (result) {
if (result.map) {
if (result.map.mappings && !result.map.sourcesContent) {
await injectSourcesContent(
result.map,
proxyModulePath,
config.logger,
)
}
content = getCodeWithSourcemap('css', result.code, result.map)
} else {
content = result.code
}
}
s.overwrite(start, end, content)
}),
)

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/pluginContainer.ts
Expand Up @@ -117,7 +117,7 @@ export interface PluginContainer {
inMap?: SourceDescription['map']
ssr?: boolean
},
): Promise<SourceDescription | null>
): Promise<{ code: string; map: SourceMap | null }>
load(
id: string,
options?: {
Expand Down
17 changes: 1 addition & 16 deletions playground/css-sourcemap/__tests__/css-sourcemap.spec.ts
Expand Up @@ -37,22 +37,7 @@ describe.runIf(isServe)('serve', () => {
},
)
const css = await res.text()
const map = extractSourcemap(css)
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
{
"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;",
"sources": [
"/root/linked.css",
],
"sourcesContent": [
".linked {
color: red;
}
",
],
"version": 3,
}
`)
expect(css).not.toContain('sourceMappingURL')
})

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

0 comments on commit 7d80a47

Please sign in to comment.