fix(css): include inline css module in bundle (#7591)

This commit is contained in:
翠 / green 2022-04-04 19:21:02 +09:00 committed by GitHub
parent cf59005a79
commit 45b9273d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 7 deletions

View File

@ -242,6 +242,11 @@ test('css modules w/ sass', async () => {
await untilUpdated(() => getColor(imported), 'blue')
})
test('inline css modules', async () => {
const css = await page.textContent('.modules-inline')
expect(css).toMatch(/\.inline-module__apply-color-inline___[\w-]{5}/)
})
test('@import dependency w/ style entry', async () => {
expect(await getColor('.css-dep')).toBe('purple')
})

View File

@ -89,6 +89,9 @@
</p>
<pre class="path-resolved-modules-code"></pre>
<p>Inline CSS module:</p>
<pre class="modules-inline"></pre>
<p class="css-dep">
@import dependency w/ style enrtrypoints: this should be purple
</p>

View File

@ -0,0 +1,3 @@
.apply-color-inline {
color: turquoise;
}

View File

@ -38,6 +38,9 @@ text(
JSON.stringify(composesPathResolvingMod, null, 2)
)
import inlineMod from './inline.module.css?inline'
text('.modules-inline', inlineMod)
import './dep.css'
import './glob-dep.css'

View File

@ -358,14 +358,21 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
styles.set(id, css)
}
let code: string
if (usedRE.test(id)) {
if (inlined) {
code = `export default ${JSON.stringify(
await minifyCSS(css, config)
)}`
} else {
code = modulesCode || `export default ${JSON.stringify(css)}`
}
} else {
code = `export default ''`
}
return {
code:
modulesCode ||
(usedRE.test(id)
? `export default ${JSON.stringify(
inlined ? await minifyCSS(css, config) : css
)}`
: `export default ''`),
code,
map: { mappings: '' },
// avoid the css module from being tree-shaken so that we can retrieve
// it in renderChunk()