Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auto-import-plugin): support functional components #2511

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions examples/sites/demos/pc/webdoc/import-components.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,54 @@ module.exports = defineConfig({
})
```

#### 关于函数式组件

TinyModal,TinyNotify,TinyLoading 可使用函数形式调用,在使用时,需使用 `unplugin-auto-import` 实现自动导入,并将 TinyVueResolver 替换为 TinyVueAutoImportResolver 来实现同时支持引入普通组件和函数式组件。

Vite

```js
// vite.config.js
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { TinyVueAutoImportResolver } from '@opentiny/unplugin-tiny-vue'

module.exports = defineConfig({
configureWebpack: {
plugins: [
Components({
resolvers: [TinyVueAutoImportResolver]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两个变量,都还使用 TinyVueResolver 这个名称即可

}),
AutoImport({
resolvers: [TinyVueAutoImportResolver]
})
]
}
})
```

Webpack

```js
// webpack.config.js
const Components = require('unplugin-vue-components/webpack').default
const AutoImport = require('unplugin-auto-import/webpack').default
const TinyVueAutoImportResolver = require('@opentiny/unplugin-tiny-vue').TinyVueAutoImportResolver

module.exports = defineConfig({
configureWebpack: {
plugins: [
Components({
resolvers: [TinyVueAutoImportResolver]
}),
AutoImport({
resolvers: [TinyVueAutoImportResolver]
})
]
}
})
```

想了解更多自动按需导入的信息,请参考:[unplugin-vue-components](https://github.com/antfu/unplugin-vue-components) 和 [unplugin-auto-import](https://github.com/antfu/unplugin-auto-import)。

### 多组件引入
Expand Down
12 changes: 12 additions & 0 deletions internals/unplugin-tiny-vue/example/auto-imports.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// noinspection JSUnusedGlobalSymbols
// Generated by unplugin-auto-import
// biome-ignore lint: disable
export {}
declare global {
const TinyLoading: (typeof import('@opentiny/vue'))['TinyLoading']
const TinyModal: (typeof import('@opentiny/vue'))['TinyModal']
const TinyNotify: (typeof import('@opentiny/vue'))['TinyNotify']
}
15 changes: 15 additions & 0 deletions internals/unplugin-tiny-vue/example/components.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable */
/* prettier-ignore */
// @ts-nocheck
// Generated by unplugin-vue-components
// Read more: https://github.com/vuejs/core/pull/3399
export {}

declare module 'vue' {
export interface GlobalComponents {
RouterLink: (typeof import('vue-router'))['RouterLink']
RouterView: (typeof import('vue-router'))['RouterView']
TinyButton: (typeof import('@opentiny/vue'))['Button']
TinyModal: (typeof import('@opentiny/vue'))['TinyModal']
}
}
9 changes: 4 additions & 5 deletions internals/unplugin-tiny-vue/example/package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
{
"name": "my-vue-app",
"version": "0.0.0",
"description": "",
"author": "",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@opentiny/vue": "~3.12.1",
"@opentiny/vue-alert": "~3.13.0",
"@opentiny/vue-button-group": "~3.13.0",
"@opentiny/vue": "~3.18.0",
"@opentiny/vue-icon": "^3.12.0",
"vue": "^3.3.9"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.1.0",
"vite": "link:../node_modules/vite",
"unplugin-auto-import": "^0.18.3",
"vite": "^4.3.8",
"vite-plugin-inspect": "^0.8.3"
}
}
80 changes: 65 additions & 15 deletions internals/unplugin-tiny-vue/example/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,22 +1,72 @@
<template>
<div>
<tiny-button-group :data="groupData" v-model="checkedVal"></tiny-button-group>
<span>当前选中值:{{ checkedVal }}</span>
<tiny-alert description="type 为默认值 success"></tiny-alert>
<tiny-button @click="closeLoading">close Loading</tiny-button>
<div id="tiny-basic-loading1"></div>
<tiny-button @click="handleClick" :reset-time="0">弹出提示框</tiny-button>

<h2>函数式调用</h2>
<div class="content">
<span>弹窗模式:</span>
<tiny-button @click="baseClick"> 基本提示框 </tiny-button>
<tiny-button @click="successClick"> 成功提示框 </tiny-button>
<tiny-button @click="confirmClick"> 确认提示框 </tiny-button>
</div>

<h2>标签式调用</h2>
<div class="content">
<tiny-modal v-model="show1" title="基本提示框" message="窗口内容1" show-footer> </tiny-modal>
<tiny-modal v-model="show2" title="基本提示框" message="窗口内容2" status="success" show-footer> </tiny-modal>
<tiny-button @click="show1 = true"> 打开弹窗1 </tiny-button>
<tiny-button @click="show2 = true"> 打开弹窗2 </tiny-button>
</div>
</div>
</template>

<script>
export default {
data() {
return {
checkedVal: 'Button1',
groupData: [
{ text: 'Button1', value: 'Button1' },
{ text: 'Button2', value: 'Button2' },
{ text: 'Button3', value: 'Button3' }
]
}
}
<script setup>
import { ref, onMounted } from 'vue'

const loadingInstance = ref(null)

const closeLoading = () => {
loadingInstance.value.close()
}

onMounted(() => {
loadingInstance.value = TinyLoading.service({
target: document.getElementById('tiny-basic-loading1')
})
})

function handleClick() {
TinyNotify({
type: 'info',
title: '通知消息的标题',
message: '通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文,通知消息的正文',
position: 'top-right',
duration: 5000,
customClass: 'my-custom-cls'
})
}

const show1 = ref(false)
const show2 = ref(false)

function baseClick() {
const modal = TinyModal.alert('基本提示框', '标题')
setTimeout(() => modal.vm.close(), 3000)
}

function successClick() {
TinyModal.alert({ message: '成功提示框', status: 'success' })
}

function confirmClick() {
TinyModal.confirm('您确定要删除吗?').then((res) => {
TinyNotify({
type: 'info',
title: '触发回调事件',
message: `点击${res}按钮`
})
})
}
</script>
15 changes: 13 additions & 2 deletions internals/unplugin-tiny-vue/example/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import Inspect from 'vite-plugin-inspect'
import autoImportPlugin from '../dist/index.js'
import Components from 'unplugin-vue-components/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { TinyVueAutoImportResolver } from '../dist/index.js'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), Inspect(), autoImportPlugin()],
plugins: [
vue(),
Inspect(),
Components({
resolvers: [TinyVueAutoImportResolver]
}),
AutoImport({
resolvers: [TinyVueAutoImportResolver]
})
],
define: {
'process.env': { ...process.env, TINY_MODE: 'pc' }
},
Expand Down
20 changes: 19 additions & 1 deletion internals/unplugin-tiny-vue/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@ const supportMap = {
'rspack': AutoRspack
}

export const TinyVueResolver = (componentName) => {
const TinyVueFunc = ['TinyModal', 'TinyNotify', 'TinyLoading']

export const TinyVueAutoImportResolver = (componentName: string) => {
if (TinyVueFunc.includes(componentName)) {
return {
name: componentName,
from: '@opentiny/vue'
}
}

if (componentName.startsWith('Tiny') && !componentName.startsWith('TinyIcon')) {
return {
name: componentName.slice(4),
from: '@opentiny/vue'
}
}
}

export const TinyVueResolver = (componentName: string) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了避免破坏性变更, 你是保留了 TinyVueResolver 的名字, 特意新加了一个TinyVueAutoImportResolver 变量。

但现在看来, TinyVueAutoImportResolver 变量的能力涵盖了 TinyVueResolver, 且都是一层函数了
建议:只导出 TinyVueResolver变量, 但它的能力是 上面函数的能力即可。

if (componentName.startsWith('Tiny') && !componentName.startsWith('TinyIcon')) {
return {
name: componentName.slice(4),
Expand Down
Loading