@@ -3,14 +3,15 @@ import fs from 'fs-extra'
3
3
import { inspectPostcssPlugin , inspectProcessTailwindFeaturesReturnContext } from './postcss-v3'
4
4
import { inspectPostcssPlugin as inspectPostcssPluginCompat , inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-v2'
5
5
import type { InternalPatchOptions } from '@/types'
6
- import { ensureFileContent } from '@/utils'
7
6
import logger from '@/logger'
8
7
9
8
export function monkeyPatchForExposingContextV3 ( twDir : string , opt : InternalPatchOptions ) {
10
- const processTailwindFeaturesFilePath = path . resolve ( twDir , 'lib/processTailwindFeatures.js' )
9
+ const k0 = 'lib/processTailwindFeatures.js'
10
+ const processTailwindFeaturesFilePath = path . resolve ( twDir , k0 )
11
+
12
+ const processTailwindFeaturesContent = fs . readFileSync ( processTailwindFeaturesFilePath , 'utf8' )
13
+ const result : Record < string , any > = { }
11
14
12
- const processTailwindFeaturesContent = ensureFileContent ( processTailwindFeaturesFilePath )
13
- const result : { processTailwindFeatures ?: string , plugin ?: string } & Record < string , any > = { }
14
15
if ( processTailwindFeaturesContent ) {
15
16
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext ( processTailwindFeaturesContent )
16
17
if ( ! hasPatched && opt . overwrite ) {
@@ -19,31 +20,46 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
19
20
} )
20
21
logger . success ( 'patch tailwindcss processTailwindFeatures for return content successfully!' )
21
22
}
22
- result . processTailwindFeatures = code
23
+ result [ k0 ] = code
24
+ }
25
+ let injectFilepath
26
+ let k1
27
+ const try0 = 'lib/plugin.js'
28
+ const try1 = 'lib/index.js'
29
+ const pluginFilePath = path . resolve ( twDir , try0 )
30
+ const indexFilePath = path . resolve ( twDir , try1 )
31
+ if ( fs . existsSync ( pluginFilePath ) ) {
32
+ k1 = try0
33
+ injectFilepath = pluginFilePath
34
+ }
35
+ else if ( fs . existsSync ( indexFilePath ) ) {
36
+ k1 = try1
37
+ injectFilepath = indexFilePath
23
38
}
24
39
25
- const pluginFilePath = path . resolve ( twDir , 'lib/plugin.js' )
26
- const indexFilePath = path . resolve ( twDir , 'lib/index.js' )
27
- const pluginContent = ensureFileContent ( [ pluginFilePath , indexFilePath ] )
28
- if ( pluginContent ) {
29
- const { code, hasPatched } = inspectPostcssPlugin ( pluginContent )
30
- if ( ! hasPatched && opt . overwrite ) {
31
- fs . writeFileSync ( pluginFilePath , code , {
32
- encoding : 'utf8' ,
33
- } )
34
- logger . success ( 'patch tailwindcss for expose runtime context successfully!' )
40
+ if ( injectFilepath && k1 ) {
41
+ const pluginContent = fs . readFileSync ( injectFilepath , 'utf8' )
42
+ if ( pluginContent ) {
43
+ const { code, hasPatched } = inspectPostcssPlugin ( pluginContent )
44
+ if ( ! hasPatched && opt . overwrite ) {
45
+ fs . writeFileSync ( injectFilepath , code , {
46
+ encoding : 'utf8' ,
47
+ } )
48
+ logger . success ( 'patch tailwindcss for expose runtime context successfully!' )
49
+ }
50
+ result [ k1 ] = code
35
51
}
36
- result . plugin = code
37
- }
38
52
39
- return result
53
+ return result
54
+ }
40
55
}
41
56
42
57
export function monkeyPatchForExposingContextV2 ( twDir : string , opt : InternalPatchOptions ) {
43
- const processTailwindFeaturesFilePath = path . resolve ( twDir , 'lib/jit/processTailwindFeatures.js' )
58
+ const k0 = 'lib/jit/processTailwindFeatures.js'
59
+ const processTailwindFeaturesFilePath = path . resolve ( twDir , k0 )
44
60
45
- const processTailwindFeaturesContent = ensureFileContent ( processTailwindFeaturesFilePath )
46
- const result : { processTailwindFeatures ?: string , plugin ?: string } & Record < string , any > = { }
61
+ const processTailwindFeaturesContent = fs . readFileSync ( processTailwindFeaturesFilePath , 'utf8' )
62
+ const result : Record < string , any > = { }
47
63
if ( processTailwindFeaturesContent ) {
48
64
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContextCompat ( processTailwindFeaturesContent )
49
65
if ( ! hasPatched && opt . overwrite ) {
@@ -52,11 +68,11 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
52
68
} )
53
69
logger . success ( 'patch tailwindcss processTailwindFeatures for return content successfully!' )
54
70
}
55
- result . processTailwindFeatures = code
71
+ result [ k0 ] = code
56
72
}
57
-
58
- const indexFilePath = path . resolve ( twDir , 'lib/jit/index.js' )
59
- const pluginContent = ensureFileContent ( [ indexFilePath ] )
73
+ const k1 = 'lib/jit/index.js'
74
+ const indexFilePath = path . resolve ( twDir , k1 )
75
+ const pluginContent = fs . readFileSync ( indexFilePath , 'utf8' )
60
76
if ( pluginContent ) {
61
77
const { code, hasPatched } = inspectPostcssPluginCompat ( pluginContent )
62
78
if ( ! hasPatched && opt . overwrite ) {
@@ -65,7 +81,7 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
65
81
} )
66
82
logger . success ( 'patch tailwindcss for expose runtime content successfully!' )
67
83
}
68
- result . plugin = code
84
+ result [ k1 ] = code
69
85
}
70
86
71
87
return result
0 commit comments