From 93b629a6a48765945341bfcdfc59344ca28f3597 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Mon, 28 Oct 2024 16:15:07 -0400 Subject: [PATCH] Store candidates per-module --- packages/@tailwindcss-vite/src/index.ts | 38 ++++++++++++------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/@tailwindcss-vite/src/index.ts b/packages/@tailwindcss-vite/src/index.ts index b045ab350..e0874d58f 100644 --- a/packages/@tailwindcss-vite/src/index.ts +++ b/packages/@tailwindcss-vite/src/index.ts @@ -35,7 +35,7 @@ export default function tailwindcss(): Plugin[] { // Note: To improve performance, we do not remove candidates from this set. // This means a longer-ongoing dev mode session might contain candidates that // are no longer referenced in code. - let moduleGraphCandidates = new Set() + let moduleGraphCandidates = new DefaultMap>(() => new Set()) let moduleGraphScanner = new Scanner({}) let roots: DefaultMap = new DefaultMap( @@ -46,7 +46,7 @@ export default function tailwindcss(): Plugin[] { let updated = false for (let candidate of moduleGraphScanner.scanFiles([{ content, extension }])) { updated = true - moduleGraphCandidates.add(candidate) + moduleGraphCandidates.get(id).add(candidate) } if (updated) { @@ -348,14 +348,9 @@ class Root { // root. private dependencies = new Set() - // Whether to include candidates from the module graph. This is disabled when - // the user provides `source(none)` to essentially disable auto source - // detection. - private includeCandidatesFromModuleGraph = true - constructor( private id: string, - private getSharedCandidates: () => Set, + private getSharedCandidates: () => Map>, private base: string, ) {} @@ -387,20 +382,14 @@ class Root { let sources = (() => { // Disable auto source detection if (this.compiler.root === 'none') { - this.includeCandidatesFromModuleGraph = false return [] } // No root specified, use the module graph if (this.compiler.root === null) { - this.includeCandidatesFromModuleGraph = true - return [] } - // TODO: In a follow up PR we want this filter this against the module graph. - this.includeCandidatesFromModuleGraph = true - // Use the specified root return [this.compiler.root] })().concat(this.compiler.globs) @@ -440,13 +429,24 @@ class Root { this.requiresRebuild = true env.DEBUG && console.time('[@tailwindcss/vite] Build CSS') - let result = this.compiler.build( - this.includeCandidatesFromModuleGraph - ? [...this.getSharedCandidates(), ...this.candidates] - : Array.from(this.candidates), - ) + let result = this.compiler.build([...this.sharedCandidates(), ...this.candidates]) env.DEBUG && console.timeEnd('[@tailwindcss/vite] Build CSS') return result } + + private sharedCandidates(): Set { + if (!this.compiler) return new Set() + if (this.compiler.root === 'none') return new Set() + + let shared = new Set() + + for (let [id, candidates] of this.getSharedCandidates()) { + for (let candidate of candidates) { + shared.add(candidate) + } + } + + return shared + } }