1- import type { UserConfig } from '.. /config'
1+ import type { UserConfig } from '@tailwindcss-mangle /config'
22import type { CacheStrategy , InternalCacheOptions , InternalPatchOptions , PackageInfo , TailwindcssClassCache , TailwindcssPatcherOptions , TailwindcssRuntimeContext } from '../types'
33import type { ExtractValidCandidatesOption } from './candidates'
44import { createRequire } from 'node:module'
5+ import process from 'node:process'
56import fs from 'fs-extra'
67import { getPackageInfoSync } from 'local-pkg'
78import path from 'pathe'
@@ -89,16 +90,55 @@ export class TailwindcssPatcher {
8990 return contexts . filter ( x => isObject ( x ) ) . map ( x => x . classCache )
9091 }
9192
92- async getClassCacheSet ( options ?: { removeUniversalSelector ?: boolean } & Partial < ExtractValidCandidatesOption > ) : Promise < Set < string > > {
93+ async getClassCacheSet ( options ?: UserConfig [ 'patch' ] ) : Promise < Set < string > > {
9394 const classSet = new Set < string > ( )
95+ const { output, tailwindcss } = options ?? { }
9496 if ( this . majorVersion === 4 ) {
95- const candidates = await extractValidCandidates ( {
96- base : options ?. base ,
97- css : options ?. css ,
98- sources : options ?. sources ,
99- } )
100- for ( const candidate of candidates ) {
101- classSet . add ( candidate )
97+ const { v4 } = tailwindcss ?? { }
98+ if ( Array . isArray ( v4 ?. cssEntries ) ) {
99+ const results = (
100+ await Promise . all (
101+ v4
102+ . cssEntries
103+ . map ( async ( x ) => {
104+ if ( await fs . exists ( x ) ) {
105+ const css = await fs . readFile ( x , 'utf8' )
106+ return css
107+ }
108+ return false
109+ } ) ,
110+ )
111+ ) . filter ( x => x ) as string [ ]
112+ for ( const css of results ) {
113+ const candidates = await extractValidCandidates ( {
114+ base : v4 ?. base ,
115+ css,
116+ sources : v4 ?. sources ?. map ( ( x ) => {
117+ return {
118+ base : x . base ?? v4 ?. base ?? process . cwd ( ) ,
119+ pattern : x . pattern ,
120+ }
121+ } ) ,
122+ } )
123+ for ( const candidate of candidates ) {
124+ classSet . add ( candidate )
125+ }
126+ }
127+ }
128+ else {
129+ const candidates = await extractValidCandidates ( {
130+ base : v4 ?. base ,
131+ css : v4 ?. css ,
132+ sources : v4 ?. sources ?. map ( ( x ) => {
133+ return {
134+ base : x . base ?? v4 ?. base ?? process . cwd ( ) ,
135+ pattern : x . pattern ,
136+ }
137+ } ) ,
138+ } )
139+ for ( const candidate of candidates ) {
140+ classSet . add ( candidate )
141+ }
102142 }
103143 }
104144 else {
@@ -108,7 +148,7 @@ export class TailwindcssPatcher {
108148 const keys = classCacheMap . keys ( )
109149 for ( const key of keys ) {
110150 const v = key . toString ( )
111- if ( options ?. removeUniversalSelector && v === '*' ) {
151+ if ( output ?. removeUniversalSelector && v === '*' ) {
112152 continue
113153 }
114154 classSet . add ( v )
@@ -122,10 +162,12 @@ export class TailwindcssPatcher {
122162 /**
123163 * @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
124164 */
125- async getClassSet ( options ?: { cacheStrategy ?: CacheStrategy , removeUniversalSelector ?: boolean } ) {
126- const { cacheStrategy = this . cacheOptions . strategy ?? 'merge' , removeUniversalSelector = true } = options ?? { }
165+ async getClassSet ( options ?: UserConfig [ 'patch' ] ) {
166+ const { output, tailwindcss } = options ?? { }
167+ const cacheStrategy = this . cacheOptions . strategy ?? 'merge'
127168 const set = await this . getClassCacheSet ( {
128- removeUniversalSelector,
169+ output,
170+ tailwindcss,
129171 } )
130172 if ( cacheStrategy === 'overwrite' ) {
131173 set . size > 0 && this . setCache ( set )
@@ -146,7 +188,7 @@ export class TailwindcssPatcher {
146188 async extract ( options ?: UserConfig [ 'patch' ] ) {
147189 const { output, tailwindcss } = options ?? { }
148190 if ( output && tailwindcss ) {
149- const { removeUniversalSelector , filename, loose } = output
191+ const { filename, loose } = output
150192
151193 if ( this . majorVersion === 3 ) {
152194 await processTailwindcss ( {
@@ -156,7 +198,8 @@ export class TailwindcssPatcher {
156198 }
157199
158200 const set = await this . getClassSet ( {
159- removeUniversalSelector,
201+ output,
202+ tailwindcss,
160203 } )
161204 if ( filename ) {
162205 const classList = [ ...set ]
0 commit comments