1
- import fs from 'node:fs/promises'
2
- import { dirname } from 'node:path'
3
- import { getClassCacheSet , getContexts , getTailwindcssEntry } from './exposeContext'
1
+ import path from 'node:path'
2
+ import fs from 'node:fs'
4
3
import { CacheManager , getCacheOptions } from './cache'
5
4
import { createPatch , getPatchOptions } from './runtime-patcher'
6
5
import { processTailwindcss } from './postcss'
7
6
import type { UserConfig } from '@/config'
8
- import { ensureDir } from '@/utils'
9
- import type { CacheStrategy , InternalCacheOptions , InternalPatchOptions , TailwindcssPatcherOptions } from '@/types'
7
+ import { ensureDir , getPackageInfoSync , isObject } from '@/utils'
8
+ import type { CacheStrategy , InternalCacheOptions , InternalPatchOptions , PackageInfo , TailwindcssClassCache , TailwindcssPatcherOptions , TailwindcssRuntimeContext } from '@/types'
10
9
11
10
export class TailwindcssPatcher {
12
11
public rawOptions : TailwindcssPatcherOptions
13
12
public cacheOptions : InternalCacheOptions
14
13
public patchOptions : InternalPatchOptions
15
14
public patch : ( ) => void
16
15
public cacheManager : CacheManager
16
+ public packageInfo ?: PackageInfo
17
+ public majorVersion ?: number
18
+
17
19
constructor ( options : TailwindcssPatcherOptions = { } ) {
18
20
this . rawOptions = options
19
21
this . cacheOptions = getCacheOptions ( options . cache )
20
22
this . patchOptions = getPatchOptions ( options . patch )
21
23
this . patch = createPatch ( this . patchOptions )
22
24
this . cacheManager = new CacheManager ( this . cacheOptions )
23
- }
24
-
25
- getPkgEntry ( basedir ?: string ) {
26
- return getTailwindcssEntry ( basedir )
25
+ this . packageInfo = getPackageInfoSync ( 'tailwindcss' , { basedir : this . patchOptions . basedir } )
26
+ if ( this . packageInfo && this . packageInfo . version ) {
27
+ this . majorVersion = Number . parseInt ( this . packageInfo . version [ 0 ] )
28
+ }
27
29
}
28
30
29
31
setCache ( set : Set < string > ) {
@@ -33,17 +35,61 @@ export class TailwindcssPatcher {
33
35
}
34
36
35
37
getCache ( ) {
36
- // if(this.cache.enable){
37
38
return this . cacheManager . read ( )
38
- // }
39
+ }
40
+
41
+ getContexts ( ) : TailwindcssRuntimeContext [ ] {
42
+ if ( this . packageInfo ) {
43
+ const distPath = path . join ( this . packageInfo . rootPath , 'lib' )
44
+ let injectFilePath : string | undefined
45
+ if ( this . majorVersion === 2 ) {
46
+ injectFilePath = path . join ( distPath , 'jit/index.js' )
47
+ }
48
+ else {
49
+ injectFilePath = path . join ( distPath , 'plugin.js' )
50
+ if ( ! fs . existsSync ( injectFilePath ) ) {
51
+ injectFilePath = path . join ( distPath , 'index.js' )
52
+ }
53
+ }
54
+ if ( injectFilePath ) {
55
+ // eslint-disable-next-line ts/no-require-imports, ts/no-var-requires
56
+ const mo = require ( injectFilePath )
57
+ if ( mo . contextRef ) {
58
+ return mo . contextRef . value
59
+ }
60
+ }
61
+ }
62
+
63
+ return [ ]
64
+ }
65
+
66
+ getClassCaches ( ) : TailwindcssClassCache [ ] {
67
+ const contexts = this . getContexts ( )
68
+ return contexts . filter ( x => isObject ( x ) ) . map ( x => x . classCache )
69
+ }
70
+
71
+ getClassCacheSet ( options ?: { removeUniversalSelector ?: boolean } ) : Set < string > {
72
+ const classCaches = this . getClassCaches ( )
73
+ const classSet = new Set < string > ( )
74
+ for ( const classCacheMap of classCaches ) {
75
+ const keys = classCacheMap . keys ( )
76
+ for ( const key of keys ) {
77
+ const v = key . toString ( )
78
+ if ( options ?. removeUniversalSelector && v === '*' ) {
79
+ continue
80
+ }
81
+ classSet . add ( v )
82
+ }
83
+ }
84
+ return classSet
39
85
}
40
86
41
87
/**
42
88
* @description 在多个 tailwindcss 上下文时,这个方法将被执行多次,所以策略上应该使用 append
43
89
*/
44
- getClassSet ( options ?: { basedir ?: string , cacheStrategy ?: CacheStrategy , removeUniversalSelector ?: boolean } ) {
45
- const { basedir , cacheStrategy = this . cacheOptions . strategy ?? 'merge' , removeUniversalSelector = true } = options ?? { }
46
- const set = getClassCacheSet ( basedir , {
90
+ getClassSet ( options ?: { cacheStrategy ?: CacheStrategy , removeUniversalSelector ?: boolean } ) {
91
+ const { cacheStrategy = this . cacheOptions . strategy ?? 'merge' , removeUniversalSelector = true } = options ?? { }
92
+ const set = this . getClassCacheSet ( {
47
93
removeUniversalSelector,
48
94
} )
49
95
if ( cacheStrategy === 'overwrite' ) {
@@ -62,10 +108,6 @@ export class TailwindcssPatcher {
62
108
return set
63
109
}
64
110
65
- getContexts ( basedir ?: string ) {
66
- return getContexts ( basedir )
67
- }
68
-
69
111
async extract ( options ?: UserConfig [ 'patch' ] ) {
70
112
const { output, tailwindcss } = options ?? { }
71
113
if ( output && tailwindcss ) {
@@ -77,9 +119,9 @@ export class TailwindcssPatcher {
77
119
removeUniversalSelector,
78
120
} )
79
121
if ( filename ) {
80
- await ensureDir ( dirname ( filename ) )
122
+ await ensureDir ( path . dirname ( filename ) )
81
123
const classList = [ ...set ]
82
- await fs . writeFile ( filename , JSON . stringify ( classList , null , loose ? 2 : undefined ) , 'utf8' )
124
+ fs . writeFileSync ( filename , JSON . stringify ( classList , null , loose ? 2 : undefined ) , 'utf8' )
83
125
return filename
84
126
}
85
127
}
0 commit comments