@@ -4,8 +4,7 @@ import MagicString from 'magic-string'
4
4
import { splitCode } from '@tailwindcss-mangle/shared'
5
5
import { sort } from 'fast-sort'
6
6
import { jsStringEscape } from '@ast-core/escape'
7
- import { parse , ParseResult } from '@babel/parser'
8
- import traverse from '@babel/traverse'
7
+ import type { ParseResult } from '@babel/parser'
9
8
import { escapeStringRegexp } from '@/utils'
10
9
import type { Context } from '@/ctx'
11
10
import { between } from '@/math'
@@ -147,7 +146,7 @@ export function preProcessJs(options: IPreProcessJsOptions) {
147
146
return code
148
147
}
149
148
const markedArray : [ number , number ] [ ] = [ ]
150
- traverse ( ast , {
149
+ babel . traverse ( ast , {
151
150
CallExpression : {
152
151
enter ( p ) {
153
152
const callee = p . get ( 'callee' )
@@ -230,34 +229,36 @@ export function preProcessRawCode(options: IPreProcessRawCodeOptions) {
230
229
}
231
230
// magicString.original.matchAll(regex)
232
231
for ( const regExpMatch of allArr ) {
233
- let ast : ParseResult < babel . types . File >
232
+ let ast : ParseResult < babel . types . File > | null
234
233
try {
235
- ast = parse ( regExpMatch [ 0 ] , {
234
+ ast = babel . parseSync ( regExpMatch [ 0 ] , {
236
235
sourceType : 'unambiguous'
237
236
} )
238
- traverse ( ast , {
239
- StringLiteral : {
240
- enter ( p ) {
241
- const arr = sort ( splitCode ( p . node . value ) ) . desc ( ( x ) => x . length )
242
237
243
- for ( const v of arr ) {
244
- if ( replaceMap . has ( v ) ) {
245
- ctx . addPreserveClass ( v )
238
+ ast &&
239
+ babel . traverse ( ast , {
240
+ StringLiteral : {
241
+ enter ( p ) {
242
+ const arr = sort ( splitCode ( p . node . value ) ) . desc ( ( x ) => x . length )
243
+
244
+ for ( const v of arr ) {
245
+ if ( replaceMap . has ( v ) ) {
246
+ ctx . addPreserveClass ( v )
247
+ }
246
248
}
247
249
}
248
- }
249
- } ,
250
- TemplateElement : {
251
- enter ( p ) {
252
- const arr = sort ( splitCode ( p . node . value . raw ) ) . desc ( ( x ) => x . length )
253
- for ( const v of arr ) {
254
- if ( replaceMap . has ( v ) ) {
255
- ctx . addPreserveClass ( v )
250
+ } ,
251
+ TemplateElement : {
252
+ enter ( p ) {
253
+ const arr = sort ( splitCode ( p . node . value . raw ) ) . desc ( ( x ) => x . length )
254
+ for ( const v of arr ) {
255
+ if ( replaceMap . has ( v ) ) {
256
+ ctx . addPreserveClass ( v )
257
+ }
256
258
}
257
259
}
258
260
}
259
- }
260
- } )
261
+ } )
261
262
} catch {
262
263
continue
263
264
}
0 commit comments