Skip to content

Commit fb07c5d

Browse files
committed
feat: commit custom lengthUnits
1 parent a767989 commit fb07c5d

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './exportContext'
2+
export * from './supportCustomUnits'
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
import * as t from '@babel/types'
4+
import type { ArrayExpression, StringLiteral } from '@babel/types'
5+
import type { ILengthUnitsPatchDangerousOptions, ILengthUnitsPatchOptions } from './types'
6+
import { generate, parse, traverse } from '@/babel'
7+
8+
function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
9+
const DOPTS = options.dangerousOptions as Required<ILengthUnitsPatchDangerousOptions>
10+
const ast = parse(content)
11+
12+
let arrayRef: ArrayExpression | undefined
13+
let changed = false
14+
traverse(ast, {
15+
Identifier(path) {
16+
if (
17+
path.node.name === DOPTS.variableName
18+
&& t.isVariableDeclarator(path.parent)
19+
&& t.isArrayExpression(path.parent.init)
20+
) {
21+
arrayRef = path.parent.init
22+
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]
25+
if (!set.has(unit)) {
26+
path.parent.init.elements = path.parent.init.elements.map((x) => {
27+
if (t.isStringLiteral(x)) {
28+
return {
29+
type: x?.type,
30+
value: x?.value,
31+
}
32+
}
33+
return x
34+
})
35+
path.parent.init.elements.push({
36+
type: 'StringLiteral',
37+
value: unit,
38+
})
39+
changed = true
40+
}
41+
}
42+
}
43+
},
44+
})
45+
return {
46+
arrayRef,
47+
changed,
48+
}
49+
}
50+
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)
55+
const dataTypesFileContent = fs.readFileSync(dataTypesFilePath, {
56+
encoding: 'utf8',
57+
})
58+
const { arrayRef, changed } = findAstNode(dataTypesFileContent, options)
59+
if (arrayRef && changed) {
60+
const { code } = generate(arrayRef, {
61+
jsescOption: {
62+
quotes: 'single',
63+
},
64+
})
65+
66+
if (arrayRef.start && arrayRef.end) {
67+
const prev = dataTypesFileContent.slice(0, arrayRef.start)
68+
const next = dataTypesFileContent.slice(arrayRef.end as number)
69+
const newCode = prev + code + next
70+
if (DOPTS.overwrite) {
71+
fs.writeFileSync(DOPTS.destPath ?? dataTypesFilePath, newCode, {
72+
encoding: 'utf8',
73+
})
74+
console.log('patch tailwindcss for custom length unit successfully!')
75+
}
76+
}
77+
return code
78+
}
79+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface ILengthUnitsPatchDangerousOptions {
2+
packageName?: string
3+
gteVersion?: string
4+
lengthUnitsFilePath?: string
5+
variableName?: string
6+
overwrite?: boolean
7+
destPath?: string
8+
}
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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
22
import { gte } from 'semver'
33
import type { PackageJson } from 'pkg-types'
4-
import { monkeyPatchForExposingContextV2, monkeyPatchForExposingContextV3 } from './patches'
4+
import { monkeyPatchForExposingContextV2, monkeyPatchForExposingContextV3, monkeyPatchForSupportingCustomUnit } from './patches'
55

66
import type { InternalPatchOptions } from '@/types'
77

@@ -12,6 +12,14 @@ 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+
})
1523
const result = monkeyPatchForExposingContextV3(twDir, options)
1624
return result
1725
}

0 commit comments

Comments
 (0)