Skip to content

Commit 58a8a4c

Browse files
committed
add source(…) improvements to @tailwindcss/vite
1 parent ddeaa15 commit 58a8a4c

File tree

1 file changed

+50
-7
lines changed

1 file changed

+50
-7
lines changed

packages/@tailwindcss-vite/src/index.ts

+50-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function tailwindcss(): Plugin[] {
3535
// Note: To improve performance, we do not remove candidates from this set.
3636
// This means a longer-ongoing dev mode session might contain candidates that
3737
// are no longer referenced in code.
38-
let moduleGraphCandidates = new Set<string>()
38+
let moduleGraphCandidates = new DefaultMap<string, Set<string>>(() => new Set<string>())
3939
let moduleGraphScanner = new Scanner({})
4040

4141
let roots: DefaultMap<string, Root> = new DefaultMap(
@@ -46,7 +46,7 @@ export default function tailwindcss(): Plugin[] {
4646
let updated = false
4747
for (let candidate of moduleGraphScanner.scanFiles([{ content, extension }])) {
4848
updated = true
49-
moduleGraphCandidates.add(candidate)
49+
moduleGraphCandidates.get(id).add(candidate)
5050
}
5151

5252
if (updated) {
@@ -350,7 +350,7 @@ class Root {
350350

351351
constructor(
352352
private id: string,
353-
private getSharedCandidates: () => Set<string>,
353+
private getSharedCandidates: () => Map<string, Set<string>>,
354354
private base: string,
355355
) {}
356356

@@ -379,9 +379,22 @@ class Root {
379379
})
380380
env.DEBUG && console.timeEnd('[@tailwindcss/vite] Setup compiler')
381381

382-
this.scanner = new Scanner({
383-
sources: this.compiler.globs,
384-
})
382+
let sources = (() => {
383+
// Disable auto source detection
384+
if (this.compiler.root === 'none') {
385+
return []
386+
}
387+
388+
// No root specified, use the module graph
389+
if (this.compiler.root === null) {
390+
return []
391+
}
392+
393+
// Use the specified root
394+
return [this.compiler.root]
395+
})().concat(this.compiler.globs)
396+
397+
this.scanner = new Scanner({ sources })
385398
}
386399

387400
// This should not be here, but right now the Vite plugin is setup where we
@@ -416,9 +429,39 @@ class Root {
416429
this.requiresRebuild = true
417430

418431
env.DEBUG && console.time('[@tailwindcss/vite] Build CSS')
419-
let result = this.compiler.build([...this.getSharedCandidates(), ...this.candidates])
432+
let result = this.compiler.build([...this.sharedCandidates(), ...this.candidates])
420433
env.DEBUG && console.timeEnd('[@tailwindcss/vite] Build CSS')
421434

422435
return result
423436
}
437+
438+
private sharedCandidates(): Set<string> {
439+
if (!this.compiler) return new Set()
440+
if (this.compiler.root === 'none') return new Set()
441+
442+
let root = this.compiler.root
443+
let basePath = root ? path.resolve(root.base, root.pattern) : null
444+
445+
function moduleIdIsAllowed(id: string) {
446+
if (basePath === null) return true
447+
448+
// This a virtual module that's not on the file system
449+
// TODO: What should we do here?
450+
if (!id.startsWith('/')) return true
451+
452+
return id.startsWith(basePath)
453+
}
454+
455+
let shared = new Set<string>()
456+
457+
for (let [id, candidates] of this.getSharedCandidates()) {
458+
if (!moduleIdIsAllowed(id)) continue
459+
460+
for (let candidate of candidates) {
461+
shared.add(candidate)
462+
}
463+
}
464+
465+
return shared
466+
}
424467
}

0 commit comments

Comments
 (0)