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

perf(CSSEntryPointsPlugin): Only visit chunks once per entry point #239

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/plugins/CSSEntryPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,23 @@
},

generateBundle(options, bundle) {
for (const chunk of Object.values(bundle)) {
for (const [chunkName, chunk] of Object.entries(bundle)) {
// Only handle entry points
if (chunk.type !== 'chunk' || !chunk.isEntry) {
continue
}

// Set of all synchronously imported CSS of this entry point
const importedCSS = new Set<string>(chunk.viteMetadata?.importedCss ?? [])
const visitedChunks = new Set<string>()
const getImportedCSS = (importedNames: string[]) => {
for (const importedName of importedNames) {
// skip if already extracted
if (visitedChunks.has(importedName)) {
continue
}
visitedChunks.add(importedName)

const importedChunk = bundle[importedName]
// Skip non chunks
if (importedChunk.type !== 'chunk') {
Expand All @@ -86,7 +93,7 @@
.forEach((name: string) => importedCSS.add(name))
}
}
getImportedCSS(chunk.imports)
getImportedCSS([chunkName])

// Skip empty entries if not configured to output empty CSS
if (importedCSS.size === 0 && !pluginOptions.createEmptyEntryPoints) {
Expand All @@ -102,7 +109,11 @@
const cssName = `${entryName}.css`

// Keep original path
const path = dirname(typeof options.assetFileNames === 'string' ? options.assetFileNames : options.assetFileNames({ type: 'asset', source: '', name: 'name.css' }))
const path = dirname(
typeof options.assetFileNames === 'string'
? options.assetFileNames
: options.assetFileNames({ type: 'asset', source: '', name: 'name.css' })

Check warning on line 115 in lib/plugins/CSSEntryPoints.ts

View workflow job for this annotation

GitHub Actions / NPM lint

Missing trailing comma
)

this.emitFile({
type: 'asset',
Expand Down
Loading