Skip to content

Commit dcd13af

Browse files
committed
feat: commit codes
1 parent 0b3c0a3 commit dcd13af

File tree

5 files changed

+6799
-25
lines changed

5 files changed

+6799
-25
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'node:path'
22
import fs from 'fs-extra'
33
import * as t from '@babel/types'
44
import type { ArrayExpression, StringLiteral } from '@babel/types'
5-
import { defu } from '@/utils'
5+
import { defuOverrideArray } from '@/utils'
66
import type { ILengthUnitsPatchOptions } from '@/types'
77
import { generate, parse, traverse } from '@/babel'
88
import logger from '@/logger'
@@ -51,7 +51,7 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
5151
}
5252

5353
export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: Partial<ILengthUnitsPatchOptions>) {
54-
const opts = defu<Required<ILengthUnitsPatchOptions>, ILengthUnitsPatchOptions[]>(options, {
54+
const opts = defuOverrideArray<Required<ILengthUnitsPatchOptions>, ILengthUnitsPatchOptions[]>(options as Required<ILengthUnitsPatchOptions>, {
5555
units: ['rpx'],
5656
lengthUnitsFilePath: 'lib/util/dataTypes.js',
5757
variableName: 'lengthUnits',
@@ -81,6 +81,8 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: Pa
8181
logger.success('patch tailwindcss for custom length unit successfully!')
8282
}
8383
}
84-
return code
84+
return {
85+
[opts.lengthUnitsFilePath]: code,
86+
}
8587
}
8688
}

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import path from 'node:path'
22
import { gte } from 'semver'
33
import type { PackageJson } from 'pkg-types'
44
import { monkeyPatchForExposingContextV2, monkeyPatchForExposingContextV3, monkeyPatchForSupportingCustomUnit } from './patches'
5-
6-
import type { InternalPatchOptions } from '@/types'
5+
import { defu } from '@/utils'
6+
import type { ILengthUnitsPatchOptions, InternalPatchOptions } from '@/types'
77

88
export function internalPatch(pkgJsonPath: string | undefined, options: InternalPatchOptions) {
99
if (pkgJsonPath) {
@@ -12,22 +12,21 @@ export function internalPatch(pkgJsonPath: string | undefined, options: Internal
1212
const twDir = path.dirname(pkgJsonPath)
1313
options.version = pkgJson.version
1414
if (gte(pkgJson.version!, '3.0.0')) {
15-
options.version = pkgJson.version
16-
15+
let result: Record<string, any> | undefined = {}
16+
if (options.applyPatches?.exportContext) {
17+
result = monkeyPatchForExposingContextV3(twDir, options)
18+
}
1719
if (options.applyPatches?.extendLengthUnits) {
1820
try {
19-
monkeyPatchForSupportingCustomUnit(twDir, {
21+
Object.assign(result ?? {}, monkeyPatchForSupportingCustomUnit(twDir, defu<Partial<ILengthUnitsPatchOptions>, Partial<ILengthUnitsPatchOptions>[]>(options.applyPatches.extendLengthUnits === true ? undefined : options.applyPatches.extendLengthUnits, {
2022
overwrite: options.overwrite,
21-
})
23+
})))
2224
}
2325
catch {
2426

2527
}
2628
}
27-
28-
if (options.applyPatches?.exportContext) {
29-
return monkeyPatchForExposingContextV3(twDir, options)
30-
}
29+
return result
3130
}
3231
else if (gte(pkgJson.version!, '2.0.0')) {
3332
if (options.applyPatches?.exportContext) {

packages/tailwindcss-patch/src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ import fs from 'fs-extra'
33
import type { SyncOpts } from 'resolve'
44
import pkg from 'resolve'
55
import type { PackageJson } from 'pkg-types'
6+
import { createDefu } from 'defu'
67

78
export { defu } from 'defu'
89
const { sync } = pkg
910

11+
export const defuOverrideArray = createDefu((obj, key, value) => {
12+
if (Array.isArray(obj[key]) && Array.isArray(value)) {
13+
obj[key] = value
14+
return true
15+
}
16+
})
17+
1018
export function requireResolve(id: string, opts?: SyncOpts) {
1119
return sync(id, opts)
1220
}

0 commit comments

Comments
 (0)