移除组件全局引用,改成 vite-plugin-components 的自动引入

This commit is contained in:
hooray 2021-08-30 09:59:58 +08:00
parent 28dd53f7cd
commit fe42f50596
7 changed files with 30 additions and 13 deletions

View File

@ -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",

View File

@ -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

View File

@ -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)
}
}

View File

@ -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'

View File

@ -0,0 +1,7 @@
import components from 'vite-plugin-components'
export default function createComponents() {
return components({
dirs: ['src/components']
})
}

View File

@ -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))

View File

@ -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'
]
})
}