Skip to content

Commit ddd37e6

Browse files
committed
feat: add applyPatches options
1 parent 2810e09 commit ddd37e6

File tree

8 files changed

+50
-40
lines changed

8 files changed

+50
-40
lines changed

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

+18-12
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ import fs from 'node:fs'
22
import path from 'node:path'
33
import * as t from '@babel/types'
44
import type { ArrayExpression, StringLiteral } from '@babel/types'
5-
import type { ILengthUnitsPatchDangerousOptions, ILengthUnitsPatchOptions } from './types'
5+
import { defu } from 'defu'
6+
import type { ILengthUnitsPatchOptions } from './types'
67
import { generate, parse, traverse } from '@/babel'
78

89
function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
9-
const DOPTS = options.dangerousOptions as Required<ILengthUnitsPatchDangerousOptions>
10+
const { variableName, units } = options
1011
const ast = parse(content)
1112

1213
let arrayRef: ArrayExpression | undefined
1314
let changed = false
1415
traverse(ast, {
1516
Identifier(path) {
1617
if (
17-
path.node.name === DOPTS.variableName
18+
path.node.name === variableName
1819
&& t.isVariableDeclarator(path.parent)
1920
&& t.isArrayExpression(path.parent.init)
2021
) {
2122
arrayRef = path.parent.init
2223
const set = new Set(path.parent.init.elements.map(x => (<StringLiteral>x).value))
23-
for (let i = 0; i < options.units.length; i++) {
24-
const unit = options.units[i]
24+
for (let i = 0; i < units.length; i++) {
25+
const unit = units[i]
2526
if (!set.has(unit)) {
2627
path.parent.init.elements = path.parent.init.elements.map((x) => {
2728
if (t.isStringLiteral(x)) {
@@ -48,14 +49,19 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
4849
}
4950
}
5051

51-
export function monkeyPatchForSupportingCustomUnit(rootDir: string, options: ILengthUnitsPatchOptions) {
52-
const { dangerousOptions } = options
53-
const DOPTS = dangerousOptions as Required<ILengthUnitsPatchDangerousOptions>
54-
const dataTypesFilePath = path.resolve(rootDir, DOPTS.lengthUnitsFilePath)
52+
export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: ILengthUnitsPatchOptions) {
53+
const opts = defu<Required<ILengthUnitsPatchOptions>, ILengthUnitsPatchOptions[]>(options, {
54+
units: ['rpx'],
55+
lengthUnitsFilePath: 'lib/util/dataTypes.js',
56+
variableName: 'lengthUnits',
57+
overwrite: true,
58+
})
59+
const { lengthUnitsFilePath, overwrite, destPath } = opts
60+
const dataTypesFilePath = path.resolve(rootDir, lengthUnitsFilePath)
5561
const dataTypesFileContent = fs.readFileSync(dataTypesFilePath, {
5662
encoding: 'utf8',
5763
})
58-
const { arrayRef, changed } = findAstNode(dataTypesFileContent, options)
64+
const { arrayRef, changed } = findAstNode(dataTypesFileContent, opts)
5965
if (arrayRef && changed) {
6066
const { code } = generate(arrayRef, {
6167
jsescOption: {
@@ -67,8 +73,8 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options: ILe
6773
const prev = dataTypesFileContent.slice(0, arrayRef.start)
6874
const next = dataTypesFileContent.slice(arrayRef.end as number)
6975
const newCode = prev + code + next
70-
if (DOPTS.overwrite) {
71-
fs.writeFileSync(DOPTS.destPath ?? dataTypesFilePath, newCode, {
76+
if (overwrite) {
77+
fs.writeFileSync(destPath ?? dataTypesFilePath, newCode, {
7278
encoding: 'utf8',
7379
})
7480
console.log('patch tailwindcss for custom length unit successfully!')
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
export interface ILengthUnitsPatchDangerousOptions {
2-
packageName?: string
3-
gteVersion?: string
1+
export interface ILengthUnitsPatchOptions {
2+
units: string[]
43
lengthUnitsFilePath?: string
54
variableName?: string
65
overwrite?: boolean
76
destPath?: string
87
}
9-
10-
export interface ILengthUnitsPatchOptions {
11-
units: string[]
12-
paths?: string[]
13-
dangerousOptions?: ILengthUnitsPatchDangerousOptions
14-
basedir?: string
15-
}

packages/tailwindcss-patch/src/core/runtime-patcher.ts

+16-12
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ export function internalPatch(pkgJsonPath: string | undefined, options: Internal
1212
const twDir = path.dirname(pkgJsonPath)
1313
if (gte(pkgJson.version!, '3.0.0')) {
1414
options.version = pkgJson.version
15-
monkeyPatchForSupportingCustomUnit(twDir, {
16-
units: ['rpx'],
17-
dangerousOptions: {
18-
lengthUnitsFilePath: 'lib/util/dataTypes.js',
19-
variableName: 'lengthUnits',
20-
overwrite: true,
21-
},
22-
})
23-
const result = monkeyPatchForExposingContextV3(twDir, options)
24-
return result
15+
16+
if (options.applyPatches?.extendLengthUnits) {
17+
try {
18+
monkeyPatchForSupportingCustomUnit(twDir)
19+
}
20+
catch {
21+
22+
}
23+
}
24+
25+
if (options.applyPatches?.exportContext) {
26+
return monkeyPatchForExposingContextV3(twDir, options)
27+
}
2528
}
2629
else if (gte(pkgJson.version!, '2.0.0')) {
2730
options.version = pkgJson.version
28-
const result = monkeyPatchForExposingContextV2(twDir, options)
29-
return result
31+
if (options.applyPatches?.exportContext) {
32+
return monkeyPatchForExposingContextV2(twDir, options)
33+
}
3034
}
3135
// no sth
3236
}

packages/tailwindcss-patch/src/core/tw-patcher.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class TailwindcssPatcher {
3232
return internalPatch(this.packageInfo?.packageJsonPath, this.patchOptions)
3333
}
3434
catch (error) {
35-
console.warn(`patch tailwindcss failed: ${(<Error>error).message}`)
35+
console.error(`patch tailwindcss failed: ${(<Error>error).message}`)
3636
}
3737
}
3838
}

packages/tailwindcss-patch/src/defaults.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import type { DeepRequired, InternalPatchOptions, PatchOptions } from './types'
44

55
export function getDefaultPatchOptions(): DeepRequired<PatchOptions> {
66
return {
7+
applyPatches: {
8+
exportContext: true,
9+
extendLengthUnits: false,
10+
},
711
overwrite: true,
812
}
913
}

packages/tailwindcss-patch/src/types.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ export interface PatchOptions {
3131
}
3232
}
3333

34-
export interface InternalPatchOptions {
35-
overwrite: boolean
36-
paths?: string[]
37-
basedir?: string
38-
custom?: (dir: string, ctx: Record<string, any>) => void
34+
export interface InternalPatchOptions extends PatchOptions {
3935
version?: string
4036
}
4137

packages/tailwindcss-patch/test/__snapshots__/defaults.test.ts.snap

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
exports[`defaults > getDefaultPatchOptions 1`] = `
44
{
5+
"applyPatches": {
6+
"exportContext": true,
7+
"extendLengthUnits": false,
8+
},
59
"overwrite": true,
610
}
711
`;

packages/tailwindcss-patch/test/index.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function getTailwindcssVersion(str: string) {
1818
}
1919
}
2020

21+
// eslint-disable-next-line ts/no-var-requires, ts/no-require-imports
2122
const pkg = require(versionsPkgDir)
2223
const versions = Object.keys(pkg.dependencies)
2324

@@ -27,6 +28,9 @@ describe('versions-patch', () => {
2728

2829
const res = internalPatch(path.resolve(tailwindcssCasePath, `versions/${v}/package.json`), {
2930
overwrite: false,
31+
applyPatches: {
32+
exportContext: true,
33+
},
3034
})
3135
expect(res).toMatchSnapshot()
3236
})

0 commit comments

Comments
 (0)