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): missing css in lib mode #10185

Merged
merged 3 commits into from Sep 25, 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
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'
}
}
})