@@ -2,26 +2,27 @@ import fs from 'node:fs'
2
2
import path from 'node:path'
3
3
import * as t from '@babel/types'
4
4
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'
6
7
import { generate , parse , traverse } from '@/babel'
7
8
8
9
function findAstNode ( content : string , options : ILengthUnitsPatchOptions ) {
9
- const DOPTS = options . dangerousOptions as Required < ILengthUnitsPatchDangerousOptions >
10
+ const { variableName , units } = options
10
11
const ast = parse ( content )
11
12
12
13
let arrayRef : ArrayExpression | undefined
13
14
let changed = false
14
15
traverse ( ast , {
15
16
Identifier ( path ) {
16
17
if (
17
- path . node . name === DOPTS . variableName
18
+ path . node . name === variableName
18
19
&& t . isVariableDeclarator ( path . parent )
19
20
&& t . isArrayExpression ( path . parent . init )
20
21
) {
21
22
arrayRef = path . parent . init
22
23
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 ]
25
26
if ( ! set . has ( unit ) ) {
26
27
path . parent . init . elements = path . parent . init . elements . map ( ( x ) => {
27
28
if ( t . isStringLiteral ( x ) ) {
@@ -48,14 +49,19 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
48
49
}
49
50
}
50
51
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 )
55
61
const dataTypesFileContent = fs . readFileSync ( dataTypesFilePath , {
56
62
encoding : 'utf8' ,
57
63
} )
58
- const { arrayRef, changed } = findAstNode ( dataTypesFileContent , options )
64
+ const { arrayRef, changed } = findAstNode ( dataTypesFileContent , opts )
59
65
if ( arrayRef && changed ) {
60
66
const { code } = generate ( arrayRef , {
61
67
jsescOption : {
@@ -67,8 +73,8 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options: ILe
67
73
const prev = dataTypesFileContent . slice ( 0 , arrayRef . start )
68
74
const next = dataTypesFileContent . slice ( arrayRef . end as number )
69
75
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 , {
72
78
encoding : 'utf8' ,
73
79
} )
74
80
console . log ( 'patch tailwindcss for custom length unit successfully!' )
0 commit comments