Skip to content

Commit bb496d7

Browse files
authored
fix(auto-import-plugin): support functional components (#2511)
1 parent 483af9e commit bb496d7

File tree

7 files changed

+167
-23
lines changed

7 files changed

+167
-23
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,54 @@ module.exports = defineConfig({
8080
})
8181
```
8282

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]
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]
125+
})
126+
]
127+
}
128+
})
129+
```
130+
83131
想了解更多自动按需导入的信息,请参考:[unplugin-vue-components](https://github.com/antfu/unplugin-vue-components)[unplugin-auto-import](https://github.com/antfu/unplugin-auto-import)
84132

85133
## 多组件引入
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'))['TinyModal']
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: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,72 @@
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+
15+
<h2>标签式调用</h2>
16+
<div class="content">
17+
<tiny-modal v-model="show1" title="基本提示框" message="窗口内容1" show-footer> </tiny-modal>
18+
<tiny-modal v-model="show2" title="基本提示框" message="窗口内容2" status="success" show-footer> </tiny-modal>
19+
<tiny-button @click="show1 = true"> 打开弹窗1 </tiny-button>
20+
<tiny-button @click="show2 = true"> 打开弹窗2 </tiny-button>
21+
</div>
622
</div>
723
</template>
824

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-
}
25+
<script setup>
26+
import { ref, onMounted } from 'vue'
27+
28+
const loadingInstance = ref(null)
29+
30+
const closeLoading = () => {
31+
loadingInstance.value.close()
32+
}
33+
34+
onMounted(() => {
35+
loadingInstance.value = TinyLoading.service({
36+
target: document.getElementById('tiny-basic-loading1')
37+
})
38+
})
39+
40+
function handleClick() {
41+
TinyNotify({
42+
type: 'info',
43+
title: '通知消息的标题',
44+
message: '通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文',
45+
position: 'top-right',
46+
duration: 5000,
47+
customClass: 'my-custom-cls'
48+
})
49+
}
50+
51+
const show1 = ref(false)
52+
const show2 = ref(false)
53+
54+
function baseClick() {
55+
const modal = TinyModal.alert('基本提示框', '标题')
56+
setTimeout(() => modal.vm.close(), 3000)
57+
}
58+
59+
function successClick() {
60+
TinyModal.alert({ message: '成功提示框', status: 'success' })
61+
}
62+
63+
function confirmClick() {
64+
TinyModal.confirm('您确定要删除吗?').then((res) => {
65+
TinyNotify({
66+
type: 'info',
67+
title: '触发回调事件',
68+
message: `点击${res}按钮`
69+
})
70+
})
2171
}
2272
</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 { TinyVueAutoImportResolver } 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: [TinyVueAutoImportResolver]
15+
}),
16+
AutoImport({
17+
resolvers: [TinyVueAutoImportResolver]
18+
})
19+
],
920
define: {
1021
'process.env': { ...process.env, TINY_MODE: 'pc' }
1122
},

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,16 @@ const supportMap = {
1212
'rspack': AutoRspack
1313
}
1414

15-
export const TinyVueResolver = (componentName) => {
15+
const TinyVueFunc = ['TinyModal', 'TinyNotify', 'TinyLoading']
16+
17+
export const TinyVueResolver = (componentName: string) => {
18+
if (TinyVueFunc.includes(componentName)) {
19+
return {
20+
name: componentName,
21+
from: '@opentiny/vue'
22+
}
23+
}
24+
1625
if (componentName.startsWith('Tiny') && !componentName.startsWith('TinyIcon')) {
1726
return {
1827
name: componentName.slice(4),

0 commit comments

Comments
 (0)