Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fbf704

Browse files
feat(transformer-attributify-jsx): migrate from babel to oxc for JSX parsing (#5068)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 3f4de79 commit 6fbf704

File tree

9 files changed

+366
-201
lines changed

9 files changed

+366
-201
lines changed

interactive/nuxt.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ export default defineNuxtConfig({
6666
include: [/\.vue$/, /\.md$/],
6767
},
6868
optimizeDeps: {
69-
exclude: externals,
69+
exclude: [
70+
...externals,
71+
'oxc-parser',
72+
],
7073
},
7174
define: {
7275
'process.env.VSCODE_TEXTMATE_DEBUG': 'false',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@iconify-json/teenyicons": "catalog:icons",
4848
"@iconify-json/twemoji": "catalog:icons",
4949
"@iconify-json/uim": "catalog:icons",
50+
"@oxc-parser/binding-wasm32-wasi": "catalog:docs",
5051
"@shikijs/vitepress-twoslash": "catalog:docs",
5152
"@types/connect": "catalog:types",
5253
"@types/css-tree": "catalog:types",

packages-presets/transformer-attributify-jsx/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@
3333
"dev": "tsdown --config-loader unrun --watch"
3434
},
3535
"dependencies": {
36-
"@babel/parser": "catalog:babel",
37-
"@babel/traverse": "catalog:babel",
38-
"@unocss/core": "workspace:*"
36+
"@unocss/core": "workspace:*",
37+
"oxc-parser": "catalog:oxc",
38+
"oxc-walker": "catalog:oxc"
3939
},
4040
"devDependencies": {
41-
"@types/babel__traverse": "catalog:types",
4241
"magic-string": "catalog:utils"
4342
}
4443
}

packages-presets/transformer-attributify-jsx/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { SourceCodeTransformer, UnoGenerator } from '@unocss/core'
22
import type MagicString from 'magic-string'
33
import { getEnvFlags } from '#integration/env'
44
import { toArray } from '@unocss/core'
5-
import { attributifyJsxBabelResolver } from './resolver/babel'
5+
import { attributifyJsxOxcResolver } from './resolver/oxc'
66
import { attributifyJsxRegexResolver } from './resolver/regex'
77

88
export type FilterPattern = Array<string | RegExp> | string | RegExp | null
@@ -94,11 +94,11 @@ export default function transformerAttributifyJsx(options: TransformerAttributif
9494
}
9595

9696
try {
97-
await attributifyJsxBabelResolver(params)
97+
await attributifyJsxOxcResolver(params)
9898
}
9999
catch (error) {
100100
console.warn(
101-
`[@unocss/transformer-attributify-jsx]: Babel resolver failed for "${id}", falling back to regex resolver:`,
101+
`[@unocss/transformer-attributify-jsx]: Oxc resolver failed for "${id}", falling back to regex resolver:`,
102102
error,
103103
)
104104
await attributifyJsxRegexResolver(params)

packages-presets/transformer-attributify-jsx/src/resolver/babel.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { AttributifyResolverParams } from '..'
2+
import { parseSync } from 'oxc-parser'
3+
import { walk } from 'oxc-walker'
4+
5+
export async function attributifyJsxOxcResolver(params: AttributifyResolverParams) {
6+
const { code, id, uno, isBlocked } = params
7+
const tasks: Promise<void>[] = []
8+
const ast = parseSync(id, code.toString(), {
9+
sourceType: 'module',
10+
})
11+
12+
if (ast.errors?.length) {
13+
throw new Error(`Oxc parse errors:\n${ast.errors.join('\n')}`)
14+
}
15+
16+
walk(ast.program, {
17+
enter(node) {
18+
if (node.type !== 'JSXAttribute')
19+
return
20+
21+
if (node.value === null) {
22+
const attr = node.name.type === 'JSXNamespacedName'
23+
? `${node.name.namespace.name}:${node.name.name.name}`
24+
: node.name.name
25+
26+
if (isBlocked(attr))
27+
return
28+
29+
tasks.push(
30+
uno.parseToken(attr).then((matched) => {
31+
if (matched) {
32+
code.appendRight(node.end, '=""')
33+
}
34+
}),
35+
)
36+
}
37+
},
38+
})
39+
40+
await Promise.all(tasks)
41+
}

playground/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default defineConfig({
5858
'@iconify/utils/lib/loader/install-pkg',
5959
'@iconify/utils/lib/loader/node-loader',
6060
'@iconify/utils/lib/loader/node-loaders',
61+
'oxc-parser',
6162
],
6263
},
6364
build: {

0 commit comments

Comments
 (0)