Remove unused CSS output files when inlined (#8743)
This commit is contained in:
parent
9ed7da1e3b
commit
85cc8daff8
2 changed files with 25 additions and 2 deletions
5
.changeset/tasty-meals-buy.md
Normal file
5
.changeset/tasty-meals-buy.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Remove unused CSS output files when inlined
|
|
@ -200,7 +200,7 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
|
||||||
const inlineConfig = settings.config.build.inlineStylesheets;
|
const inlineConfig = settings.config.build.inlineStylesheets;
|
||||||
const { assetsInlineLimit = 4096 } = settings.config.vite?.build ?? {};
|
const { assetsInlineLimit = 4096 } = settings.config.vite?.build ?? {};
|
||||||
|
|
||||||
Object.entries(bundle).forEach(([_, stylesheet]) => {
|
Object.entries(bundle).forEach(([id, stylesheet]) => {
|
||||||
if (
|
if (
|
||||||
stylesheet.type !== 'asset' ||
|
stylesheet.type !== 'asset' ||
|
||||||
stylesheet.name?.endsWith('.css') !== true ||
|
stylesheet.name?.endsWith('.css') !== true ||
|
||||||
|
@ -224,10 +224,15 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
|
||||||
: { type: 'external', src: stylesheet.fileName };
|
: { type: 'external', src: stylesheet.fileName };
|
||||||
|
|
||||||
const pages = Array.from(eachPageData(internals));
|
const pages = Array.from(eachPageData(internals));
|
||||||
|
let sheetAddedToPage = false;
|
||||||
|
|
||||||
pages.forEach((pageData) => {
|
pages.forEach((pageData) => {
|
||||||
const orderingInfo = pagesToCss[pageData.moduleSpecifier]?.[stylesheet.fileName];
|
const orderingInfo = pagesToCss[pageData.moduleSpecifier]?.[stylesheet.fileName];
|
||||||
if (orderingInfo !== undefined) return pageData.styles.push({ ...orderingInfo, sheet });
|
if (orderingInfo !== undefined) {
|
||||||
|
pageData.styles.push({ ...orderingInfo, sheet });
|
||||||
|
sheetAddedToPage = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const propagatedPaths = pagesToPropagatedCss[pageData.moduleSpecifier];
|
const propagatedPaths = pagesToPropagatedCss[pageData.moduleSpecifier];
|
||||||
if (propagatedPaths === undefined) return;
|
if (propagatedPaths === undefined) return;
|
||||||
|
@ -243,8 +248,21 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
|
||||||
pageData.propagatedStyles.set(pageInfoId, new Set()).get(pageInfoId)!;
|
pageData.propagatedStyles.set(pageInfoId, new Set()).get(pageInfoId)!;
|
||||||
|
|
||||||
propagatedStyles.add(sheet);
|
propagatedStyles.add(sheet);
|
||||||
|
sheetAddedToPage = true;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (toBeInlined && sheetAddedToPage) {
|
||||||
|
// CSS is already added to all used pages, we can delete it from the bundle
|
||||||
|
// and make sure no chunks reference it via `importedCss` (for Vite preloading)
|
||||||
|
// to avoid duplicate CSS.
|
||||||
|
delete bundle[id];
|
||||||
|
for (const chunk of Object.values(bundle)) {
|
||||||
|
if (chunk.type === 'chunk') {
|
||||||
|
chunk.viteMetadata?.importedCss?.delete(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue