Skip to content

Commit

Permalink
fix(css): include inline css module in bundle when ssr
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 19, 2022
1 parent 7f96b26 commit 5b6a950
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -300,8 +300,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {

const inlined = inlineRE.test(id)
const modules = cssModulesCache.get(config)!.get(id)

// #6984, #7552
// `foo.module.css` => modulesCode
// `foo.module.css?inline` => cssContent
const modulesCode =
modules && dataToEsm(modules, { namedExports: true, preferConst: true })
modules &&
!inlined &&
dataToEsm(modules, { namedExports: true, preferConst: true })

if (config.command === 'serve') {
if (isDirectCSSRequest(id)) {
Expand Down Expand Up @@ -360,12 +366,14 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {

let code: string
if (usedRE.test(id)) {
if (inlined) {
code = `export default ${JSON.stringify(
await minifyCSS(css, config)
)}`
if (modulesCode) {
code = modulesCode
} else {
code = modulesCode || `export default ${JSON.stringify(css)}`
let content = css
if (inlined) {
content = await minifyCSS(content, config)
}
code = `export default ${JSON.stringify(content)}`
}
} else {
code = `export default ''`
Expand Down

0 comments on commit 5b6a950

Please sign in to comment.