@@ -36,6 +36,15 @@ interface TailwindV4IncrementalGenerateCacheEntry {
3636 target : TailwindV4GenerateTarget
3737}
3838
39+ interface TailwindV4IncrementalCacheSeedOptions {
40+ compatibleSource : TailwindV4ResolvedSource
41+ generated : Awaited < ReturnType < TailwindV4Engine [ 'generate' ] > >
42+ requestedCandidates : Set < string >
43+ styleOptions : Partial < IStyleHandlerOptions > | undefined
44+ tailwindcssV3Compatibility : boolean | undefined
45+ target : TailwindV4GenerateTarget
46+ }
47+
3948function findLeadingImportInsertionIndex ( css : string ) {
4049 const importPattern = / (?: ^ | \n ) \s * @ i m p o r t \b [ ^ ; ] * ; / g
4150 let insertionIndex = 0
@@ -134,6 +143,36 @@ function resolveTargetCandidates(
134143 : collected
135144}
136145
146+ function collectSeenCandidates (
147+ generated : Pick < Awaited < ReturnType < TailwindV4Engine [ 'generate' ] > > , 'rawCandidates' | 'classSet' > ,
148+ requestedCandidates : Set < string > ,
149+ ) {
150+ return new Set ( [
151+ ...requestedCandidates ,
152+ ...generated . rawCandidates ,
153+ ...generated . classSet ,
154+ ] )
155+ }
156+
157+ function seedIncrementalGenerateCache ( options : TailwindV4IncrementalCacheSeedOptions ) {
158+ const cacheKey = createIncrementalGenerateCacheKey (
159+ options . compatibleSource ,
160+ options . target ,
161+ options . styleOptions ,
162+ options . tailwindcssV3Compatibility ,
163+ )
164+ incrementalGenerateCache . set ( cacheKey , {
165+ seenCandidates : collectSeenCandidates ( options . generated , options . requestedCandidates ) ,
166+ classSet : new Set ( options . generated . classSet ) ,
167+ css : options . generated . css ,
168+ rawCss : options . generated . rawCss ,
169+ dependencies : options . generated . dependencies ,
170+ sources : options . generated . sources ,
171+ root : options . generated . root ,
172+ target : options . generated . target ,
173+ } )
174+ }
175+
137176function parseImportSourceParam ( params : string ) {
138177 const match = / \b s o u r c e \( \s * ( n o n e | ( [ ' " ] ) ( .* ?) \2) \s * \) / . exec ( params )
139178 if ( ! match ) {
@@ -360,13 +399,27 @@ export function createTailwindV4Engine(source: TailwindV4ResolvedSource): Tailwi
360399 }
361400
362401 async function generateWithIncrementalCache ( options : TailwindV4GenerateOptions = { } ) {
363- if ( ( options . sources ?. length ?? 0 ) > 0 || options . bareArbitraryValues !== undefined || options . scanSources === true || Array . isArray ( options . scanSources ) ) {
364- return generateOnce ( source , options )
365- }
366-
367402 const target = options . target ?? 'weapp'
368403 const compatibleSource = createCompatibleSource ( source , target , options . tailwindcssV3Compatibility )
369404 const requestedCandidates = resolveTargetCandidates ( options . candidates , target )
405+
406+ if ( ( options . sources ?. length ?? 0 ) > 0 || options . bareArbitraryValues !== undefined || Array . isArray ( options . scanSources ) ) {
407+ return generateOnce ( source , options )
408+ }
409+
410+ if ( options . scanSources === true ) {
411+ const generated = await generateOnce ( source , options )
412+ seedIncrementalGenerateCache ( {
413+ compatibleSource,
414+ generated,
415+ requestedCandidates,
416+ styleOptions : options . styleOptions ,
417+ tailwindcssV3Compatibility : options . tailwindcssV3Compatibility ,
418+ target,
419+ } )
420+ return generated
421+ }
422+
370423 const cacheKey = createIncrementalGenerateCacheKey (
371424 compatibleSource ,
372425 target ,
@@ -427,15 +480,13 @@ export function createTailwindV4Engine(source: TailwindV4ResolvedSource): Tailwi
427480 }
428481
429482 const generated = await generateOnce ( source , options )
430- incrementalGenerateCache . set ( cacheKey , {
431- seenCandidates : new Set ( requestedCandidates ) ,
432- classSet : new Set ( generated . classSet ) ,
433- css : generated . css ,
434- rawCss : generated . rawCss ,
435- dependencies : generated . dependencies ,
436- sources : generated . sources ,
437- root : generated . root ,
438- target : generated . target ,
483+ seedIncrementalGenerateCache ( {
484+ compatibleSource,
485+ generated,
486+ requestedCandidates,
487+ styleOptions : options . styleOptions ,
488+ tailwindcssV3Compatibility : options . tailwindcssV3Compatibility ,
489+ target,
439490 } )
440491 return generated
441492 }
0 commit comments