Vue ํ๋ก์ ํธ์์ ์ปดํฌ๋ํธ๋ฅผ ๋งค๋ฒ import ์์ด ์๋์ผ๋ก ์ ์ญ ๋ฑ๋กํ๊ณ ์ถ๋ค๋ฉด, unplugin-vue-components ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ๋ฉด ๋งค์ฐ ๊ฐํธํ๊ฒ ํด๊ฒฐํ ์ ์์ต๋๋ค. ์ด ํ๋ฌ๊ทธ์ธ์ Vite์ Webpack์ ๋ชจ๋ ์ง์ํ๋ฉฐ, Vue 2์ Vue 3์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
๋จผ์ ํ๋ก์ ํธ์ unplugin-vue-components ํ๋ฌ๊ทธ์ธ์ ์ค์นํฉ๋๋ค.
Vite๋ฅผ ์ฌ์ฉ ์ค์ด๋ผ๋ฉด, vite.config.ts ํ์ผ์ ํ๋ฌ๊ทธ์ธ์ ์ถ๊ฐํด์ผ ํฉ๋๋ค.
// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import Components from 'unplugin-vue-components/vite';
export default defineConfig({
plugins: [
vue(),
Components({
dirs: ['src/components'], // ์ปดํฌ๋ํธ๊ฐ ์์นํ ๋๋ ํ ๋ฆฌ
extensions: ['vue'], // ์ฌ์ฉํ ํ์ผ ํ์ฅ์
deep: true, // ํ์ ๋๋ ํ ๋ฆฌ๊น์ง ๊ฒ์
}),
],
});
Webpack์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, webpack.config.js ํ์ผ์ ํ๋ฌ๊ทธ์ธ์ ์ค์ ํด์ผ ํฉ๋๋ค.
// webpack.config.js
const Components = require('unplugin-vue-components/webpack');
module.exports = {
// ๊ธฐ์กด ์ค์
plugins: [
Components({
dirs: ['src/components'],
extensions: ['vue'],
deep: true,
}),
],
};
์ค์ ์ ๋ง์น ํ, src/components ๋๋ ํ ๋ฆฌ์ ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํ๋ฉด ์๋์ผ๋ก ์ ์ญ ๋ฑ๋ก๋ฉ๋๋ค. ์ด์ ๊ฐ ํ์ผ์์ import ์์ด ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค.
<template>
<div>
<MyButton /> <!-- import ์์ด ์ฌ์ฉ ๊ฐ๋ฅ -->
</div>
</template>
unplugin-vue-components๋ ๋ค์ํ ์ค์ ์ต์
์ ์ ๊ณตํฉ๋๋ค. ์๋ฅผ ๋ค์ด:
dirs: ์ปดํฌ๋ํธ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ฅผ ์ค์ ํฉ๋๋ค.extensions: ๊ฒ์ํ ํ์ผ ํ์ฅ์๋ฅผ ์ง์ ํฉ๋๋ค.deep: ํ์ ๋๋ ํ ๋ฆฌ๊น์ง ์ปดํฌ๋ํธ๋ฅผ ๊ฒ์ํ ์ง ์ฌ๋ถ๋ฅผ ์ค์ ํฉ๋๋ค.import ์์ด ์ฌ์ฉํ ์ ์์ด ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํด์ง๋๋ค.unplugin-vue-components๋ Vue ํ๋ก์ ํธ์์ import ์์ด ์ปดํฌ๋ํธ๋ฅผ ์ ์ญ์ผ๋ก ์๋ ๋ฑ๋กํ ์ ์๊ฒ ํด์ฃผ๋ ๊ฐ๋ ฅํ ๋๊ตฌ์
๋๋ค. Vite์ Webpack์ ๋ชจ๋ ์ง์ํ๋ฉฐ, ์ค์ ๋ ๊ฐ๋จํ๋ฏ๋ก ๋ง์ Vue ํ๋ก์ ํธ์์ ์ ์ฉํ๊ฒ ํ์ฉํ ์ ์์ต๋๋ค.
์๋ ๋ฑ๋ก์ ํตํด ์ฝ๋์ ๊ฐ๋ ์ฑ์ ๋์ด๊ณ , ๋ฐ๋ณต์ ์ธ ์์ ์ ์ค์ฌ ๊ฐ๋ฐ ์์ฐ์ฑ์ ํฅ์์ํฌ ์ ์์ต๋๋ค.