Skip to content

Commit

Permalink
fix: work with vite-plugin-icons
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jun 11, 2021
1 parent 308c77c commit 8de33e4
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 7 deletions.
9 changes: 9 additions & 0 deletions examples/vue3/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ declare module 'vue' {
Avatar: typeof import('./src/components/global/avatar.vue')['default']
UiButton: typeof import('./src/components/ui/button.vue')['default']
UiNestedCheckbox: typeof import('./src/components/ui/nested/checkbox.vue')['default']
MyCustom: typeof import('./src/CustomResolved.vue')['default']
VanRate: typeof import('vant/es')['Rate']
VanRadio: typeof import('vant/es')['Radio']
VanRadioGroup: typeof import('vant/es')['RadioGroup']
IFaSolidDiceFive: typeof import('virtual:vite-icons/fa-solid/dice-five')['default']
IHeroiconsOutlineMenuAlt2: typeof import('virtual:vite-icons/heroicons-outline/menu-alt2')['default']
IRiApps2Line: typeof import('virtual:vite-icons/ri/apps2-line')['default']
'IMdi:diceD12': typeof import('virtual:vite-icons/mdi/dice-d12')['default']
IMdiLightAlarm: typeof import('virtual:vite-icons/mdi-light/alarm')['default']
}
}

Expand Down
2 changes: 2 additions & 0 deletions examples/vue3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
"vue": "^3.0.11"
},
"devDependencies": {
"@iconify/json": "^1.1.355",
"@vitejs/plugin-vue": "^1.2.1",
"@vue/compiler-sfc": "^3.0.11",
"cross-env": "^7.0.3",
"typescript": "^4.2.4",
"vite": "^2.1.5",
"vite-plugin-components": "workspace:*",
"vite-plugin-icons": "^0.6.2",
"vite-plugin-md": "^0.6.3",
"vite-plugin-vue-svg": "^0.1.0"
}
Expand Down
9 changes: 9 additions & 0 deletions examples/vue3/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ const radio = ref('1')
</van-radio-group>
<br>
</div>

<div class="block">
<h1>Icons (5)</h1>
<i-fa-solid-dice-five />
<i-heroicons-outline-menu-alt-2 />
<i-ri-apps-2-line />
<i-mdi:dice-d12 />
<i-mdi-light-alarm />
</div>
</template>

<style scoped>
Expand Down
3 changes: 3 additions & 0 deletions examples/vue3/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig"
}
5 changes: 5 additions & 0 deletions examples/vue3/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { UserConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import ViteComponents, { VantResolver } from 'vite-plugin-components'
import Markdown from 'vite-plugin-md'
import Icons, { ViteIconsResolver } from 'vite-plugin-icons'
// @ts-expect-error
import SVG from 'vite-plugin-vue-svg'

Expand All @@ -18,6 +19,7 @@ const config: UserConfig = {
}),
Markdown(),
SVG(),
Icons(),
ViteComponents({
extensions: ['vue', 'md', 'svg'],
directoryAsNamespace: true,
Expand All @@ -31,6 +33,9 @@ const config: UserConfig = {
return '/src/CustomResolved.vue'
},
VantResolver(),
ViteIconsResolver({
componentPrefix: 'i',
}),
],
}),
],
Expand Down
30 changes: 30 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class Context {
})
}

findComponent(name: string, excludePaths: string[] = []): ComponentInfo | undefined {
findComponent(name: string, excludePaths: string[] = [], rawName?: string): ComponentInfo | undefined {
// resolve from fs
let info = this._componentNameMap[name]
if (info && !excludePaths.includes(info.path) && !excludePaths.includes(info.path.slice(1)))
Expand Down
9 changes: 9 additions & 0 deletions src/declaration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve, dirname, relative } from 'path'
import { promises as fs } from 'fs'
import { notNullish } from '@antfu/utils'
import { Context } from './context'
import { slash } from './utils'

Expand All @@ -9,16 +10,24 @@ export async function generateDeclaration(ctx: Context, root: string, filepath:
...ctx.componentCustomMap,
})
.map(({ path, name, importName }) => {
if (!name)
return undefined

const related = slash(path).startsWith('/')
? `./${relative(dirname(filepath), resolve(root, path.slice(1)))}`
: path

if (!/^\w+$/.test(name))
name = `'${name}'`

let entry = `${name}: typeof import('${slash(related)}')`
if (importName)
entry += `['${importName}']`
else
entry += '[\'default\']'
return entry
})
.filter(notNullish)

if (!lines.length)
return
Expand Down
10 changes: 5 additions & 5 deletions src/transforms/vue2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ export function Vue2Transformer(ctx: Context): Transformer {
const s = new MagicString(code)

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

if (match.index != null && matchStr && !matchStr.startsWith('_')) {
if (match.index != null && matchedName && !matchedName.startsWith('_')) {
const start = match.index
const end = start + full.length
debug(`| ${matchStr}`)
const name = pascalCase(matchStr)
debug(`| ${matchedName}`)
const name = pascalCase(matchedName)
componentPaths.push(name)
const component = ctx.findComponent(name, [sfcPath])
const component = ctx.findComponent(name, [sfcPath], matchedName)
if (component) {
const var_name = `__vite_components_${no}`
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/vue3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function Vue3Transformer(ctx: Context): Transformer {
debug(`| ${matchedName}`)
const name = pascalCase(matchedName)
componentPaths.push(name)
const component = ctx.findComponent(name, [sfcPath])
const component = ctx.findComponent(name, [sfcPath], matchedName)
if (component) {
const var_name = `__vite_components_${no}`
head.push(stringifyComponentImport({ ...component, name: var_name }, ctx))
Expand Down

0 comments on commit 8de33e4

Please sign in to comment.