Skip to content
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: don't inject CSS sourcemap for direct requests #13115

Merged
merged 4 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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