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(css): include inline css module in bundle (fix #6984,#7552) #7591

Merged
merged 1 commit into from Apr 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/playground/css/__tests__/css.spec.ts
Expand Up @@ -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')
})
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/index.html
Expand Up @@ -89,6 +89,9 @@ <h1>CSS</h1>
</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>
Expand Down
3 changes: 3 additions & 0 deletions packages/playground/css/inline.module.css
@@ -0,0 +1,3 @@
.apply-color-inline {
color: turquoise;
}
3 changes: 3 additions & 0 deletions packages/playground/css/main.js
Expand Up @@ -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'

Expand Down
21 changes: 14 additions & 7 deletions packages/vite/src/node/plugins/css.ts
Expand Up @@ -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()
Expand Down