@@ -8,78 +8,21 @@ import type {
88import { promises as fs } from 'node:fs'
99import process from 'node:process'
1010import path from 'pathe'
11+ import { resolveValidTailwindV4Candidates } from '../v4/candidates'
12+ import { getTailwindV4DesignSystemCacheKey , loadTailwindV4DesignSystem } from '../v4/node-adapter'
1113
12- let nodeImportPromise : ReturnType < typeof importNode > | undefined
1314let oxideImportPromise : ReturnType < typeof importOxide > | undefined
14- const designSystemPromiseCache = new Map < string , Promise < any > > ( )
1515const designSystemCandidateCache = new Map < string , Map < string , boolean > > ( )
1616
17- async function importNode ( ) {
18- return import ( '@tailwindcss/node' )
19- }
20-
2117async function importOxide ( ) {
2218 return import ( '@tailwindcss/oxide' )
2319}
2420
25- function getNodeModule ( ) {
26- nodeImportPromise ??= importNode ( )
27- return nodeImportPromise
28- }
29-
3021function getOxideModule ( ) {
3122 oxideImportPromise ??= importOxide ( )
3223 return oxideImportPromise
3324}
3425
35- function createDesignSystemCacheKey ( css : string , bases : string [ ] ) {
36- return JSON . stringify ( {
37- css,
38- bases : Array . from ( new Set ( bases . filter ( Boolean ) ) ) ,
39- } )
40- }
41-
42- async function loadDesignSystem ( css : string , bases : string [ ] ) {
43- const uniqueBases = Array . from ( new Set ( bases . filter ( Boolean ) ) )
44- if ( uniqueBases . length === 0 ) {
45- throw new Error ( 'No base directories provided for Tailwind CSS design system.' )
46- }
47-
48- const cacheKey = createDesignSystemCacheKey ( css , uniqueBases )
49- const cached = designSystemPromiseCache . get ( cacheKey )
50- if ( cached ) {
51- return cached
52- }
53-
54- const promise = ( async ( ) => {
55- const { __unstable__loadDesignSystem } = await getNodeModule ( )
56- let lastError : unknown
57-
58- for ( const base of uniqueBases ) {
59- try {
60- return await __unstable__loadDesignSystem ( css , { base } )
61- }
62- catch ( error ) {
63- lastError = error
64- }
65- }
66-
67- if ( lastError instanceof Error ) {
68- throw lastError
69- }
70- throw new Error ( 'Failed to load Tailwind CSS design system.' )
71- } ) ( )
72-
73- designSystemPromiseCache . set ( cacheKey , promise )
74- promise . catch ( ( ) => {
75- if ( designSystemPromiseCache . get ( cacheKey ) === promise ) {
76- designSystemPromiseCache . delete ( cacheKey )
77- designSystemCandidateCache . delete ( cacheKey )
78- }
79- } )
80- return promise
81- }
82-
8326export interface ExtractValidCandidatesOption {
8427 sources ?: SourceEntry [ ]
8528 base ?: string
@@ -131,8 +74,15 @@ export async function extractValidCandidates(options?: ExtractValidCandidatesOpt
13174 negated : source . negated ,
13275 } ) )
13376
134- const designSystemKey = createDesignSystemCacheKey ( css , [ base , ...baseFallbacks ] )
135- const designSystem = await loadDesignSystem ( css , [ base , ...baseFallbacks ] )
77+ const source = {
78+ projectRoot : defaultCwd ,
79+ base,
80+ baseFallbacks,
81+ css,
82+ dependencies : [ ] ,
83+ }
84+ const designSystemKey = getTailwindV4DesignSystemCacheKey ( source )
85+ const designSystem = await loadTailwindV4DesignSystem ( source )
13686 const candidateCache = designSystemCandidateCache . get ( designSystemKey ) ?? new Map < string , boolean > ( )
13787 designSystemCandidateCache . set ( designSystemKey , candidateCache )
13888
@@ -163,15 +113,10 @@ export async function extractValidCandidates(options?: ExtractValidCandidatesOpt
163113 return validCandidates
164114 }
165115
166- const cssByCandidate = designSystem . candidatesToCss ( uncachedCandidates )
116+ const validUncachedCandidates = resolveValidTailwindV4Candidates ( designSystem , uncachedCandidates )
167117
168- for ( let index = 0 ; index < uncachedCandidates . length ; index ++ ) {
169- const candidate = uncachedCandidates [ index ]
170- if ( candidate === undefined ) {
171- continue
172- }
173- const candidateCss = cssByCandidate [ index ]
174- const isValid = typeof candidateCss === 'string' && candidateCss . trim ( ) . length > 0
118+ for ( const candidate of uncachedCandidates ) {
119+ const isValid = validUncachedCandidates . has ( candidate )
175120 candidateCache . set ( candidate , isValid )
176121 if ( ! isValid ) {
177122 continue
0 commit comments