@@ -6,6 +6,7 @@ import fs from 'fs-extra'
66import path from 'pathe'
77import { generate , parse , traverse } from '../../../babel'
88import logger from '../../../logger'
9+ import { spliceChangesIntoString } from '../../../utils'
910
1011function findAstNode ( content : string , options : ILengthUnitsPatchOptions ) {
1112 const { variableName, units } = options
@@ -22,14 +23,13 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
2223 ) {
2324 arrayRef = path . parent . init
2425 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 ) {
2727 if ( ! set . has ( unit ) ) {
2828 path . parent . init . elements = path . parent . init . elements . map ( ( x ) => {
2929 if ( t . isStringLiteral ( x ) ) {
3030 return {
31- type : x ? .type ,
32- value : x ? .value ,
31+ type : x . type ,
32+ value : x . value ,
3333 }
3434 }
3535 return x
@@ -50,7 +50,7 @@ function findAstNode(content: string, options: ILengthUnitsPatchOptions) {
5050 }
5151}
5252
53- export function monkeyPatchForSupportingCustomUnit ( rootDir : string , options ?: Partial < ILengthUnitsPatchOptions > ) {
53+ export function monkeyPatchForSupportingCustomUnitV3 ( rootDir : string , options ?: Partial < ILengthUnitsPatchOptions > ) {
5454 const opts = defuOverrideArray < Required < ILengthUnitsPatchOptions > , ILengthUnitsPatchOptions [ ] > ( options as Required < ILengthUnitsPatchOptions > , {
5555 units : [ 'rpx' ] ,
5656 lengthUnitsFilePath : 'lib/util/dataTypes.js' ,
@@ -86,3 +86,75 @@ export function monkeyPatchForSupportingCustomUnit(rootDir: string, options?: Pa
8686 }
8787 }
8888}
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