diff --git a/packages/plugin-legacy/src/index.ts b/packages/plugin-legacy/src/index.ts index ea96abaa1330ca..898f7ab4cf7558 100644 --- a/packages/plugin-legacy/src/index.ts +++ b/packages/plugin-legacy/src/index.ts @@ -327,6 +327,17 @@ function viteLegacyPlugin(options: Options = {}): Plugin[] { } }, + renderStart(opts) { + // @ts-ignore + // In the `renderChunk` hook, + // we skip inlining CSS for the entry chunk because a separate CSS file + // would've been already linked in index.html (see #9761). + // But if `genModern` is set to false, the CSS still should be inlined. + // If in the future vite doesn't inline CSS any more (as #9920 suggests), + // this flag can be safely deleted. + opts.__vite_inline_entry_css__ = !genModern + }, + async renderChunk(raw, chunk, opts) { if (config.build.ssr) { return null diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index aebb3286584662..eb02d3085393ff 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -563,7 +563,8 @@ 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) { + // @ts-ignore + if (chunk.isEntry && !config.build.lib && !opts.__vite_inline_entry_css__) { return null } chunkCSS = await finalizeCss(chunkCSS, true, config)