Skip to content

Commit a24b5e3

Browse files
sapphi-redpatak-dev
authored andcommitted
fix: add direct query to html-proxy css (fixes #8091) (#8094)
1 parent e5556ab commit a24b5e3

File tree

3 files changed

+49
-36
lines changed

3 files changed

+49
-36
lines changed

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

+16-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,22 @@ if (!isBuild) {
2727
}
2828
)
2929
const css = await res.text()
30-
const lines = css.split('\n')
31-
expect(lines[lines.length - 1].includes('/*')).toBe(false) // expect no sourcemap
30+
const map = extractSourcemap(css)
31+
expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(`
32+
Object {
33+
"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;",
34+
"sources": Array [
35+
"/root/linked.css",
36+
],
37+
"sourcesContent": Array [
38+
".linked {
39+
color: red;
40+
}
41+
",
42+
],
43+
"version": 3,
44+
}
45+
`)
3246
})
3347

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

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

+32-33
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ export function cssPlugin(config: ResolvedConfig): Plugin {
223223
const thisModule = moduleGraph.getModuleById(id)
224224
if (thisModule) {
225225
// CSS modules cannot self-accept since it exports values
226-
const isSelfAccepting = !modules && !inlineRE.test(id)
226+
const isSelfAccepting =
227+
!modules && !inlineRE.test(id) && !htmlProxyRE.test(id)
227228
if (deps) {
228229
// record deps in the module graph so edits to @import css can trigger
229230
// main import to hot update
@@ -301,7 +302,6 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
301302
return
302303
}
303304

304-
const isHTMLProxy = htmlProxyRE.test(id)
305305
const inlined = inlineRE.test(id)
306306
const modules = cssModulesCache.get(config)!.get(id)
307307

@@ -314,43 +314,41 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
314314
dataToEsm(modules, { namedExports: true, preferConst: true })
315315

316316
if (config.command === 'serve') {
317-
if (isDirectCSSRequest(id)) {
318-
return css
319-
} else {
320-
// server only
321-
if (options?.ssr) {
322-
return modulesCode || `export default ${JSON.stringify(css)}`
323-
}
324-
if (inlined) {
325-
return `export default ${JSON.stringify(css)}`
326-
}
327-
328-
let cssContent = css
317+
const getContentWithSourcemap = async (content: string) => {
329318
if (config.css?.devSourcemap) {
330319
const sourcemap = this.getCombinedSourcemap()
331320
await injectSourcesContent(sourcemap, cleanUrl(id), config.logger)
332-
cssContent = getCodeWithSourcemap('css', css, sourcemap)
333-
}
334-
335-
if (isHTMLProxy) {
336-
return cssContent
321+
return getCodeWithSourcemap('css', content, sourcemap)
337322
}
323+
return content
324+
}
338325

339-
return [
340-
`import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
341-
path.posix.join(config.base, CLIENT_PUBLIC_PATH)
342-
)}`,
343-
`const __vite__id = ${JSON.stringify(id)}`,
344-
`const __vite__css = ${JSON.stringify(cssContent)}`,
345-
`__vite__updateStyle(__vite__id, __vite__css)`,
346-
// css modules exports change on edit so it can't self accept
347-
`${
348-
modulesCode ||
349-
`import.meta.hot.accept()\nexport default __vite__css`
350-
}`,
351-
`import.meta.hot.prune(() => __vite__removeStyle(__vite__id))`
352-
].join('\n')
326+
if (isDirectCSSRequest(id)) {
327+
return await getContentWithSourcemap(css)
328+
}
329+
// server only
330+
if (options?.ssr) {
331+
return modulesCode || `export default ${JSON.stringify(css)}`
353332
}
333+
if (inlined) {
334+
return `export default ${JSON.stringify(css)}`
335+
}
336+
337+
const cssContent = await getContentWithSourcemap(css)
338+
return [
339+
`import { updateStyle as __vite__updateStyle, removeStyle as __vite__removeStyle } from ${JSON.stringify(
340+
path.posix.join(config.base, CLIENT_PUBLIC_PATH)
341+
)}`,
342+
`const __vite__id = ${JSON.stringify(id)}`,
343+
`const __vite__css = ${JSON.stringify(cssContent)}`,
344+
`__vite__updateStyle(__vite__id, __vite__css)`,
345+
// css modules exports change on edit so it can't self accept
346+
`${
347+
modulesCode ||
348+
`import.meta.hot.accept()\nexport default __vite__css`
349+
}`,
350+
`import.meta.hot.prune(() => __vite__removeStyle(__vite__id))`
351+
].join('\n')
354352
}
355353

356354
// build CSS handling ----------------------------------------------------
@@ -359,6 +357,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
359357
// cache css compile result to map
360358
// and then use the cache replace inline-style-flag when `generateBundle` in vite:build-html plugin
361359
const inlineCSS = inlineCSSRE.test(id)
360+
const isHTMLProxy = htmlProxyRE.test(id)
362361
const query = parseRequest(id)
363362
if (inlineCSS && isHTMLProxy) {
364363
addToHTMLProxyTransformResult(

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ const devHtmlHook: IndexHtmlTransformHook = async (
217217

218218
await Promise.all(
219219
styleUrl.map(async ({ start, end, code }, index) => {
220-
const url = `${proxyModulePath}?html-proxy&index=${index}.css`
220+
const url = `${proxyModulePath}?html-proxy&direct&index=${index}.css`
221221

222222
// ensure module in graph after successful load
223223
const mod = await moduleGraph.ensureEntryFromUrl(url, false)

0 commit comments

Comments
 (0)