자동 임포트 (Auto Import)
필요할 때만 PrimeVue 컴포넌트를 가져오는 auto import와 tree-shaking으로 번들을 가볍게 유지하는 방법을 알려드릴게요.
출처: 문서
본문
Auto Import
On-demand PrimeVue components with auto imports and tree-shaking.
Example
PrimeVue를 auto import와 함께 사용하는 완성된 예제는 playground에서 볼 수 있어요.
Overview
PrimeVue 컴포넌트는 개별적으로 임포트하고 설정해야 해요. 다음 섹션에서 auto import로 이 코드를 정리해 볼게요.
import { createApp } from "vue"; import PrimeVue from "primevue/config"; import InputText from 'primevue/inputtext'; import Button from 'primevue/button'; import App from './App.vue' const app = createApp(App); app.use(PrimeVue);
app.component('InputText', InputText); app.component('Button', Button);
Unplugin
unplugin-vue-components 라이브러리와 @primevue/auto-import-resolver를 사용하면 PrimeVue 컴포넌트를 자동으로 임포트하고 등록할 수 있어요. 먼저 두 패키지를 dev dependency로 설치하고, 다음으로 vite.config에서 Components 플러그인에 PrimeVueResolver를 추가하면 돼요. 이게 전부예요. 이제 초기화 코드를 다음처럼 다듬을 수 있어요. 네이밍 같은 설정은 공식 문서를 참고하세요.