Skip to content

Commit e4c1c6d

Browse files
authoredSep 25, 2022
fix(css): missing css in lib mode (#10185)
1 parent 9acb839 commit e4c1c6d

File tree

6 files changed

+35
-1
lines changed

6 files changed

+35
-1
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
563563
// the legacy build should avoid inserting entry CSS modules here, they
564564
// will be collected into `chunk.viteMetadata.importedCss` and injected
565565
// later by the `'vite:build-html'` plugin into the `index.html`
566-
if (chunk.isEntry) {
566+
if (chunk.isEntry && !config.build.lib) {
567567
return null
568568
}
569569
chunkCSS = await finalizeCss(chunkCSS, true, config)

‎playground/vue-lib/__tests__/vue-lib.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ describe('vue component library', () => {
2222
expect(code).toContain('styleA') // styleA is used by CompA
2323
expect(code).not.toContain('styleB') // styleB is not used
2424
})
25+
26+
test('should inject css when cssCodeSplit = true', async () => {
27+
// Build lib
28+
const { output } = (
29+
await build({
30+
logLevel: 'silent',
31+
configFile: path.resolve(__dirname, '../vite.config.lib-css.ts')
32+
})
33+
)[0]
34+
expect(output[0].code).toContain('.card{padding:4rem}')
35+
})
2536
})

‎playground/vue-lib/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"dev-consumer": "vite --config ./vite.config.consumer.ts",
77
"build-lib": "vite build --config ./vite.config.lib.ts",
8+
"build-lib-css": "vite build --config ./vite.config.lib-css.ts",
89
"build-consumer": "vite build --config ./vite.config.consumer.ts"
910
},
1011
"dependencies": {
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.card {
2+
padding: 4rem;
3+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import './index.css'
2+
3+
export function setup() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import path from 'node:path'
2+
import { defineConfig } from 'vite'
3+
4+
export default defineConfig({
5+
root: __dirname,
6+
build: {
7+
outDir: 'dist/lib',
8+
cssCodeSplit: true,
9+
lib: {
10+
entry: path.resolve(__dirname, 'src-lib-css/index.ts'),
11+
name: 'index',
12+
formats: ['umd'],
13+
fileName: 'index.js'
14+
}
15+
}
16+
})

0 commit comments

Comments
 (0)
Please sign in to comment.