From e2cdb2827f9a9cb5a192c41f686e66d09d638797 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sun, 25 Sep 2022 21:46:49 +0800 Subject: [PATCH] fix(css): missing css in lib mode (#10185) --- packages/vite/src/node/plugins/css.ts | 2 +- playground/vue-lib/__tests__/vue-lib.spec.ts | 11 +++++++++++ playground/vue-lib/package.json | 1 + playground/vue-lib/src-lib-css/index.css | 3 +++ playground/vue-lib/src-lib-css/index.ts | 3 +++ playground/vue-lib/vite.config.lib-css.ts | 16 ++++++++++++++++ 6 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 playground/vue-lib/src-lib-css/index.css create mode 100644 playground/vue-lib/src-lib-css/index.ts create mode 100644 playground/vue-lib/vite.config.lib-css.ts diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index aebb3286584662..19cfcb2d0188f4 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -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) diff --git a/playground/vue-lib/__tests__/vue-lib.spec.ts b/playground/vue-lib/__tests__/vue-lib.spec.ts index 64f318656ca10e..50b554d63f9f76 100644 --- a/playground/vue-lib/__tests__/vue-lib.spec.ts +++ b/playground/vue-lib/__tests__/vue-lib.spec.ts @@ -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}') + }) }) diff --git a/playground/vue-lib/package.json b/playground/vue-lib/package.json index d59f3d626160d4..d28ab18295c799 100644 --- a/playground/vue-lib/package.json +++ b/playground/vue-lib/package.json @@ -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": { diff --git a/playground/vue-lib/src-lib-css/index.css b/playground/vue-lib/src-lib-css/index.css new file mode 100644 index 00000000000000..135f4787b30766 --- /dev/null +++ b/playground/vue-lib/src-lib-css/index.css @@ -0,0 +1,3 @@ +.card { + padding: 4rem; +} diff --git a/playground/vue-lib/src-lib-css/index.ts b/playground/vue-lib/src-lib-css/index.ts new file mode 100644 index 00000000000000..0da52ebb0b6115 --- /dev/null +++ b/playground/vue-lib/src-lib-css/index.ts @@ -0,0 +1,3 @@ +import './index.css' + +export function setup() {} diff --git a/playground/vue-lib/vite.config.lib-css.ts b/playground/vue-lib/vite.config.lib-css.ts new file mode 100644 index 00000000000000..e20ec925e05b0e --- /dev/null +++ b/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' + } + } +})