@@ -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 (
0 commit comments