@@ -6,6 +6,7 @@ import fs from 'fs-extra'
6
6
import path from 'pathe'
7
7
import { generate , parse , traverse } from '../../../babel'
8
8
import logger from '../../../logger'
9
+ import { spliceChangesIntoString } from '../../../utils'
9
10
10
11
function findAstNode ( content : string , options : ILengthUnitsPatchOptions ) {
11
12
const { variableName, units } = options
@@ -22,14 +23,13 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
22
23
) {
23
24
arrayRef = path . parent . init
24
25
const set = new Set ( path . parent . init . elements . map ( x => ( < StringLiteral > x ) . value ) )
25
- for ( let i = 0 ; i < units . length ; i ++ ) {
26
- const unit = units [ i ]
26
+ for ( const unit of units ) {
27
27
if ( ! set . has ( unit ) ) {
28
28
path . parent . init . elements = path . parent . init . elements . map ( ( x ) => {
29
29
if ( t . isStringLiteral ( x ) ) {
30
30
return {
31
- type : x ? .type ,
32
- value : x ? .value ,
31
+ type : x . type ,
32
+ value : x . value ,
33
33
}
34
34
}
35
35
return x
@@ -50,7 +50,7 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
50
50
}
51
51
}
52
52
53
- export function monkeyPatchForSupportingCustomUnit ( rootDir : string , options ?: Partial < ILengthUnitsPatchOptions > ) {
53
+ export function monkeyPatchForSupportingCustomUnitV3 ( rootDir : string , options ?: Partial < ILengthUnitsPatchOptions > ) {
54
54
const opts = defuOverrideArray < Required < ILengthUnitsPatchOptions > , ILengthUnitsPatchOptions [ ] > ( options as Required < ILengthUnitsPatchOptions > , {
55
55
units : [ 'rpx' ] ,
56
56
lengthUnitsFilePath : 'lib/util/dataTypes.js' ,
@@ -86,3 +86,75 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: Pa
86
86
}
87
87
}
88
88
}
89
+
90
+ // "cm","mm","Q","in","pc","pt","px","em","ex","ch","rem","lh","rlh","vw","vh","vmin","vmax","vb","vi","svw","svh","lvw","lvh","dvw","dvh","cqw","cqh","cqi","cqb","cqmin","cqmax"
91
+
92
+ export function monkeyPatchForSupportingCustomUnitV4 ( rootDir : string , options ?: Partial < ILengthUnitsPatchOptions > ) {
93
+ const opts = defuOverrideArray < Required < ILengthUnitsPatchOptions > , ILengthUnitsPatchOptions [ ] > ( options as Required < ILengthUnitsPatchOptions > , {
94
+ units : [ 'rpx' ] ,
95
+ overwrite : true ,
96
+ } )
97
+ const distPath = path . resolve ( rootDir , 'dist' )
98
+ const list = fs . readdirSync ( distPath )
99
+ const chunks = list . filter ( x => x . startsWith ( 'chunk-' ) )
100
+ const guessUnitStart = / \[ \s * [ " ' ] c m [ " ' ] , \s * [ " ' ] m m [ " ' ] , [ \w , " ] + \] /
101
+ let code
102
+ let matches : RegExpMatchArray | null = null
103
+ let guessFile : string | undefined
104
+ for ( const chunkName of chunks ) {
105
+ guessFile = path . join ( distPath , chunkName )
106
+ code = fs . readFileSync ( guessFile , 'utf8' )
107
+ const res = guessUnitStart . exec ( code )
108
+ if ( res ) {
109
+ matches = res
110
+ break
111
+ }
112
+ }
113
+ let hasPatched = false
114
+ if ( matches && code ) {
115
+ const match = matches [ 0 ]
116
+ const ast = parse ( match , {
117
+ sourceType : 'unambiguous' ,
118
+ } )
119
+
120
+ traverse ( ast , {
121
+ ArrayExpression ( path ) {
122
+ for ( const unit of opts . units ) {
123
+ if ( path . node . elements . some ( x => t . isStringLiteral ( x ) && x . value === unit ) ) {
124
+ hasPatched = true
125
+ break
126
+ }
127
+ path . node . elements . push ( t . stringLiteral ( unit ) )
128
+ }
129
+ } ,
130
+ } )
131
+ if ( hasPatched ) {
132
+ return {
133
+ code,
134
+ hasPatched,
135
+ }
136
+ }
137
+ const { code : replacement } = generate ( ast , {
138
+ minified : true ,
139
+ } )
140
+ code = spliceChangesIntoString ( code , [
141
+ {
142
+ start : matches . index as number ,
143
+ end : matches . index as number + match . length ,
144
+ replacement : replacement . endsWith ( ';' ) ? replacement . slice ( 0 , - 1 ) : replacement ,
145
+ } ,
146
+ ] )
147
+ if ( opts . overwrite && guessFile ) {
148
+ fs . writeFileSync ( guessFile , code , {
149
+ encoding : 'utf8' ,
150
+ } )
151
+ logger . success ( 'patch tailwindcss for custom length unit successfully!' )
152
+ }
153
+ }
154
+
155
+ return {
156
+ code,
157
+ hasPatched,
158
+ }
159
+ // /\["cm","mm"/
160
+ }
0 commit comments