Skip to content

Commit

Permalink
fix(css): missing css in lib mode (#10185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Sep 25, 2022
1 parent 9acb839 commit e4c1c6d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -563,7 +563,7 @@ export function cssPostPlugin(config: ResolvedConfig): Plugin {
// the legacy build should avoid inserting entry CSS modules here, they
// will be collected into `chunk.viteMetadata.importedCss` and injected
// later by the `'vite:build-html'` plugin into the `index.html`
if (chunk.isEntry) {
if (chunk.isEntry && !config.build.lib) {
return null
}
chunkCSS = await finalizeCss(chunkCSS, true, config)
Expand Down
11 changes: 11 additions & 0 deletions playground/vue-lib/__tests__/vue-lib.spec.ts
Expand Up @@ -22,4 +22,15 @@ describe('vue component library', () => {
expect(code).toContain('styleA') // styleA is used by CompA
expect(code).not.toContain('styleB') // styleB is not used
})

test('should inject css when cssCodeSplit = true', async () => {
// Build lib
const { output } = (
await build({
logLevel: 'silent',
configFile: path.resolve(__dirname, '../vite.config.lib-css.ts')
})
)[0]
expect(output[0].code).toContain('.card{padding:4rem}')
})
})
1 change: 1 addition & 0 deletions playground/vue-lib/package.json
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev-consumer": "vite --config ./vite.config.consumer.ts",
"build-lib": "vite build --config ./vite.config.lib.ts",
"build-lib-css": "vite build --config ./vite.config.lib-css.ts",
"build-consumer": "vite build --config ./vite.config.consumer.ts"
},
"dependencies": {
Expand Down
3 changes: 3 additions & 0 deletions playground/vue-lib/src-lib-css/index.css
@@ -0,0 +1,3 @@
.card {
padding: 4rem;
}
3 changes: 3 additions & 0 deletions playground/vue-lib/src-lib-css/index.ts
@@ -0,0 +1,3 @@
import './index.css'

export function setup() {}
16 changes: 16 additions & 0 deletions playground/vue-lib/vite.config.lib-css.ts
@@ -0,0 +1,16 @@
import path from 'node:path'
import { defineConfig } from 'vite'

export default defineConfig({
root: __dirname,
build: {
outDir: 'dist/lib',
cssCodeSplit: true,
lib: {
entry: path.resolve(__dirname, 'src-lib-css/index.ts'),
name: 'index',
formats: ['umd'],
fileName: 'index.js'
}
}
})

0 comments on commit e4c1c6d

Please sign in to comment.