Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/dynamic-component-registration-vite.md
1---2title: Use import.meta.glob for Dynamic Component Registration in Vite3impact: MEDIUM4impactDescription: require.context from Webpack doesn't work in Vite projects5type: gotcha6tags: [vue3, component-registration, vite, dynamic-import, migration, webpack]7---89# Use import.meta.glob for Dynamic Component Registration in Vite1011**Impact: MEDIUM** - When migrating from Webpack to Vite or starting a new Vite project, the `require.context` pattern for dynamically registering components won't work. Vite uses `import.meta.glob` instead. Using the wrong approach will cause build errors or runtime failures.1213## Task Checklist1415- [ ] Replace `require.context` with `import.meta.glob` in Vite projects16- [ ] Update component registration patterns when migrating from Vue CLI to Vite17- [ ] Use `{ eager: true }` for synchronous loading when needed18- [ ] Handle async components appropriately with `defineAsyncComponent`1920**Incorrect (Webpack pattern - doesn't work in Vite):**21```javascript22// main.js - WRONG for Vite23import { createApp } from 'vue'24import App from './App.vue'2526const app = createApp(App)2728// This Webpack-specific API doesn't exist in Vite29const requireComponent = require.context(30'./components/base',31false,32/Base[A-Z]\w+\.vue$/33)3435requireComponent.keys().forEach(fileName => {36const componentConfig = requireComponent(fileName)37const componentName = fileName38.split('/')39.pop()40.replace(/\.\w+$/, '')4142app.component(componentName, componentConfig.default || componentConfig)43})4445app.mount('#app')46```4748**Correct (Vite pattern):**49```javascript50// main.js - Correct for Vite51import { createApp } from 'vue'52import App from './App.vue'5354const app = createApp(App)5556// Vite's glob import - eager loading for synchronous registration57const modules = import.meta.glob('./components/base/Base*.vue', { eager: true })5859for (const path in modules) {60// Extract component name from path: './components/base/BaseButton.vue' -> 'BaseButton'61const componentName = path.split('/').pop().replace('.vue', '')62app.component(componentName, modules[path].default)63}6465app.mount('#app')66```6768## Lazy Loading with Async Components6970```javascript71// main.js - Lazy loading variant72import { createApp, defineAsyncComponent } from 'vue'73import App from './App.vue'7475const app = createApp(App)7677// Without { eager: true }, returns functions that return Promises78const modules = import.meta.glob('./components/base/Base*.vue')7980for (const path in modules) {81const componentName = path.split('/').pop().replace('.vue', '')82// Wrap in defineAsyncComponent for lazy loading83app.component(componentName, defineAsyncComponent(modules[path]))84}8586app.mount('#app')87```8889## Glob Pattern Examples9091```javascript92// All .vue files in a directory (not recursive)93import.meta.glob('./components/*.vue', { eager: true })9495// All .vue files recursively96import.meta.glob('./components/**/*.vue', { eager: true })9798// Specific naming pattern99import.meta.glob('./components/Base*.vue', { eager: true })100101// Multiple patterns102import.meta.glob([103'./components/Base*.vue',104'./components/App*.vue'105], { eager: true })106107// Exclude patterns108import.meta.glob('./components/**/*.vue', {109eager: true,110ignore: ['**/*.test.vue', '**/*.spec.vue']111})112```113114## TypeScript Support115116```typescript117// main.ts - with proper typing118import { createApp, Component } from 'vue'119import App from './App.vue'120121const app = createApp(App)122123const modules = import.meta.glob<{ default: Component }>(124'./components/base/Base*.vue',125{ eager: true }126)127128for (const path in modules) {129const componentName = path.split('/').pop()!.replace('.vue', '')130app.component(componentName, modules[path].default)131}132133app.mount('#app')134```135136## Migration Checklist (Webpack to Vite)137138| Webpack | Vite |139|---------|------|140| `require.context(dir, recursive, regex)` | `import.meta.glob(pattern, options)` |141| Synchronous by default | Use `{ eager: true }` for sync |142| `.keys()` returns array | Returns object with paths as keys |143| Returns module directly | Access via `.default` for ES modules |144145## Reference146- [Vite - Glob Import](https://vitejs.dev/guide/features.html#glob-import)147- [Vue.js Component Registration](https://vuejs.org/guide/components/registration.html)148