Skip to content

Commit 8de33e4

Browse files
committed
fix: work with vite-plugin-icons
1 parent 308c77c commit 8de33e4

File tree

10 files changed

+74
-7
lines changed

10 files changed

+74
-7
lines changed

examples/vue3/components.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ declare module 'vue' {
1515
Avatar: typeof import('./src/components/global/avatar.vue')['default']
1616
UiButton: typeof import('./src/components/ui/button.vue')['default']
1717
UiNestedCheckbox: typeof import('./src/components/ui/nested/checkbox.vue')['default']
18+
MyCustom: typeof import('./src/CustomResolved.vue')['default']
19+
VanRate: typeof import('vant/es')['Rate']
20+
VanRadio: typeof import('vant/es')['Radio']
21+
VanRadioGroup: typeof import('vant/es')['RadioGroup']
22+
IFaSolidDiceFive: typeof import('virtual:vite-icons/fa-solid/dice-five')['default']
23+
IHeroiconsOutlineMenuAlt2: typeof import('virtual:vite-icons/heroicons-outline/menu-alt2')['default']
24+
IRiApps2Line: typeof import('virtual:vite-icons/ri/apps2-line')['default']
25+
'IMdi:diceD12': typeof import('virtual:vite-icons/mdi/dice-d12')['default']
26+
IMdiLightAlarm: typeof import('virtual:vite-icons/mdi-light/alarm')['default']
1827
}
1928
}
2029

examples/vue3/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
"vue": "^3.0.11"
1212
},
1313
"devDependencies": {
14+
"@iconify/json": "^1.1.355",
1415
"@vitejs/plugin-vue": "^1.2.1",
1516
"@vue/compiler-sfc": "^3.0.11",
1617
"cross-env": "^7.0.3",
1718
"typescript": "^4.2.4",
1819
"vite": "^2.1.5",
1920
"vite-plugin-components": "workspace:*",
21+
"vite-plugin-icons": "^0.6.2",
2022
"vite-plugin-md": "^0.6.3",
2123
"vite-plugin-vue-svg": "^0.1.0"
2224
}

examples/vue3/src/App.vue

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ const radio = ref('1')
6767
</van-radio-group>
6868
<br>
6969
</div>
70+
71+
<div class="block">
72+
<h1>Icons (5)</h1>
73+
<i-fa-solid-dice-five />
74+
<i-heroicons-outline-menu-alt-2 />
75+
<i-ri-apps-2-line />
76+
<i-mdi:dice-d12 />
77+
<i-mdi-light-alarm />
78+
</div>
7079
</template>
7180

7281
<style scoped>

examples/vue3/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig"
3+
}

examples/vue3/vite.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { UserConfig } from 'vite'
33
import Vue from '@vitejs/plugin-vue'
44
import ViteComponents, { VantResolver } from 'vite-plugin-components'
55
import Markdown from 'vite-plugin-md'
6+
import Icons, { ViteIconsResolver } from 'vite-plugin-icons'
67
// @ts-expect-error
78
import SVG from 'vite-plugin-vue-svg'
89

@@ -18,6 +19,7 @@ const config: UserConfig = {
1819
}),
1920
Markdown(),
2021
SVG(),
22+
Icons(),
2123
ViteComponents({
2224
extensions: ['vue', 'md', 'svg'],
2325
directoryAsNamespace: true,
@@ -31,6 +33,9 @@ const config: UserConfig = {
3133
return '/src/CustomResolved.vue'
3234
},
3335
VantResolver(),
36+
ViteIconsResolver({
37+
componentPrefix: 'i',
38+
}),
3439
],
3540
}),
3641
],

pnpm-lock.yaml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class Context {
152152
})
153153
}
154154

155-
findComponent(name: string, excludePaths: string[] = []): ComponentInfo | undefined {
155+
findComponent(name: string, excludePaths: string[] = [], rawName?: string): ComponentInfo | undefined {
156156
// resolve from fs
157157
let info = this._componentNameMap[name]
158158
if (info && !excludePaths.includes(info.path) && !excludePaths.includes(info.path.slice(1)))

src/declaration.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { resolve, dirname, relative } from 'path'
22
import { promises as fs } from 'fs'
3+
import { notNullish } from '@antfu/utils'
34
import { Context } from './context'
45
import { slash } from './utils'
56

@@ -9,16 +10,24 @@ export async function generateDeclaration(ctx: Context, root: string, filepath:
910
...ctx.componentCustomMap,
1011
})
1112
.map(({ path, name, importName }) => {
13+
if (!name)
14+
return undefined
15+
1216
const related = slash(path).startsWith('/')
1317
? `./${relative(dirname(filepath), resolve(root, path.slice(1)))}`
1418
: path
19+
20+
if (!/^\w+$/.test(name))
21+
name = `'${name}'`
22+
1523
let entry = `${name}: typeof import('${slash(related)}')`
1624
if (importName)
1725
entry += `['${importName}']`
1826
else
1927
entry += '[\'default\']'
2028
return entry
2129
})
30+
.filter(notNullish)
2231

2332
if (!lines.length)
2433
return

src/transforms/vue2.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ export function Vue2Transformer(ctx: Context): Transformer {
2424
const s = new MagicString(code)
2525

2626
for (const match of code.matchAll(/_c\(['"](.+?)["']([,)])/g)) {
27-
const [full, matchStr, append] = match
27+
const [full, matchedName, append] = match
2828

29-
if (match.index != null && matchStr && !matchStr.startsWith('_')) {
29+
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
3030
const start = match.index
3131
const end = start + full.length
32-
debug(`| ${matchStr}`)
33-
const name = pascalCase(matchStr)
32+
debug(`| ${matchedName}`)
33+
const name = pascalCase(matchedName)
3434
componentPaths.push(name)
35-
const component = ctx.findComponent(name, [sfcPath])
35+
const component = ctx.findComponent(name, [sfcPath], matchedName)
3636
if (component) {
3737
const var_name = `__vite_components_${no}`
3838
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))

src/transforms/vue3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function Vue3Transformer(ctx: Context): Transformer {
3131
debug(`| ${matchedName}`)
3232
const name = pascalCase(matchedName)
3333
componentPaths.push(name)
34-
const component = ctx.findComponent(name, [sfcPath])
34+
const component = ctx.findComponent(name, [sfcPath], matchedName)
3535
if (component) {
3636
const var_name = `__vite_components_${no}`
3737
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))

0 commit comments

Comments
 (0)