Skip to content

Commit

Permalink
fix: inline entry css if modern bundle is skipped
Browse files Browse the repository at this point in the history
  • Loading branch information
lsdsjy committed Oct 10, 2022
1 parent 9c4daec commit a99eade
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/plugin-legacy/src/index.ts
Expand Up @@ -352,6 +352,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
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/plugins/css.ts
Expand Up @@ -569,7 +569,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 && !config.build.lib) {
// @ts-ignore
if (chunk.isEntry && !config.build.lib && !opts.__vite_inline_entry_css__) {
return null
}
chunkCSS = await finalizeCss(chunkCSS, true, config)
Expand Down

0 comments on commit a99eade

Please sign in to comment.