Skip to content

Commit 130ce38

Browse files
committed
feat: change patch return
1 parent 445c21d commit 130ce38

File tree

4 files changed

+101
-88
lines changed

4 files changed

+101
-88
lines changed

packages/tailwindcss-patch/src/core/patches/exportContext/index.ts

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import fs from 'fs-extra'
33
import { inspectPostcssPlugin, inspectProcessTailwindFeaturesReturnContext } from './postcss-v3'
44
import { inspectPostcssPlugin as inspectPostcssPluginCompat, inspectProcessTailwindFeaturesReturnContext as inspectProcessTailwindFeaturesReturnContextCompat } from './postcss-v2'
55
import type { InternalPatchOptions } from '@/types'
6-
import { ensureFileContent } from '@/utils'
76
import logger from '@/logger'
87

98
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> = {}
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,31 +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-
return result
53+
return result
54+
}
4055
}
4156

4257
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)
4460

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> = {}
4763
if (processTailwindFeaturesContent) {
4864
const { code, hasPatched } = inspectProcessTailwindFeaturesReturnContextCompat(processTailwindFeaturesContent)
4965
if (!hasPatched && opt.overwrite) {
@@ -52,11 +68,11 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
5268
})
5369
logger.success('patch tailwindcss processTailwindFeatures for return content successfully!')
5470
}
55-
result.processTailwindFeatures = code
71+
result[k0] = code
5672
}
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')
6076
if (pluginContent) {
6177
const { code, hasPatched } = inspectPostcssPluginCompat(pluginContent)
6278
if (!hasPatched && opt.overwrite) {
@@ -65,7 +81,7 @@ export function monkeyPatchForExposingContextV2(twDir: string, opt: InternalPatc
6581
})
6682
logger.success('patch tailwindcss for expose runtime content successfully!')
6783
}
68-
result.plugin = code
84+
result[k1] = code
6985
}
7086

7187
return result

packages/tailwindcss-patch/src/utils.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,6 @@ import type { PackageJson } from 'pkg-types'
77
export { defu } from 'defu'
88
const { sync } = pkg
99

10-
export function ensureFileContent(filepaths: string | string[]) {
11-
if (typeof filepaths === 'string') {
12-
filepaths = [filepaths]
13-
}
14-
let content
15-
for (const filepath of filepaths) {
16-
if (fs.existsSync(filepath)) {
17-
content = fs.readFileSync(filepath, {
18-
encoding: 'utf8',
19-
})
20-
break
21-
}
22-
}
23-
return content
24-
}
25-
2610
export function requireResolve(id: string, opts?: SyncOpts) {
2711
return sync(id, opts)
2812
}

0 commit comments

Comments
 (0)