11import path from 'node:path'
22import fs from 'fs-extra'
3- import { inspectPostcssPlugin , inspectProcessTailwindFeaturesReturnContext } from './postcss'
4- import { inspectPostcssPlugin as inspectPostcssPluginCompat , inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-compat '
3+ import { inspectPostcssPlugin , inspectProcessTailwindFeaturesReturnContext } from './postcss-v3 '
4+ import { inspectPostcssPlugin as inspectPostcssPluginCompat , inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-v2 '
55import type { InternalPatchOptions } from '@/types'
6- import { ensureFileContent } from '@/utils'
76import logger from '@/logger'
87
98export 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 > = { }
1114
12- const processTailwindFeaturesContent = ensureFileContent ( processTailwindFeaturesFilePath )
13- const result : { processTailwindFeatures ?: string , plugin ?: string } & Record < string , any > = { }
1415 if ( processTailwindFeaturesContent ) {
1516 const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContext ( processTailwindFeaturesContent )
1617 if ( ! hasPatched && opt . overwrite ) {
@@ -19,32 +20,46 @@ export function monkeyPatchForExposingContextV3(twDir: string, opt: InternalPatc
1920 } )
2021 logger . success ( 'patch tailwindcss processTailwindFeatures for return content successfully!' )
2122 }
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
2338 }
2439
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
3551 }
36- result . plugin = code
37- }
3852
39- opt . custom && typeof opt . custom === 'function' && opt . custom ( twDir , result )
40- return result
53+ return result
54+ }
4155}
4256
4357export function monkeyPatchForExposingContextV2 ( twDir : string , opt : InternalPatchOptions ) {
44- const processTailwindFeaturesFilePath = path . resolve ( twDir , 'lib/jit/processTailwindFeatures.js' )
58+ const k0 = 'lib/jit/processTailwindFeatures.js'
59+ const processTailwindFeaturesFilePath = path . resolve ( twDir , k0 )
4560
46- const processTailwindFeaturesContent = ensureFileContent ( processTailwindFeaturesFilePath )
47- const result : { processTailwindFeatures ?: string , plugin ?: string } & Record < string , any > = { }
61+ const processTailwindFeaturesContent = fs . readFileSync ( processTailwindFeaturesFilePath , 'utf8' )
62+ const result : Record < string , any > = { }
4863 if ( processTailwindFeaturesContent ) {
4964 const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContextCompat ( processTailwindFeaturesContent )
5065 if ( ! hasPatched && opt . overwrite ) {
@@ -53,11 +68,11 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
5368 } )
5469 logger . success ( 'patch tailwindcss processTailwindFeatures for return content successfully!' )
5570 }
56- result . processTailwindFeatures = code
71+ result [ k0 ] = code
5772 }
58-
59- const indexFilePath = path . resolve ( twDir , 'lib/jit/index.js' )
60- 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' )
6176 if ( pluginContent ) {
6277 const { code, hasPatched } = inspectPostcssPluginCompat ( pluginContent )
6378 if ( ! hasPatched && opt . overwrite ) {
@@ -66,9 +81,8 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
6681 } )
6782 logger . success ( 'patch tailwindcss for expose runtime content successfully!' )
6883 }
69- result . plugin = code
84+ result [ k1 ] = code
7085 }
7186
72- opt . custom && typeof opt . custom === 'function' && opt . custom ( twDir , result )
7387 return result
7488}
0 commit comments