Skip to content

Commit 82e9184

Browse files
committed
fix(auto-import-plugin): support functional components
1 parent a3e30c3 commit 82e9184

File tree

7 files changed

+191
-30
lines changed

7 files changed

+191
-30
lines changed

examples/sites/demos/pc/webdoc/import-components.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import { TinyVueResolver } from '@opentiny/unplugin-tiny-vue'
5555
export default {
5656
plugins: [
5757
Components({
58-
resolvers: [TinyVueResolver]
58+
resolvers: [TinyVueResolver()]
5959
})
6060
]
6161
}
@@ -73,7 +73,55 @@ module.exports = defineConfig({
7373
configureWebpack: {
7474
plugins: [
7575
Components({
76-
resolvers: [TinyVueResolver]
76+
resolvers: [TinyVueResolver()]
77+
})
78+
]
79+
}
80+
})
81+
```
82+
83+
#### 关于函数式组件
84+
85+
TinyModal,TinyNotify,TinyLoading 可使用函数形式调用,在使用时,需使用 `unplugin-auto-import` 实现自动导入。
86+
87+
Vite
88+
89+
```js
90+
// vite.config.js
91+
import Components from 'unplugin-vue-components/vite'
92+
import AutoImport from 'unplugin-auto-import/vite'
93+
import { TinyVueResolver } from '@opentiny/unplugin-tiny-vue'
94+
95+
module.exports = defineConfig({
96+
configureWebpack: {
97+
plugins: [
98+
Components({
99+
resolvers: [TinyVueResolver()]
100+
}),
101+
AutoImport({
102+
resolvers: [TinyVueResolver({ autoImport: true })]
103+
})
104+
]
105+
}
106+
})
107+
```
108+
109+
Webpack
110+
111+
```js
112+
// webpack.config.js
113+
const Components = require('unplugin-vue-components/webpack').default
114+
const AutoImport = require('unplugin-auto-import/webpack').default
115+
const TinyVueResolver = require('@opentiny/unplugin-tiny-vue').TinyVueResolver
116+
117+
module.exports = defineConfig({
118+
configureWebpack: {
119+
plugins: [
120+
Components({
121+
resolvers: [TinyVueResolver()]
122+
}),
123+
AutoImport({
124+
resolvers: [TinyVueResolver({ autoImport: true })]
77125
})
78126
]
79127
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* eslint-disable */
2+
/* prettier-ignore */
3+
// @ts-nocheck
4+
// noinspection JSUnusedGlobalSymbols
5+
// Generated by unplugin-auto-import
6+
// biome-ignore lint: disable
7+
export {}
8+
declare global {
9+
const TinyLoading: (typeof import('@opentiny/vue'))['TinyLoading']
10+
const TinyModal: (typeof import('@opentiny/vue'))['TinyModal']
11+
const TinyNotify: (typeof import('@opentiny/vue'))['TinyNotify']
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable */
2+
/* prettier-ignore */
3+
// @ts-nocheck
4+
// Generated by unplugin-vue-components
5+
// Read more: https://github.com/vuejs/core/pull/3399
6+
export {}
7+
8+
declare module 'vue' {
9+
export interface GlobalComponents {
10+
RouterLink: (typeof import('vue-router'))['RouterLink']
11+
RouterView: (typeof import('vue-router'))['RouterView']
12+
TinyButton: (typeof import('@opentiny/vue'))['Button']
13+
TinyModal: (typeof import('@opentiny/vue'))['Modal']
14+
}
15+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
{
22
"name": "my-vue-app",
3+
"version": "0.0.0",
34
"description": "",
45
"author": "",
5-
"version": "0.0.0",
66
"scripts": {
77
"dev": "vite",
88
"build": "vite build",
99
"preview": "vite preview"
1010
},
1111
"dependencies": {
12-
"@opentiny/vue": "~3.12.1",
13-
"@opentiny/vue-alert": "~3.13.0",
14-
"@opentiny/vue-button-group": "~3.13.0",
12+
"@opentiny/vue": "~3.18.0",
1513
"@opentiny/vue-icon": "^3.12.0",
1614
"vue": "^3.3.9"
1715
},
1816
"devDependencies": {
1917
"@vitejs/plugin-vue": "^4.1.0",
20-
"vite": "link:../node_modules/vite",
18+
"unplugin-auto-import": "^0.18.3",
19+
"vite": "^4.3.8",
2120
"vite-plugin-inspect": "^0.8.3"
2221
}
2322
}
Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,76 @@
11
<template>
22
<div>
3-
<tiny-button-group :data="groupData" v-model="checkedVal"></tiny-button-group>
4-
<span>当前选中值:{{ checkedVal }}</span>
5-
<tiny-alert description="type 为默认值 success"></tiny-alert>
3+
<tiny-button @click="closeLoading">close Loading</tiny-button>
4+
<div id="tiny-basic-loading1"></div>
5+
<tiny-button @click="handleClick" :reset-time="0">弹出提示框</tiny-button>
6+
7+
<h2>函数式调用</h2>
8+
<div class="content">
9+
<span>弹窗模式:</span>
10+
<tiny-button @click="baseClick"> 基本提示框 </tiny-button>
11+
<tiny-button @click="successClick"> 成功提示框 </tiny-button>
12+
<tiny-button @click="confirmClick"> 确认提示框 </tiny-button>
13+
</div>
14+
<div class="content">
15+
<span>消息模式:</span>
16+
<tiny-button @click="messageClick"> 消息提示 </tiny-button>
17+
</div>
18+
19+
<h2>标签式调用</h2>
20+
<div class="content">
21+
<tiny-modal v-model="show1" title="基本提示框" message="窗口内容1" show-footer> </tiny-modal>
22+
<tiny-modal v-model="show2" title="基本提示框" message="窗口内容2" status="success" show-footer> </tiny-modal>
23+
<tiny-button @click="show1 = true"> 打开弹窗1 </tiny-button>
24+
<tiny-button @click="show2 = true"> 打开弹窗2 </tiny-button>
25+
</div>
626
</div>
727
</template>
828

9-
<script>
10-
export default {
11-
data() {
12-
return {
13-
checkedVal: 'Button1',
14-
groupData: [
15-
{ text: 'Button1', value: 'Button1' },
16-
{ text: 'Button2', value: 'Button2' },
17-
{ text: 'Button3', value: 'Button3' }
18-
]
19-
}
20-
}
29+
<script setup>
30+
import { ref, onMounted } from 'vue'
31+
32+
const loadingInstance = ref(null)
33+
34+
const closeLoading = () => {
35+
loadingInstance.value.close()
36+
}
37+
38+
onMounted(() => {
39+
loadingInstance.value = TinyLoading.service({
40+
target: document.getElementById('tiny-basic-loading1')
41+
})
42+
})
43+
44+
function handleClick() {
45+
TinyNotify({
46+
type: 'info',
47+
title: '通知消息的标题',
48+
message: '通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文',
49+
position: 'top-right',
50+
duration: 5000,
51+
customClass: 'my-custom-cls'
52+
})
53+
}
54+
55+
const show1 = ref(false)
56+
const show2 = ref(false)
57+
58+
function baseClick() {
59+
const modal = TinyModal.alert('基本提示框', '标题')
60+
setTimeout(() => modal.vm.close(), 3000)
61+
}
62+
63+
function successClick() {
64+
TinyModal.alert({ message: '成功提示框', status: 'success' })
65+
}
66+
67+
function confirmClick() {
68+
TinyModal.confirm('您确定要删除吗?').then((res) => {
69+
TinyNotify({
70+
type: 'info',
71+
title: '触发回调事件',
72+
message: `点击${res}按钮`
73+
})
74+
})
2175
}
2276
</script>

internals/unplugin-tiny-vue/example/vite.config.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { defineConfig } from 'vite'
22
import vue from '@vitejs/plugin-vue'
33
import Inspect from 'vite-plugin-inspect'
4-
import autoImportPlugin from '../dist/index.js'
4+
import Components from 'unplugin-vue-components/vite'
5+
import AutoImport from 'unplugin-auto-import/vite'
6+
import { TinyVueResolver } from '../dist/index.js'
57

68
// https://vitejs.dev/config/
79
export default defineConfig({
8-
plugins: [vue(), Inspect(), autoImportPlugin()],
10+
plugins: [
11+
vue(),
12+
Inspect(),
13+
Components({
14+
resolvers: [TinyVueResolver()]
15+
}),
16+
AutoImport({
17+
resolvers: [TinyVueResolver({ autoImport: true })]
18+
})
19+
],
920
define: {
1021
'process.env': { ...process.env, TINY_MODE: 'pc' }
1122
},

internals/unplugin-tiny-vue/src/index.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import AutoRollup from 'unplugin-vue-components/rollup'
44
import AutoEsbuild from 'unplugin-vue-components/esbuild'
55
import AutoRspack from 'unplugin-vue-components/rspack'
66

7+
export interface TinyVueResolverOption {
8+
/**
9+
* compatible with unplugin-auto-import
10+
*
11+
* @default false
12+
*/
13+
autoImport?: boolean
14+
}
15+
716
const supportMap = {
817
'vite': AutoVite,
918
'webpack': AutoWebpack,
@@ -12,11 +21,24 @@ const supportMap = {
1221
'rspack': AutoRspack
1322
}
1423

15-
export const TinyVueResolver = (componentName) => {
16-
if (componentName.startsWith('Tiny') && !componentName.startsWith('TinyIcon')) {
17-
return {
18-
name: componentName.slice(4),
19-
from: '@opentiny/vue'
24+
const TinyVueFunc = ['TinyModal', 'TinyNotify', 'TinyLoading']
25+
26+
export const TinyVueResolver = (option: TinyVueResolverOption = {}) => {
27+
return (componentName: string) => {
28+
const { autoImport = false } = option
29+
30+
if (autoImport && TinyVueFunc.includes(componentName)) {
31+
return {
32+
name: componentName,
33+
from: '@opentiny/vue'
34+
}
35+
}
36+
37+
if (componentName.startsWith('Tiny') && !componentName.startsWith('TinyIcon')) {
38+
return {
39+
name: componentName.slice(4),
40+
from: '@opentiny/vue'
41+
}
2042
}
2143
}
2244
}
@@ -32,6 +54,6 @@ export default (name) => {
3254
const autoPlugin = supportMap[name].default || supportMap[name]
3355

3456
return autoPlugin({
35-
resolvers: [TinyVueResolver]
57+
resolvers: [TinyVueResolver()]
3658
})
3759
}

0 commit comments

Comments
 (0)