From fe42f50596d01933e502108e39dd16e8680db6ae Mon Sep 17 00:00:00 2001 From: hooray <304327508@qq.com> Date: Mon, 30 Aug 2021 09:59:58 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E7=BB=84=E4=BB=B6=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E5=BC=95=E7=94=A8=EF=BC=8C=E6=94=B9=E6=88=90=20vite-p?= =?UTF-8?q?lugin-components=20=E7=9A=84=E8=87=AA=E5=8A=A8=E5=BC=95?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + pnpm-lock.yaml | 19 ++++++++++++++++++- src/components/registerGlobalComponent.js | 7 ------- src/main.js | 4 ---- vite/plugins/components.js | 7 +++++++ vite/plugins/index.js | 2 ++ vite/plugins/restart.js | 3 ++- 7 files changed, 30 insertions(+), 13 deletions(-) delete mode 100644 src/components/registerGlobalComponent.js create mode 100644 vite/plugins/components.js diff --git a/package.json b/package.json index 8ea4a05..b3d6de2 100644 --- a/package.json +++ b/package.json @@ -49,6 +49,7 @@ "svgo": "^2.5.0", "vite": "^2.5.1", "vite-plugin-banner": "^0.1.3", + "vite-plugin-components": "^0.13.2", "vite-plugin-compression": "^0.3.5", "vite-plugin-html": "^2.1.0", "vite-plugin-mock": "^2.9.6", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e2273ae..864cf39 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -30,6 +30,7 @@ specifiers: tinymce: ^5.9.1 vite: ^2.5.1 vite-plugin-banner: ^0.1.3 + vite-plugin-components: ^0.13.2 vite-plugin-compression: ^0.3.5 vite-plugin-html: ^2.1.0 vite-plugin-mock: ^2.9.6 @@ -76,6 +77,7 @@ devDependencies: svgo: registry.nlark.com/svgo/2.5.0 vite: registry.nlark.com/vite/2.5.1 vite-plugin-banner: registry.nlark.com/vite-plugin-banner/0.1.3 + vite-plugin-components: registry.nlark.com/vite-plugin-components/0.13.2_vite@2.5.1 vite-plugin-compression: registry.nlark.com/vite-plugin-compression/0.3.5_vite@2.5.1 vite-plugin-html: registry.nlark.com/vite-plugin-html/2.1.0_vite@2.5.1 vite-plugin-mock: registry.nlark.com/vite-plugin-mock/2.9.6_mockjs@1.1.0+vite@2.5.1 @@ -5208,7 +5210,7 @@ packages: dev: true registry.nlark.com/ms/2.1.2: - resolution: {integrity: sha1-0J0fNXtEP0kzgqjrPM0YOHKuYAk=, registry: https://registry.npm.taobao.org/, tarball: https://registry.nlark.com/ms/download/ms-2.1.2.tgz?cache=0&sync_timestamp=1629940432911&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fms%2Fdownload%2Fms-2.1.2.tgz} + resolution: {integrity: sha1-0J0fNXtEP0kzgqjrPM0YOHKuYAk=, registry: https://registry.npm.taobao.org/, tarball: https://registry.nlark.com/ms/download/ms-2.1.2.tgz} name: ms version: 2.1.2 dev: true @@ -6173,6 +6175,21 @@ packages: version: 0.1.3 dev: true + registry.nlark.com/vite-plugin-components/0.13.2_vite@2.5.1: + resolution: {integrity: sha1-nhsbkMqKgXlU9axezCTbvfHMFoQ=, registry: https://registry.npm.taobao.org/, tarball: https://registry.nlark.com/vite-plugin-components/download/vite-plugin-components-0.13.2.tgz} + id: registry.nlark.com/vite-plugin-components/0.13.2 + name: vite-plugin-components + version: 0.13.2 + peerDependencies: + vite: ^2.0.0 + dependencies: + debug: registry.nlark.com/debug/4.3.2 + fast-glob: registry.nlark.com/fast-glob/3.2.7 + magic-string: 0.25.7 + minimatch: 3.0.4 + vite: registry.nlark.com/vite/2.5.1 + dev: true + registry.nlark.com/vite-plugin-compression/0.3.5_vite@2.5.1: resolution: {integrity: sha1-HlM460PmASjebW8isqq/Dj3AwX8=, registry: https://registry.npm.taobao.org/, tarball: https://registry.nlark.com/vite-plugin-compression/download/vite-plugin-compression-0.3.5.tgz} id: registry.nlark.com/vite-plugin-compression/0.3.5 diff --git a/src/components/registerGlobalComponent.js b/src/components/registerGlobalComponent.js deleted file mode 100644 index 908c9e2..0000000 --- a/src/components/registerGlobalComponent.js +++ /dev/null @@ -1,7 +0,0 @@ -export default function registerGlobalComponent(app) { - const componentsContext = import.meta.globEager('./**/index.vue') - for (const path in componentsContext) { - const component = componentsContext[path].default - app.component(component.name, component) - } -} diff --git a/src/main.js b/src/main.js index a567ba7..add3524 100644 --- a/src/main.js +++ b/src/main.js @@ -21,10 +21,6 @@ globalProperties(app) import directive from '@/util/directive' directive(app) -// 全局组件自动注册 -import registerGlobalComponent from '@/components/registerGlobalComponent' -registerGlobalComponent(app) - // 加载 svg 图标 import 'vite-plugin-svg-icons/register' diff --git a/vite/plugins/components.js b/vite/plugins/components.js new file mode 100644 index 0000000..dd997b6 --- /dev/null +++ b/vite/plugins/components.js @@ -0,0 +1,7 @@ +import components from 'vite-plugin-components' + +export default function createComponents() { + return components({ + dirs: ['src/components'] + }) +} diff --git a/vite/plugins/index.js b/vite/plugins/index.js index ef25207..a16b586 100644 --- a/vite/plugins/index.js +++ b/vite/plugins/index.js @@ -2,6 +2,7 @@ import vue from '@vitejs/plugin-vue' import createRestart from './restart' import createHtml from './html' +import createComponents from './components' import createSvgIcon from './svg-icon' import createMock from './mock' import createCompression from './compression' @@ -12,6 +13,7 @@ export default function createVitePlugins(viteEnv, isBuild = false) { const vitePlugins = [vue()] !isBuild && vitePlugins.push(createRestart()) vitePlugins.push(createHtml(viteEnv, isBuild)) + vitePlugins.push(createComponents()) vitePlugins.push(createSvgIcon(isBuild)) vitePlugins.push(createMock()) isBuild && vitePlugins.push(...createCompression(viteEnv)) diff --git a/vite/plugins/restart.js b/vite/plugins/restart.js index 02800af..eedc4a6 100644 --- a/vite/plugins/restart.js +++ b/vite/plugins/restart.js @@ -3,7 +3,8 @@ import ViteRestart from 'vite-plugin-restart' export default function createRestart() { return ViteRestart({ restart: [ - '.env.development' + '.env.development', + 'vite/plugins/index.js' ] }) }