Skip to content

Commit dc70398

Browse files
committed
fix(cli): prevent duplicate template traversal in icon transform
by claude phosphor icon test snap update REGISTRY_URL port to 3000
1 parent 10e1948 commit dc70398

File tree

3 files changed

+25
-19
lines changed

3 files changed

+25
-19
lines changed

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"clean": "node ./scripts/rimraf.js",
5454
"lint": "eslint .",
5555
"lint:fix": "eslint --fix .",
56-
"start:dev": "REGISTRY_URL=http://localhost:5173/r node dist/index.js",
56+
"start:dev": "REGISTRY_URL=http://localhost:3000/r node dist/index.js",
5757
"start": "node dist/index.js",
5858
"release": "changeset version",
5959
"pub:beta": "pnpm build && pnpm publish --no-git-checks --access public --tag beta",

packages/cli/src/utils/transformers/transform-icons.ts

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,47 +33,53 @@ export function transformIcons(opts: TransformOpts, registryIcons: Record<string
3333
return transformCount
3434
}
3535

36-
// Map<orignalIcon, targetedIcon>
36+
// Map<originalIcon, targetedIcon>
3737
const targetedIconsMap: Map<string, string> = new Map()
38+
3839
for (const scriptAST of scriptASTs) {
3940
traverseScriptAST(scriptAST, {
40-
4141
visitImportDeclaration(path) {
42-
if (!ICON_LIBRARY_IMPORTS.has(String(path.node.source.value)))
42+
const source = String(path.node.source.value)
43+
if (![...ICON_LIBRARY_IMPORTS].some(prefix => source.startsWith(prefix)))
4344
return this.traverse(path)
4445

46+
let hasChanges = false
47+
4548
for (const specifier of path.node.specifiers ?? []) {
4649
if (specifier.type === 'ImportSpecifier') {
4750
const iconName = specifier.imported.name
48-
4951
const targetedIcon = registryIcons[iconName]?.[targetLibrary]
5052

51-
if (!targetedIcon || targetedIconsMap.has(targetedIcon)) {
53+
if (!targetedIcon || targetedIconsMap.has(iconName)) {
5254
continue
5355
}
5456

5557
targetedIconsMap.set(iconName, targetedIcon)
5658
specifier.imported.name = targetedIcon
59+
hasChanges = true
5760
}
5861
}
5962

60-
if (targetedIconsMap.size > 0)
63+
if (hasChanges) {
6164
path.node.source.value = ICON_LIBRARIES[targetLibrary as keyof typeof ICON_LIBRARIES].import
65+
transformCount++
66+
}
6267

6368
return this.traverse(path)
6469
},
6570
})
71+
}
6672

67-
if (sfcAST) {
68-
traverseTemplateAST(sfcAST, {
69-
enterNode(node) {
70-
if (node.type === 'VElement' && targetedIconsMap.has(node.rawName)) {
71-
node.rawName = targetedIconsMap.get(node.rawName) ?? ''
72-
transformCount++
73-
}
74-
},
75-
})
76-
}
73+
// Move template traversal outside the loop
74+
if (sfcAST && targetedIconsMap.size > 0) {
75+
traverseTemplateAST(sfcAST, {
76+
enterNode(node) {
77+
if (node.type === 'VElement' && targetedIconsMap.has(node.rawName)) {
78+
node.rawName = targetedIconsMap.get(node.rawName) ?? ''
79+
transformCount++
80+
}
81+
},
82+
})
7783
}
7884

7985
return transformCount

packages/cli/test/utils/__snapshots__/transform-icons.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ import { Primitive } from "reka-ui";
2828

2929
exports[`transformIcons > transforms phosphor icons 1`] = `
3030
"<script setup>
31-
import { Check } from "lucide-vue-next";
31+
import { PhCheck } from '@phosphor-icons/vue';
3232
import { Primitive } from "reka-ui";
3333
</script>
3434
3535
<template>
36-
<Check />
36+
<PhCheck />
3737
<Primitive />
3838
</template>
3939
"

0 commit comments

Comments
 (0)