diff --git a/examples/sites/demos/pc/webdoc/import-components.md b/examples/sites/demos/pc/webdoc/import-components.md index d21fd7395e..9148752182 100644 --- a/examples/sites/demos/pc/webdoc/import-components.md +++ b/examples/sites/demos/pc/webdoc/import-components.md @@ -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] + }), + 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)。 ### 多组件引入 diff --git a/internals/unplugin-tiny-vue/example/auto-imports.d.ts b/internals/unplugin-tiny-vue/example/auto-imports.d.ts new file mode 100644 index 0000000000..73d664f70d --- /dev/null +++ b/internals/unplugin-tiny-vue/example/auto-imports.d.ts @@ -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'] +} diff --git a/internals/unplugin-tiny-vue/example/components.d.ts b/internals/unplugin-tiny-vue/example/components.d.ts new file mode 100644 index 0000000000..b638187ec5 --- /dev/null +++ b/internals/unplugin-tiny-vue/example/components.d.ts @@ -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'] + } +} diff --git a/internals/unplugin-tiny-vue/example/package.json b/internals/unplugin-tiny-vue/example/package.json index aec2fa8516..f296e120a1 100644 --- a/internals/unplugin-tiny-vue/example/package.json +++ b/internals/unplugin-tiny-vue/example/package.json @@ -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" } } diff --git a/internals/unplugin-tiny-vue/example/src/App.vue b/internals/unplugin-tiny-vue/example/src/App.vue index 35941f9e83..23415b852c 100644 --- a/internals/unplugin-tiny-vue/example/src/App.vue +++ b/internals/unplugin-tiny-vue/example/src/App.vue @@ -1,22 +1,72 @@ - - 当前选中值:{{ checkedVal }} - + close Loading + + 弹出提示框 + + 函数式调用 + + 弹窗模式: + 基本提示框 + 成功提示框 + 确认提示框 + + + 标签式调用 + + + + 打开弹窗1 + 打开弹窗2 + - diff --git a/internals/unplugin-tiny-vue/example/vite.config.js b/internals/unplugin-tiny-vue/example/vite.config.js index 115c877802..b5551a2aab 100644 --- a/internals/unplugin-tiny-vue/example/vite.config.js +++ b/internals/unplugin-tiny-vue/example/vite.config.js @@ -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' } }, diff --git a/internals/unplugin-tiny-vue/src/index.ts b/internals/unplugin-tiny-vue/src/index.ts index e7a4a6244b..693efb9f77 100644 --- a/internals/unplugin-tiny-vue/src/index.ts +++ b/internals/unplugin-tiny-vue/src/index.ts @@ -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) => { if (componentName.startsWith('Tiny') && !componentName.startsWith('TinyIcon')) { return { name: componentName.slice(4),