@@ -35,7 +35,7 @@ export default function tailwindcss(): Plugin[] {
35
35
// Note: To improve performance, we do not remove candidates from this set.
36
36
// This means a longer-ongoing dev mode session might contain candidates that
37
37
// are no longer referenced in code.
38
- let moduleGraphCandidates = new Set < string > ( )
38
+ let moduleGraphCandidates = new DefaultMap < string , Set < string > > ( ( ) => new Set < string > ( ) )
39
39
let moduleGraphScanner = new Scanner ( { } )
40
40
41
41
let roots : DefaultMap < string , Root > = new DefaultMap (
@@ -46,7 +46,7 @@ export default function tailwindcss(): Plugin[] {
46
46
let updated = false
47
47
for ( let candidate of moduleGraphScanner . scanFiles ( [ { content, extension } ] ) ) {
48
48
updated = true
49
- moduleGraphCandidates . add ( candidate )
49
+ moduleGraphCandidates . get ( id ) . add ( candidate )
50
50
}
51
51
52
52
if ( updated ) {
@@ -350,7 +350,7 @@ class Root {
350
350
351
351
constructor (
352
352
private id : string ,
353
- private getSharedCandidates : ( ) => Set < string > ,
353
+ private getSharedCandidates : ( ) => Map < string , Set < string > > ,
354
354
private base : string ,
355
355
) { }
356
356
@@ -379,9 +379,22 @@ class Root {
379
379
} )
380
380
env . DEBUG && console . timeEnd ( '[@tailwindcss/vite] Setup compiler' )
381
381
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 } )
385
398
}
386
399
387
400
// This should not be here, but right now the Vite plugin is setup where we
@@ -416,9 +429,39 @@ class Root {
416
429
this . requiresRebuild = true
417
430
418
431
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 ] )
420
433
env . DEBUG && console . timeEnd ( '[@tailwindcss/vite] Build CSS' )
421
434
422
435
return result
423
436
}
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
+ }
424
467
}
0 commit comments