Skip to content

Commit

Permalink
Merge pull request #239 from nextcloud-libraries/perf/only-extract-if…
Browse files Browse the repository at this point in the history
…-not-already-visited

perf(CSSEntryPointsPlugin): Only visit chunks once per entry point
  • Loading branch information
susnux authored Jul 12, 2024
2 parents e2403a6 + a46c332 commit 3a9126d
Showing 1 changed file with 14 additions and 3 deletions.
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 @@ export function CSSEntryPointsPlugin(options?: CSSEntryPointsPluginOptions) {
},

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 @@ export function CSSEntryPointsPlugin(options?: CSSEntryPointsPluginOptions) {
.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 @@ export function CSSEntryPointsPlugin(options?: CSSEntryPointsPluginOptions) {
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' })
)

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

0 comments on commit 3a9126d

Please sign in to comment.