ํ๋ฌ๊ทธ์ธ API โ
Vite ํ๋ฌ๊ทธ์ธ์ ๋ช ๊ฐ์ง Vite ์ ์ฉ ์ต์ ์ด ์ถ๊ฐ๋ Rolldown์ ํ๋ฌ๊ทธ์ธ ์ธํฐํ์ด์ค๋ฅผ ํ์ฅํฉ๋๋ค. ๊ทธ ๊ฒฐ๊ณผ Vite ํ๋ฌ๊ทธ์ธ์ ํ ๋ฒ ์์ฑํ๋ฉด ๊ฐ๋ฐ๊ณผ ๋น๋ ๋ชจ๋์์ ๋์ํ๊ฒ ํ ์ ์์ต๋๋ค.
์๋ ์น์ ์ ์ฝ๊ธฐ ์ ์ ๋จผ์ Rolldown ํ๋ฌ๊ทธ์ธ ๋ฌธ์๋ฅผ ์ดํด๋ณด๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
ํ๋ฌ๊ทธ์ธ ์์ฑํ๊ธฐ โ
Vite๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฒ์ฆ๋ ํจํด์ ์ ๊ณตํ๋ ค๊ณ ํฉ๋๋ค. ๋ฐ๋ผ์ ์ ํ๋ฌ๊ทธ์ธ์ ๋ง๋ค๊ธฐ ์ ์ ๋จผ์ ๊ธฐ๋ฅ ๊ฐ์ด๋๋ฅผ ํ์ธํด ํ์ํ ๊ธฐ๋ฅ์ด ์ด๋ฏธ ๋ค๋ค์ง๋์ง ํ์ธํ์ธ์. ๋ํ ํธํ๋๋ Rollup ํ๋ฌ๊ทธ์ธ๊ณผ Vite ์ ์ฉ ํ๋ฌ๊ทธ์ธ ํํ๋ก ์ ๊ณต๋๋ ์ปค๋ฎค๋ํฐ ํ๋ฌ๊ทธ์ธ๋ ๊ฒํ ํ์ธ์.
ํ๋ฌ๊ทธ์ธ์ ๋ง๋ค ๋๋ vite.config.js ์์ ์ธ๋ผ์ธ์ผ๋ก ์์ฑํ ์ ์์ต๋๋ค. ์ด๋ฅผ ์ํด ์ ํจํค์ง๋ฅผ ๋ง๋ค ํ์๋ ์์ต๋๋ค. ์ด๋ค ํ๋ฌ๊ทธ์ธ์ด ์ฌ๋ฌ ํ๋ก์ ํธ์์ ์ ์ฉํ๋ค๋ ๊ฒ์ ํ์ธํ๋ค๋ฉด, ์ํ๊ณ์ ๋ค๋ฅธ ์ฌ๋๋ค์ ๋๊ธฐ ์ํด ๊ณต์ ํ๋ ๊ฒ์ ๊ณ ๋ คํด ๋ณด์ธ์.
TIP
ํ๋ฌ๊ทธ์ธ์ ํ์ตํ๊ฑฐ๋, ๋๋ฒ๊น
ํ๊ฑฐ๋, ์์ฑํ ๋๋ ํ๋ก์ ํธ์ vite-plugin-inspect๋ฅผ ํฌํจํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ์ด ํ๋ฌ๊ทธ์ธ์ผ๋ก Vite ํ๋ฌ๊ทธ์ธ์ ์ค๊ฐ ์ํ๋ฅผ ๊ฒ์ฌํ ์ ์์ต๋๋ค. ์ค์น ํ localhost:5173/__inspect/์ ์ ์ํ๋ฉด ํ๋ก์ ํธ์ ๋ชจ๋๊ณผ ๋ณํ ์คํ์ ๊ฒ์ฌํ ์ ์์ต๋๋ค. ์ค์น ๋ฐฉ๋ฒ์ vite-plugin-inspect ๋ฌธ์๋ฅผ ํ์ธํ์ธ์. 
ํ๋ฌ๊ทธ์ธ ์์ฑ ๊ท์น โ
ํ๋ฌ๊ทธ์ธ์ด Vite ์ ์ฉ ํ ์ ์ฌ์ฉํ์ง ์๊ณ ํธํ๋๋ Rolldown ํ๋ฌ๊ทธ์ธ์ผ๋ก ๊ตฌํ๋ ์ ์๋ค๋ฉด, Rolldown ํ๋ฌ๊ทธ์ธ ๋ช ๋ช ๊ท์น์ ์ฌ์ฉํ๋ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค.
- Rolldown ํ๋ฌ๊ทธ์ธ์
rolldown-plugin-์ ๋์ฌ๋ฅผ ๊ฐ์ง ๋ช ํํ ์ด๋ฆ์ ๊ฐ์ ธ์ผ ํฉ๋๋ค. - package.json์
keywordsํ๋์rolldown-plugin๊ณผvite-pluginํค์๋๋ฅผ ํฌํจํ์ธ์.
์ด๋ ๊ฒ ํ๋ฉด ์์ Rolldown ๋๋ Rollup ๊ธฐ๋ฐ ํ๋ก์ ํธ์์๋ ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
Vite ์ ์ฉ ํ๋ฌ๊ทธ์ธ์ ๊ฒฝ์ฐ:
- Vite ํ๋ฌ๊ทธ์ธ์
vite-plugin-์ ๋์ฌ๊ฐ ์๋ ๋ช ํํ ์ด๋ฆ์ ๊ฐ์ต๋๋ค. - package.json ํ์ผ์
keywordsํญ๋ชฉ์๋vite-plugin์ ํฌํจ์ํค๋๋ก ํฉ๋๋ค. - ํ๋ฌ๊ทธ์ธ ๋ฌธ์์ Vite ํ๋ฌ๊ทธ์ธ์ผ๋ก ๊ตฌ์ฑํ ์ด์ ๋ฅผ ์ค๋ช ํ๋ ์น์ ์ ์ถ๊ฐํฉ๋๋ค. (๊ฐ๋ น 'Vite ์ ์ฉ ํ๋ฌ๊ทธ์ธ ํ ์ฌ์ฉ'๊ณผ ๊ฐ์ด ๋ง์ด์ฃ .)
ํน์ ํ๋ ์์ํฌ์์๋ง ๋์ํ๋ ํ๋ฌ๊ทธ์ธ์ ์ ๋์ฌ์ ํด๋น ํ๋ ์์ํฌ ์ด๋ฆ์ ํฌํจํด์ผ ํฉ๋๋ค.
vite-plugin-vue-๋ Vue ํ๋ฌ๊ทธ์ธ์ ์๋ฏธํฉ๋๋ค.vite-plugin-react-๋ React๋ง์ ์ง์ํ๋ Vite ํ๋ฌ๊ทธ์ธ์ ์๋ฏธํฉ๋๋ค.vite-plugin-svelte-๋ Svelte๋ง์ ์ง์ํ๋ Vite ํ๋ฌ๊ทธ์ธ์ ์๋ฏธํฉ๋๋ค.
๊ฐ์ ๋ชจ๋ ์ปจ๋ฒค์ ๋ ์ฐธ๊ณ ํ์ธ์.
ํ๋ฌ๊ทธ์ธ ์ค์ โ
ํ๋ก์ ํธ์ devDependencies์ ํ๋ฌ๊ทธ์ธ์ ์ถ๊ฐํ ๋ค, Vite ์ค์ ํ์ผ์ plugins ๋ฐฐ์ด ์ต์
์ ์ฌ์ฉํด ํ๋ฌ๊ทธ์ธ์ ๊ตฌ์ฑํ ์ ์์ต๋๋ค.
import rollupPlugin from 'rollup-plugin-feature'
import vitePlugin from 'vite-plugin-feature'
export default defineConfig({
plugins: [vitePlugin(), rollupPlugin()],
})๊ฑฐ์ง์ผ๋ก ํ๊ฐ๋๋ ํ๋ฌ๊ทธ์ธ์ ๋ฌด์๋๋ฉฐ, ์ด๋ฅผ ํตํด ํ๋ฌ๊ทธ์ธ์ ์ฝ๊ฒ ํ์ฑํํ๊ฑฐ๋ ๋นํ์ฑํํ ์ ์์ต๋๋ค.
plugins ํญ๋ชฉ์ ์ฌ๋ฌ ํ๋ฌ๊ทธ์ธ์ ํ๋์ ์์๋ก ํฌํจํ ์๋ ์์ต๋๋ค. ์ด๋ ํน์ ํ๋ ์์ํฌ๋ฅผ ํ๊น์ผ๋ก ์ฌ๋ฌ ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ์ ๊ฐ์ด ๋ณต์กํ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ ๋ฐ ์ ์ฉํฉ๋๋ค. ์ด ๋ ํ๋ฌ๊ทธ์ธ ๋ฐฐ์ด์ ์๋์ผ๋ก ๋ณํฉ๋ฉ๋๋ค.
import frameworkDevTools from 'vite-plugin-framework-devtools'
import frameworkRefresh from 'vite-plugin-framework-refresh'
export default function framework(config) {
return [frameworkRefresh(config), frameworkDevTools(config)]
}import { defineConfig } from 'vite'
import framework from 'vite-plugin-framework'
export default defineConfig({
plugins: [framework()],
})๊ฐ๋จํ ์์ ๋ค โ
TIP
Vite/Rolldown/Rollup ํ๋ฌ๊ทธ์ธ์ ์ค์ ํ๋ฌ๊ทธ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ํฉํ ๋ฆฌ ํจ์๋ก ์์ฑํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ธ ์ปจ๋ฒค์ ์ ๋๋ค. ์ด ํจ์๋ ์ฌ์ฉ์๊ฐ ํ๋ฌ๊ทธ์ธ์ ๋์์ ์ปค์คํฐ๋ง์ด์ฆํ ์ ์๋๋ก ์ต์ ์ ๋ฐ์ ์ ์์ต๋๋ค.
ํ์ผ ํ์ ๋ณํํ๊ธฐ โ
const fileRegex = /\.(my-file-ext)$/
export default function myPlugin() {
return {
name: 'transform-file',
transform: {
filter: {
id: fileRegex,
},
handler(src, id) {
return {
code: compileFileToJS(src),
map: null, // ์ฌ์ฉ ๊ฐ๋ฅํ ์์ค ๋งต์ด ์์ผ๋ฉด ์ ๊ณต
}
},
},
}
}๊ฐ์ ๋ชจ๋ ๊ฐ์ ธ์ค๊ธฐ โ
๊ฐ์ ๋ชจ๋์ ์ฌ์ฉํ๋ฉด ์ผ๋ฐ์ ์ธ ESM import ๊ตฌ๋ฌธ์ผ๋ก ๋น๋ ์์ ์ ๋ณด๋ฅผ ์์ค ํ์ผ์ ์ ๋ฌํ ์ ์์ต๋๋ค. ์ ์ฒด ์ปจ๋ฒค์
์ ๊ฐ์ ๋ชจ๋ ์ปจ๋ฒค์
์ ์ฐธ๊ณ ํ์ธ์.
import { exactRegex } from '@rolldown/pluginutils'
export default function myPlugin() {
const virtualModuleId = 'virtual:my-module'
const resolvedVirtualModuleId = '\0' + virtualModuleId
return {
name: 'my-plugin', // ํ์์ด๋ฉฐ ๊ฒฝ๊ณ ์ ์ค๋ฅ์ ํ์๋ฉ๋๋ค
resolveId: {
filter: { id: exactRegex(virtualModuleId) },
handler() {
return resolvedVirtualModuleId
},
},
load: {
filter: { id: exactRegex(resolvedVirtualModuleId) },
handler() {
return `export const msg = "from virtual module"`
},
},
}
}JavaScript์์๋ ๋ค์๊ณผ ๊ฐ์ด ๋ชจ๋์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค:
import { msg } from 'virtual:my-module'
console.log(msg)Vite์์๋ \0์ด ์ํฌํธ URL์ ํ์ฉ๋์ง ์์ผ๋ฏ๋ก, ๊ฐ๋ฐ ์ค ๋ธ๋ผ์ฐ์ ์์ \0{id} ๊ฐ์ id๊ฐ /@id/__x00__{id}๋ก ์ธ์ฝ๋ฉ๋ฉ๋๋ค. ์ด id๋ ํ๋ฌ๊ทธ์ธ ํ์ดํ๋ผ์ธ์ ๋ค์ด๊ฐ๊ธฐ ์ ์ ๋ค์ ๋์ฝ๋ฉ๋๋ฏ๋ก ํ๋ฌ๊ทธ์ธ ํ
์ฝ๋์์๋ ๋ณด์ด์ง ์์ต๋๋ค.
Rolldown ํ โ
๊ฐ๋ฐ ์ Vite ๊ฐ๋ฐ ์๋ฒ๋ Rolldown๊ณผ ๋์ผํ ๋ฐฉ์์ผ๋ก Rolldown ๋น๋ ํ ์ ํธ์ถํ๋ ํ๋ฌ๊ทธ์ธ ์ปจํ ์ด๋๋ฅผ ์์ฑํฉ๋๋ค.
๋ชจ๋ Rolldown ํ ์ ํ๊ฒฝ๋ณ ํ ์ ๋๋ค.
๋ค์ ํ ์ ์๋ฒ ์์ ์ ํ ๋ฒ ํธ์ถ๋ฉ๋๋ค:
๋ค์ ํ ์ ๋ค์ด์ค๋ ๊ฐ ๋ชจ๋ ์์ฒญ๋ง๋ค ํธ์ถ๋ฉ๋๋ค:
์ด ํ
๋ค์ Vite ์ ์ฉ ํ๋กํผํฐ๊ฐ ์ถ๊ฐ๋ ํ์ฅ options ๋งค๊ฐ๋ณ์๋ ๊ฐ์ง๋๋ค. ์์ธํ ๋ด์ฉ์ SSR ๋ฌธ์์์ ํ์ธํ ์ ์์ต๋๋ค.
์ผ๋ถ resolveId ํธ์ถ์ importer ๊ฐ์ ๋ฃจํธ์ ์ผ๋ฐ์ ์ธ index.html์ ๋ํ ์ ๋ ๊ฒฝ๋ก์ผ ์ ์์ต๋๋ค. ์ด๋ Vite์ ๋ฒ๋ค๋์ง ์์ ๊ฐ๋ฐ ์๋ฒ ํจํด์ผ๋ก ์ธํด ์ค์ Importer๋ฅผ ๋์ถํ๋ ๊ฒ์ด ํญ์ ๊ฐ๋ฅํ ๊ฒ์ ์๋๊ธฐ ๋๋ฌธ์
๋๋ค. ๋ค๋ง Vite์ ๋ฆฌ์กธ๋ธ ํ์ดํ๋ผ์ธ ๋ด์์ ์ฒ๋ฆฌ๋๋ Import์ ๊ฒฝ์ฐ, Import ๋ถ์ ๋จ๊ณ์์ Importer๋ฅผ ์ถ์ ํ ์ ์๊ธฐ์, ์ฌ๋ฐ๋ฅธ importer ๊ฐ์ ์ ๊ณตํ ์ ์์ต๋๋ค.
์๋์ ํ ์ ์๋ฒ๊ฐ ์ข ๋ฃ๋ ๋ ํธ์ถ๋ฉ๋๋ค:
moduleParsed ํ
์ ๊ฐ๋ฐ ์ค ํธ์ถ๋์ง ์์ต๋๋ค. Vite๋ ๋ ๋์ ์ฑ๋ฅ์ ์ํด ์ ์ฒด AST ํ์ฑ์ ํผํ๊ธฐ ๋๋ฌธ์
๋๋ค.
Output Generation Hooks(closeBundle ์ ์ธ)๋ ๊ฐ๋ฐ ์ค ํธ์ถ๋์ง ์์ต๋๋ค.
Vite ์ ์ฉ ํ โ
Vite์ ํ๋ฌ๊ทธ์ธ์ Vite ์ ์ฉ ํ ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฌผ๋ก ์ด๋ฌํ ํ ์ Rollup์์ ๋ฌด์๋ฉ๋๋ค.
config โ
ํ์ :
(config: UserConfig, env: { mode: 'build' | 'serve', command: string, isSsrBuild?: boolean, isPreview?: boolean }) => UserConfig | null | void์ข ๋ฅ:
async,sequential๋ฒ์: ์ ์ญ
Vite ์ค์ ์ด ํ์ ๋๊ธฐ ์ ์ ์์ ํฉ๋๋ค. ์ด ํ ์ ์ค์ ํ์ผ์ CLI ์ต์ ์ ๋ณํฉํ ์์ ์ฌ์ฉ์ ์ค์ ๊ณผ, ํ์ฌ ์ฌ์ฉ ์ค์ธ
mode๋ฐcommand๋ฅผ ํฌํจํ๋ ์ค์ ํ๊ฒฝ ๊ฐ์ฒด๋ฅผ ๋ฐ์ต๋๋ค. ์ผ๋ถ ์ค์ ๋ง ๋ด์ ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ฉด ๊ธฐ์กด ์ค์ ๊ณผ ๊น๊ฒ ๋ณํฉ๋ฉ๋๋ค. ๊ธฐ๋ณธ ๋ณํฉ ๋ฐฉ์์ผ๋ก ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์๋ค๋ฉด ์ค์ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.์์ :
js// ์ผ๋ถ ์ค์ ๋ง ๋ด์ ๊ฐ์ฒด๋ฅผ ๋ฐํํฉ๋๋ค(๊ถ์ฅ) const partialConfigPlugin = () => ({ name: 'return-partial', config: () => ({ resolve: { alias: { foo: 'bar', }, }, }), }) // ์ค์ ์ ์ง์ ์์ ํฉ๋๋ค(๋ณํฉ์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ ๋๋ง ์ฌ์ฉ) const mutateConfigPlugin = () => ({ name: 'mutate-config', config(config, { command }) { if (command === 'build') { config.root = 'foo' } }, })์ฐธ๊ณ
configํ ์์์ ๋ค๋ฅธ ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ์ง ๋ง์ธ์. ํ๋ฌ๊ทธ์ธ ์์ฒด๋ ์ด ํ ์ด ์คํ๋๊ธฐ ์ ์ ๋ชจ๋ ํ์ (Resolve)๋๋ฉฐ, ๋ฐ๋ผ์ ์๋ฌด๋ฐ ํจ๊ณผ๊ฐ ์์ต๋๋ค.
configResolved โ
ํ์ :
(config: ResolvedConfig) => void | Promise<void>์ข ๋ฅ:
async,parallel๋ฒ์: ์ ์ญ
Vite ์ค์ ๊ฐ์ด ํ์ ๋ ํ ํธ์ถ๋๋ ํ ์ ๋๋ค. ์ด ํ ์ ์ฌ์ฉํ์ฌ ์ต์ข ์ ์ผ๋ก ํ์ ๋ ์ค์ ๊ฐ์ ์ฝ๊ฑฐ๋ ์ ์ฅํ ์ ์์ต๋๋ค. ํนํ ํ๋ฌ๊ทธ์ธ์ด ์คํ ์ค์ธ ๋ช ๋ น์ ๋ฐ๋ผ ๋ค๋ฅธ ์์ ์ ์ํํด์ผ ํ๋ ๊ฒฝ์ฐ ์ ์ฉํฉ๋๋ค.
์์ :
jsconst examplePlugin = () => { let config return { name: 'read-config', configResolved(resolvedConfig) { // ํ์ ๋ ์ค์ ์ ์ ์ฅํฉ๋๋ค config = resolvedConfig }, // ๋ค๋ฅธ ํ ์์ ์ ์ฅ๋ ์ค์ ์ ์ฌ์ฉํฉ๋๋ค transform(code, id) { if (config.command === 'serve') { // dev: ๊ฐ๋ฐ ์๋ฒ์์ ํ๋ฌ๊ทธ์ธ์ ํธ์ถํฉ๋๋ค } else { // build: Rollup์์ ํ๋ฌ๊ทธ์ธ์ ํธ์ถํฉ๋๋ค } }, } }์ฐธ๊ณ ๋ก
command๊ฐ์ ๊ฐ๋ฐ ์๋ฒ์์serve์ ๋๋ค(cli์์vite,vite dev๋ฐvite serve๋ชจ๋ ๋ณ์นญ์ ๋๋ค).
configureServer โ
ํ์ :
(server: ViteDevServer) => (() => void) | void | Promise<(() => void) | void>์ข ๋ฅ:
async,sequential๊ด๋ จ ํญ๋ชฉ: ViteDevServer
๋ฒ์: ์ ์ญ
๊ฐ๋ฐ ์๋ฒ๋ฅผ ๊ตฌ์ฑํ๊ธฐ ์ํ ํ ์ ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ์ปค์คํ ๋ฏธ๋ค์จ์ด๋ฅผ ๋ด๋ถ์ connect ์ฑ์ ์ถ๊ฐํ๊ธฐ ์ํด ์ฌ์ฉํฉ๋๋ค:
jsconst myPlugin = () => ({ name: 'configure-server', configureServer(server) { server.middlewares.use((req, res, next) => { // ์ปค์คํ ์์ฒญ ์ฒ๋ฆฌ... }) }, })๋ด๋ถ ๋ฏธ๋ค์จ์ด ์ค์น ์ดํ์ ํธ์ถ๋๋ ์ปค์คํ ๋ฏธ๋ค์จ์ด ๊ตฌ์ฑํ๊ธฐ
configureServerํ ์ ๋ด๋ถ์ ๋ฏธ๋ค์จ์ด๊ฐ ์ค์น๋๊ธฐ ์ ํธ์ถ๋ฉ๋๋ค. ๋ฐ๋ผ์ ์์ ๊ฐ์ ๋ฐฉ์์ ์ปค์คํ ๋ฏธ๋ค์จ์ด๋ ๋ด๋ถ์ ๋ฏธ๋ค์จ์ด๋ณด๋ค ๋จผ์ ์คํ๋ฉ๋๋ค. ๋ง์ฝ ๋ด๋ถ์ ๋ฏธ๋ค์จ์ด๊ฐ ์ค์น๋ ์ดํ์ ์ปค์คํ ๋ฏธ๋ค์จ์ด๋ฅผ ํธ์ถํ๊ณ ์ ํ๋ค๋ฉด,configureServer์ ๋ฐํ ๊ฐ์ผ๋ก ํจ์๋ฅผ ๋ฐํํด์ฃผ์ธ์:jsconst myPlugin = () => ({ name: 'configure-server', configureServer(server) { // ๋ด๋ถ ๋ฏธ๋ค์จ์ด๋ฅผ ์ค์นํ ๋ค ํธ์ถ๋๋ // ํ์ฒ๋ฆฌ ํ ์ ๋ฐํํฉ๋๋ค return () => { server.middlewares.use((req, res, next) => { // ์ปค์คํ ์์ฒญ ์ฒ๋ฆฌ... }) } }, })์๋ฒ ์ธ์คํด์ค ์ ์ฅํ๊ธฐ
๊ฒฝ์ฐ์ ๋ฐ๋ผ ๋ค๋ฅธ ํ๋ฌ๊ทธ์ธ ํ ์์ ๊ฐ๋ฐ ์๋ฒ ์ธ์คํด์ค์ ์ ๊ทผํด์ผ ํ ์ ์์ต๋๋ค(์: WebSocket ์๋ฒ, ํ์ผ ์์คํ watcher, ๋ชจ๋ ๊ทธ๋ํ ์ ๊ทผ). ์ด ํ ์ ๋ค๋ฅธ ํ ์์ ์ ๊ทผํ ์ ์๋๋ก ์๋ฒ ์ธ์คํด์ค๋ฅผ ์ ์ฅํ๋ ๋ฐ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค:
jsconst myPlugin = () => { let server return { name: 'configure-server', configureServer(_server) { server = _server }, transform(code, id) { if (server) { // ์๋ฒ ์ฌ์ฉ... } }, } }์ฐธ๊ณ ๋ก
configureServer๋ ํ๋ก๋์ ๋ฒ์ ์ผ๋ก ๋น๋ํ ๋ ํธ์ถ๋์ง ์์ผ๋ฏ๋ก ์ ์์์ ๊ฐ์ด ํ ์ด ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ๋ฅผ ๋ถ๊ธฐํด์ค์ผ ํฉ๋๋ค.
configurePreviewServer โ
ํ์ :
(server: PreviewServer) => (() => void) | void | Promise<(() => void) | void>์ข ๋ฅ:
async,sequential๊ด๋ จ ํญ๋ชฉ: PreviewServer
๋ฒ์: ์ ์ญ
configureServer์ ๊ฐ์ง๋ง ํ๋ฆฌ๋ทฐ ์๋ฒ์ ๋ํ ๊ฒ์ ๋๋ค.configureServer์ ๋ง์ฐฌ๊ฐ์ง๋กconfigurePreviewServerํ ์ ๋ค๋ฅธ ๋ฏธ๋ค์จ์ด๊ฐ ์ค์น๋๊ธฐ ์ ์ ํธ์ถ๋ฉ๋๋ค. ๋ง์ฝ ๋ค๋ฅธ ๋ฏธ๋ค์จ์ด๊ฐ ์ค์น๋ ์ดํ์ ์ปค์คํ ๋ฏธ๋ค์จ์ด๋ฅผ ํธ์ถํ๊ณ ์ ํ๋ค๋ฉด,configurePreviewServer์ ๋ฐํ ๊ฐ์ผ๋ก ํจ์๋ฅผ ๋ฐํํด์ฃผ์ธ์. ์ด๋ ๊ฒ ํ๋ฉด ๋ด๋ถ ๋ฏธ๋ค์จ์ด๊ฐ ์ค์น๋ ํ์ ํธ์ถ๋ฉ๋๋ค:jsconst myPlugin = () => ({ name: 'configure-preview-server', configurePreviewServer(server) { // ๋ค๋ฅธ ๋ฏธ๋ค์จ์ด๋ฅผ ์ค์นํ ๋ค ํธ์ถ๋๋ // ํ์ฒ๋ฆฌ ํ ์ ๋ฐํํฉ๋๋ค return () => { server.middlewares.use((req, res, next) => { // ์ปค์คํ ์์ฒญ ์ฒ๋ฆฌ... }) } }, })
closeServer โ
ํ์ :
(context: { reason: 'restart' | 'close' }) => void | Promise<void>์ข ๋ฅ:
async,parallel๋ฒ์: ์ ์ญ
๊ฐ๋ฐ ์๋ฒ๊ฐ ์ฌ์์๋๊ฑฐ๋ ์ข ๋ฃ๋์ด ์ ๋ฆฌ๋ ๋ค ํธ์ถ๋ฉ๋๋ค. ์ผ๋ฐ์ ์ผ๋ก
configureServer์์ ์์ฑํ ๋ฆฌ์์ค๋ฅผ ํด์ ํ ๋ ์ฌ์ฉํฉ๋๋ค.context.reason์ผ๋ก ๋ ๊ฒฝ์ฐ๋ฅผ ๊ตฌ๋ถํฉ๋๋ค:'restart': ์ค์ ํ์ผ ๋ณ๊ฒฝ์ด๋server.restart()ํธ์ถ ๋ฑ์ผ๋ก ์๋ฒ๊ฐ ์ฌ์์๋ฉ๋๋ค.'close':q๋จ์ถํค๋server.close()ํธ์ถ ๋ฑ์ผ๋ก ์๋ฒ๊ฐ ์ข ๋ฃ๋ฉ๋๋ค.
jsconst myPlugin = () => { let resource return { name: 'close-server', configureServer(server) { resource = createResource() }, async closeServer({ reason }) { if (reason === 'close') { await resource.dispose() } }, } }
closePreviewServer โ
ํ์ :
() => void | Promise<void>์ข ๋ฅ:
async,parallel๋ฒ์: ์ ์ญ
closeServer์ ๊ฐ์ง๋ง ํ๋ฆฌ๋ทฐ ์๋ฒ๋ฅผ ๋์์ผ๋ก ํฉ๋๋ค. ํ๋ฆฌ๋ทฐ ์๋ฒ๋ ์ฌ์์๋์ง ์์ผ๋ฏ๋กreason์ด ์์ต๋๋ค.jsconst myPlugin = () => { let resource return { name: 'close-preview-server', configurePreviewServer(server) { resource = createResource() }, async closePreviewServer() { await resource.dispose() }, } }
transformIndexHtml โ
ํ์ :
IndexHtmlTransformHook | { order?: 'pre' | 'post', handler: IndexHtmlTransformHook }์ข ๋ฅ:
async,sequential๋ฒ์: ํ๊ฒฝ๋ณ
index.html๊ณผ ๊ฐ์ ์ง์ ์ ์ด ๋๋ HTML ํ์ผ์ ๋ณํํ๊ธฐ ์ํ ํ ์ ๋๋ค. ํ ์ ์ธ์๋ก๋ HTML ๋ฌธ์์ด๊ณผ ์ปจํ ์คํธ๋ฅผ ์ ๋ฌ๋ฐ์ต๋๋ค. ๊ฐ๋ฐ ์๋ฒ์ ๊ฒฝ์ฐ ์ปจํ ์คํธ์ViteDevServer์ธ์คํด์ค๋ฅผ ํจ๊ป ์ ๋ฌํ๋ฉฐ, ๋น๋ ์์๋ Rollup๋ ๋ฒ๋ค์ ์ ๋ฌํฉ๋๋ค.ํ ์ ๋น๋๊ธฐ์ ์ผ๋ก ๋์ํ ์ ์์ผ๋ฉฐ ๋ค์ ์ค ํ๋๋ฅผ ๋ฐํํฉ๋๋ค:
- ๋ณํ๋ HTML ๋ฌธ์์ด
- ๊ธฐ์กด HTML์ ์ถ๊ฐํ ํ๊ทธ ์ค๋ช
์ ๊ฐ์ฒด(
{ tag, attrs, children }) ๋ฐฐ์ด. ๊ฐ ํ๊ทธ๋ ๊ธฐ๋ณธ์ ์ผ๋ก<head>์์ ์ถ๊ฐ๋์ง๋ง, ์ด ์์น๋ฅผ ์ง์ ํ ์๋ ์์ต๋๋ค. { html, tags }๋ ๋ค ํฌํจํ๋ ๊ฐ์ฒด
๊ธฐ๋ณธ์ ์ผ๋ก
order๋undefined์ด๋ฉฐ, HTML์ด ๋ณํ๋ ์ดํ ํ ์ด ์ ์ฉ๋ฉ๋๋ค.order: 'pre'๋ Vite ํ๋ฌ๊ทธ์ธ ํ์ดํ๋ผ์ธ์ ๊ฑฐ์ณ์ผ ํ๋ ์คํฌ๋ฆฝํธ๋ฅผ ์ฃผ์ ํ๊ธฐ ์ํด HTML์ ๋ณํํ๊ธฐ ์ ํ ์ ์ ์ฉํฉ๋๋ค.order: 'post'๋order๊ฐ ์ ์๋์ง ์์ ๋ชจ๋ ํ ์ด ์ ์ฉ๋ ํ์ ํ ์ ์ ์ฉํฉ๋๋ค.์์ :
jsconst htmlPlugin = () => { return { name: 'html-transform', transformIndexHtml(html) { return html.replace( /<title>(.*?)<\/title>/, `<title>Title replaced!</title>`, ) }, } }ํ ์๊ทธ๋์ฒ:
tstype IndexHtmlTransformHook = ( html: string, ctx: { path: string filename: string server?: ViteDevServer bundle?: import('rolldown').OutputBundle chunk?: import('rolldown').OutputChunk originalUrl?: string }, ) => | IndexHtmlTransformResult | void | Promise<IndexHtmlTransformResult | void> type IndexHtmlTransformResult = | string | HtmlTagDescriptor[] | { html: string tags: HtmlTagDescriptor[] } interface HtmlTagDescriptor { tag: string /** * ํ์ํ ๊ฒฝ์ฐ ์์ฑ ๊ฐ์ ์๋์ผ๋ก ์ด์ค์ผ์ดํ๋ฉ๋๋ค */ attrs?: Record<string, string | boolean> children?: string | HtmlTagDescriptor[] /** * ๊ธฐ๋ณธ๊ฐ: 'head-prepend' */ injectTo?: 'head' | 'body' | 'head-prepend' | 'body-prepend' }์ฐธ๊ณ
์ด ํ ์ ์ง์ ์ ์ด ๋๋ ํ์ผ์ ์ปค์คํ ์ฒ๋ฆฌํ๋ ํ๋ ์์ํฌ(์: SvelteKit)๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ ํธ์ถ๋์ง ์์ต๋๋ค.
handleHotUpdate โ
ํ์ :
(ctx: HmrContext) => Array<ModuleNode> | void | Promise<Array<ModuleNode> | void>์ข ๋ฅ:
async,sequential๊ด๋ จ ํญ๋ชฉ: HMR API
๋ฒ์: ํ๊ฒฝ๋ณ
์ฌ์ฉ์๊ฐ ์ง์ ํ ๋ฐฉ์๋๋ก HMR ์ ๋ฐ์ดํธ๋ฅผ ์ํํฉ๋๋ค. ์ด ํ ์ ์๋์ ๊ฐ์ ์ปจํ ์คํธ ๊ฐ์ฒด๋ฅผ ์ ๋ฌ๋ฐ์ต๋๋ค:
tsinterface HmrContext { file: string timestamp: number modules: Array<ModuleNode> read: () => string | Promise<string> server: ViteDevServer }modules๋ ๋ณ๊ฒฝ๋ ํ์ผ์ ์ํฅ์ ๋ฐ๋ ๋ชจ๋์ ๋ฐฐ์ด์ ๋๋ค. Vue SFC์ ๊ฐ์ด ๋จ์ผ ํ์ผ์ด ์ฌ๋ฌ ๋ชจ๋์ ๋งคํ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด ํํ๋ก ์ ๊ณต๋ฉ๋๋ค.read๋ ํ์ผ์ ์ฝ์ด ๊ทธ ๋ด์ฉ์ ๋ฐํํ๋ ๋น๋๊ธฐ ํจ์์ ๋๋ค. ์ผ๋ถ ์์คํ ์์ ํซ ๋ฆฌ๋ก๋ฉ ์ ํ์ผ ๋ณ๊ฒฝ ์ฝ๋ฐฑ์ด ๋๋ฌด ์ด๋ฅด๊ฒ ํธ์ถ๋์ดfs.readFile๋ก ๋น ์ฝํ ์ธ ๊ฐ ๋ฐํ๋ ์ ์๊ธฐ์(๋ ์์ธํ ๋ด์ฉ์ hmr.ts ํ์ผ์ ์ฐธ๊ณ ํด์ฃผ์ธ์. - ์ฎ๊ธด์ด), ์ด ํจ์๋ฅผ ํตํด ์ ์์ ์ผ๋ก ํ์ผ์ ์ฝ์ ์ ์๋๋ก ์ ๊ณตํ๊ณ ์์ต๋๋ค.
์ด ํ ์ ๋ค์ ๋์์ ํ ์ ์์ต๋๋ค:
์ํฅ์ ๋ฐ๋ ๋ชจ๋ ๋ชฉ๋ก์ ํํฐ๋งํ๊ณ ๋ฒ์๋ฅผ ์ขํ ๋ ์ ํํ๊ฒ HMR์ด ๋์ํ๋๋ก ๊ตฌ์ฑ
๋น ๋ฐฐ์ด์ ๋ฐํํ๊ณ ์ ์ฒด ๋ฆฌ๋ก๋๋ฅผ ์ํ:
jshandleHotUpdate({ server, modules, timestamp }) { // ๋ชจ๋์ ์ง์ ๋ฌดํจํํฉ๋๋ค const invalidatedModules = new Set() for (const mod of modules) { server.moduleGraph.invalidateModule( mod, invalidatedModules, timestamp, true ) } server.ws.send({ type: 'full-reload' }) return [] }๋น ๋ฐฐ์ด์ ๋ฐํํ๊ณ ํด๋ผ์ด์ธํธ์๊ฒ ์ปค์คํ ์ด๋ฒคํธ๋ฅผ ์ ์กํ์ฌ, ์์ ํ ์ปค์คํ HMR ์ฒ๋ฆฌ๋ฅผ ์ํ:
jshandleHotUpdate({ server }) { server.ws.send({ type: 'custom', event: 'special-update', data: {} }) return [] }์ ์ปค์คํ ์ด๋ฒคํธ๋ HMR API๋ฅผ ์ฌ์ฉํ์ฌ ํธ๋ค๋ฌ๋ฅผ ๋ฑ๋กํด์ผ ํฉ๋๋ค(์ด๋ ๋์ผํ ํ๋ฌ๊ทธ์ธ์
transformํ ์ ์ฃผ์ ๋ ์ ์์ต๋๋ค):jsif (import.meta.hot) { import.meta.hot.on('special-update', (data) => { // ์ปค์คํ ์ ๋ฐ์ดํธ๋ฅผ ์ํํฉ๋๋ค }) }
ํ๋ฌ๊ทธ์ธ ์ปจํ ์คํธ ๋ฉํ โ
ํ๋ฌ๊ทธ์ธ ์ปจํ
์คํธ์ ์ ๊ทผํ ์ ์๋ ํ๋ฌ๊ทธ์ธ ํ
์ ๊ฒฝ์ฐ, Vite๋ this.meta์ ์ถ๊ฐ ํ๋กํผํฐ๋ฅผ ๋
ธ์ถํฉ๋๋ค:
this.meta.viteVersion: ํ์ฌ Vite ๋ฒ์ ๋ฌธ์์ด์ ๋๋ค(์:"8.0.0").
Rolldown ๊ธฐ๋ฐ Vite ๊ฐ์งํ๊ธฐ
this.meta.rolldownVersion์ Rolldown ๊ธฐ๋ฐ Vite(์ฆ Vite 8+)์์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ฅผ ์ฌ์ฉํด ํ์ฌ Vite ์ธ์คํด์ค๊ฐ Rolldown ๊ธฐ๋ฐ์ธ์ง ๊ฐ์งํ ์ ์์ต๋๋ค:
function versionCheckPlugin(): Plugin {
return {
name: 'version-check',
buildStart() {
if (this.meta.rolldownVersion) {
// Rolldown ๊ธฐ๋ฐ Vite์์ ์คํ ์ค์ผ ๋๋ง ์์
ํฉ๋๋ค.
} else {
// Rollup ๊ธฐ๋ฐ Vite์์ ์คํ ์ค์ด๋ฉด ๋ค๋ฅธ ์์
์ ํฉ๋๋ค.
}
},
}
}์ถ๋ ฅ ๋ฒ๋ค ๋ฉํ๋ฐ์ดํฐ โ
๋น๋ ์ค Vite๋ Rolldown์ ๋น๋ ์ถ๋ ฅ ๊ฐ์ฒด์ Vite ์ ์ฉ viteMetadata ํ๋๋ฅผ ์ถ๊ฐํฉ๋๋ค.
์ด๋ ๋ค์์ ํตํด ์ฌ์ฉํ ์ ์์ต๋๋ค:
RenderedChunk(์:renderChunk์augmentChunkHash์์)OutputChunk์OutputAsset(์:generateBundle๊ณผwriteBundle์์)
viteMetadata๋ ๋ค์์ ์ ๊ณตํฉ๋๋ค:
viteMetadata.importedCss: Set<string>viteMetadata.importedAssets: Set<string>
์ด๋ build.manifest์ ์์กดํ์ง ์๊ณ ๋ฐฉ์ถ๋ CSS์ ์ ์ ์์
์ ๊ฒ์ฌํด์ผ ํ๋ ํ๋ฌ๊ทธ์ธ์ ์์ฑํ ๋ ์ ์ฉํฉ๋๋ค.
์์:
function outputMetadataPlugin(): Plugin {
return {
name: 'output-metadata-plugin',
enforce: 'post',
generateBundle(_, bundle) {
for (const output of Object.values(bundle)) {
const css = output.viteMetadata?.importedCss
const assets = output.viteMetadata?.importedAssets
if (!css?.size && !assets?.size) continue
console.log(output.fileName, {
css: css ? [...css] : [],
assets: assets ? [...assets] : [],
})
}
},
}
}์์ฑํ ์์ ์ฐธ์กฐํ๊ธฐ โ
ํ๋ฌ๊ทธ์ธ์์ ์์
์ ์์ฑํ๋ ค๋ฉด this.emitFile({ type: 'asset', ... })์ ํธ์ถํ์ธ์. ๋ฒ๋ค์ด ์์ฑ๋๊ธฐ ์ ๊น์ง๋ ์ต์ข
ํ์ผ ์ด๋ฆ์ ์ ์ ์์ผ๋ฏ๋ก, ๋ฐํ๋ referenceId๋ฅผ ์ฌ์ฉํด ์์
URL์ ์์ฑํ ์ ์์ต๋๋ค.
JavaScript์์ โ
import.meta.ROLLDOWN_FILE_URL_<referenceId>๋ฅผ ์ฌ์ฉํ์ธ์:
const referenceId = this.emitFile({
type: 'asset',
name: 'icon.png',
source: fileContent,
})
// JavaScript ํํ์์ด๋ฏ๋ก ์ฟผ๋ฆฌ๋ ํด์๋ ๋ฌธ์์ด ์ฐ๊ฒฐ๋ก ์ถ๊ฐํฉ๋๋ค.
return `export default import.meta.ROLLDOWN_FILE_URL_${referenceId} + '#frag'`CSS ๋๋ HTML์์ โ
import.meta.ROLLDOWN_FILE_URL_<referenceId>๋ JavaScript ํํ์ ์์น์์๋ง ๋์ํฉ๋๋ค. CSS๋ HTML์์๋ ๋์ __VITE_ASSET__<referenceId>__ ํ ํฐ์ ์ฌ์ฉํ๊ณ , ์ฟผ๋ฆฌ๋ ํด์๋ฅผ ๋ฐ๋ก ๋ค์ ์ถ๊ฐํ์ธ์:
background: url(__VITE_ASSET__<referenceId>__#frag);ํ๋ฌ๊ทธ์ธ ์์ โ
Vite ํ๋ฌ๊ทธ์ธ์ Webpack ๋ก๋์ ์ ์ฌํ enforce ํ๋กํผํฐ๋ฅผ ์ถ๊ฐ์ ์ผ๋ก ์ง์ ํ์ฌ ํ๋ฌ๊ทธ์ธ ์ ํ๋ฆฌ์ผ์ด์
์ ์์๋ฅผ ์กฐ์ ํ ์ ์์ต๋๋ค. enforce ๊ฐ์ "pre" ๋๋ "post"๋ก ์ง์ ํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํตํด ์ง์ ๋๋ ํ๋ฌ๊ทธ์ธ ์์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- ๋ณ์นญ
enforce: 'pre'๋ก ์ง์ ๋ ํ๋ฌ๊ทธ์ธ- Vite ์ฝ์ด ํ๋ฌ๊ทธ์ธ
- enforce ๊ฐ์ด ์กด์ฌํ์ง ์๋ ํ๋ฌ๊ทธ์ธ
- Vite ๋น๋ ํ๋ฌ๊ทธ์ธ
enforce: 'post'๋ก ์ง์ ๋ ํ๋ฌ๊ทธ์ธ- ๋น๋ ํ ์คํ๋๋ Vite์ ํ๋ฌ๊ทธ์ธ (minify, manifest, reporting)
์ด๋ ํ
์์์๋ ๋ณ๊ฐ์
๋๋ค. Rolldown ํ
์ ํ์์ฒ๋ผ order ์์ฑ์ ์ํฅ์ ๋ณ๋๋ก ๋ฐ์ต๋๋ค.
์กฐ๊ฑด๋ถ ์ ์ฉ โ
๊ธฐ๋ณธ์ ์ผ๋ก ํ๋ฌ๊ทธ์ธ์ ๊ฐ๋ฐ ์๋ฒ ๋ฐ ๋น๋ ์ ๋ชจ๋ ํธ์ถ๋ฉ๋๋ค. ๋ง์ฝ ํ๋ฌ๊ทธ์ธ์ด ๊ฐ๋ฐ ์กฐ๊ฑด๋ถ๋ก ํธ์ถ๋์ด์ผ ํ๋ ๊ฒฝ์ฐ, apply ํ๋กํผํฐ๋ฅผ ์ฌ์ฉํ์ฌ 'build' ๋๋ 'serve' ์ค์๋ง ํ๋ฌ๊ทธ์ธ์ด ํธ์ถ๋๋๋ก ๊ตฌ์ฑํด์ฃผ์ธ์:
function myPlugin() {
return {
name: 'build-only',
apply: 'build', // ๋๋ 'serve'
}
}๋ณด๋ค ์ ํํ ์ ์ด๋ฅผ ์ํด ํจ์๋ฅผ ์ฌ์ฉํ ์๋ ์์ต๋๋ค:
apply(config, { command }) {
// SSR์ด ์๋ ๋น๋์๋ง ์ ์ฉํฉ๋๋ค.
return command === 'build' && !config.build.ssr
}Rolldown ํ๋ฌ๊ทธ์ธ ํธํ์ฑ โ
์๋น์ Rolldown / Rollup ํ๋ฌ๊ทธ์ธ์ Vite ํ๋ฌ๊ทธ์ธ์ผ๋ก ๋ฐ๋ก ๋์ํฉ๋๋ค(์: @rollup/plugin-alias ๋๋ @rollup/plugin-json). ํ์ง๋ง ๋ฒ๋ค๋ง๋์ง ์์ ๊ฐ๋ฐ ์๋ฒ ์ปจํ
์คํธ์์๋ ์ผ๋ถ ํ๋ฌ๊ทธ์ธ ํ
์ด ์๋ฏธ๊ฐ ์๊ธฐ ๋๋ฌธ์, ๋ชจ๋ ํ๋ฌ๊ทธ์ธ์ด ๋์ํ๋ ๊ฒ์ ์๋๋๋ค.
์ผ๋ฐ์ ์ผ๋ก Rolldown / Rollup ํ๋ฌ๊ทธ์ธ์ด ๋ค์ ๊ธฐ์ค์ ์ถฉ์กฑํ๋ค๋ฉด Vite ํ๋ฌ๊ทธ์ธ์ผ๋ก ๋์ํด์ผ ํฉ๋๋ค:
moduleParsedํ ์ ์ฌ์ฉํ์ง ์์ต๋๋ค.transform.inject๊ฐ์ Rolldown ์ ์ฉ ์ต์ ์ ์์กดํ์ง ์์ต๋๋ค.- ๋ฒ๋ค ๋จ๊ณ ํ ๊ณผ ์ถ๋ ฅ ๋จ๊ณ ํ ์ฌ์ด์ ๊ฐํ ๊ฒฐํฉ์ด ์์ต๋๋ค.
Rolldown / Rollup ํ๋ฌ๊ทธ์ธ์ด ๋น๋ ๋จ๊ณ์์๋ง ์๋ฏธ๊ฐ ์๋ค๋ฉด, ๋์ build.rolldownOptions.plugins ์๋์ ์ง์ ํ ์ ์์ต๋๋ค. ์ด๋ enforce: 'post'์ apply: 'build'๊ฐ ์๋ Vite ํ๋ฌ๊ทธ์ธ๊ณผ ๋์ผํ๊ฒ ๋์ํฉ๋๋ค.
๊ธฐ์กด Rolldown / Rollup ํ๋ฌ๊ทธ์ธ์ Vite ์ ์ฉ ํ๋กํผํฐ๋ฅผ ์ถ๊ฐํ ์๋ ์์ต๋๋ค:
import example from 'rolldown-plugin-example'
import { defineConfig } from 'vite'
export default defineConfig({
plugins: [
{
...example(),
enforce: 'post',
apply: 'build',
},
],
})๊ฒฝ๋ก ์ ๊ทํ โ
Vite๋ Windows์์๋ POSIX ๊ตฌ๋ถ ๊ธฐํธ( / )๋ฅผ ์ฌ์ฉํ ์ ์๋๋ก ํ์ผ์ ID๋ฅผ ํ์ธํ ๋ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ด ์ ๊ทํํฉ๋๋ค. ๋ฐ๋ฉด Rollup์ ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ๊ทธ๋๋ก ์ ์งํ๊ธฐ์, Windows์์ ํ์ผ์ ID๋ win32 ๊ตฌ๋ถ ๊ธฐํธ( \ )๋ฅผ ํฌํจํ๊ณ ์์ต๋๋ค. ๋ฐ๋ผ์ Rollup ํ๋ฌ๊ทธ์ธ์ ์ด ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ POSIX๋ก ๋ณํํ๋ @rollup/pluginutils์ normalizePath ์ ํธ๋ฆฌํฐ ํจ์๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ด ๋๋ถ์ Rollup ํ๋ฌ๊ทธ์ธ์ด Vite์์ ์ฌ์ฉ๋ ๋๋ ํ์ผ์ ID์ ๋ํ include ๋ฐ exclude ํจํด๊ณผ ๋น์ทํ ๊ฒฝ๋ก ๊ด๋ จ ์์
์ด ์ฌ๋ฐ๋ฅด๊ฒ ๋์ํฉ๋๋ค.
๋ฐ๋ผ์ ๋ง์ฝ ์๋ก์ด Vite ํ๋ฌ๊ทธ์ธ์ ์์ฑํ๋ ๊ฒฝ์ฐ, ํ์ผ์ ID์ ์ค์ ๊ฒฝ๋ก๋ฅผ ๋น๊ตํ ๋ ๋จผ์ POSIX ๊ตฌ๋ถ ๊ธฐํธ๋ฅผ ์ฌ์ฉํ๋๋ก ๊ฒฝ๋ก๋ฅผ ์ ๊ทํํด์ค์ผ ํฉ๋๋ค. ์ด๋ vite ๋ชจ๋์ normalizePath ์ ํธ๋ฆฌํฐ ํจ์๋ฅผ ์ด์ฉํ ์ ์์ต๋๋ค.
import { normalizePath } from 'vite'
normalizePath('foo\\bar') // 'foo/bar'
normalizePath('foo/bar') // 'foo/bar'ํํฐ๋ง, include/exclude ํจํด โ
Vite๋ Vite ์ ์ฉ ํ๋ฌ๊ทธ์ธ ๋ฐ ํตํฉ(Integration)์ด ํ์ค ํฌํจ(Include)/์ ์ธ(Exclude) ํํฐ๋ง ํจํด์ ์ฌ์ฉํ๋๋ก @rollup/pluginutils์ createFilter ํจ์๋ฅผ ์ ๊ณตํ๊ณ ์์ต๋๋ค. ์ฐธ๊ณ ๋ก ์ด ๋ฐฉ์์ Vite ์ฝ์ด ์์ฒด์์๋ ์ฌ์ฉํฉ๋๋ค.
ํ ํํฐ โ
Rolldown์ Rust์ JavaScript ๋ฐํ์ ์ฌ์ด์ ํต์ ์ค๋ฒํค๋๋ฅผ ์ค์ด๊ธฐ ์ํด ํ ํํฐ ๊ธฐ๋ฅ์ ๋์ ํ์ต๋๋ค. ์ด ๊ธฐ๋ฅ์ ํตํด ํ๋ฌ๊ทธ์ธ์ ํ ์ด ํธ์ถ๋์ด์ผ ํ๋ ์์ ์ ๊ฒฐ์ ํ๋ ํจํด์ ์ง์ ํ ์ ์์ผ๋ฉฐ, ๋ถํ์ํ ํ ํธ์ถ์ ํผํด์ ์ฑ๋ฅ์ ๊ฐ์ ํฉ๋๋ค.
์ด๋ Rollup 4.38.0+์ Vite 6.3.0+์์๋ ์ง์๋ฉ๋๋ค. ํ๋ฌ๊ทธ์ธ์ด ์ด์ ๋ฒ์ ๊ณผ ํ์ ํธํ๋๋๋ก ํ๋ ค๋ฉด, ํ ํธ๋ค๋ฌ ๋ด๋ถ์์๋ ํํฐ๋ฅผ ์คํํด์ผ ํฉ๋๋ค.
export default function myPlugin() {
const jsFileRegex = /\.js$/
return {
name: 'my-plugin',
// ์: .js ํ์ผ์ ๋ํด์๋ง transform์ ํธ์ถํฉ๋๋ค.
transform: {
filter: {
id: jsFileRegex,
},
handler(code, id) {
// ํ์ ํธํ์ฑ์ ์ํ ์ถ๊ฐ ๊ฒ์ฌ
if (!jsFileRegex.test(id)) return null
return {
code: transformCode(code),
map: null,
}
},
},
}
}TIP
@rolldown/pluginutils๋ exactRegex์ prefixRegex ๊ฐ์ ํ
ํํฐ์ฉ ์ ํธ๋ฆฌํฐ๋ฅผ ์ต์คํฌํธํฉ๋๋ค. ํธ์๋ฅผ ์ํด ์ด ์ ํธ๋ฆฌํฐ๋ค์ rolldown/filter์์๋ ๋ค์ ์ต์คํฌํธ๋ฉ๋๋ค.
์ฒญํฌ ์ํฌํธ ๋งต ์ ๋ณด โ
์คํ์ ๊ธฐ๋ฅ
์ด ๊ธฐ๋ฅ์ ์คํ์ ์ด๋ฉฐ ํฅํ ๋ณ๊ฒฝ๋ ์ ์์ต๋๋ค.
build.chunkImportMap ์ต์
์ ํ์ฑํํ๋ฉด ์์ฑ๋ ์ฒญํฌ์ ์ํฌํธ ๋ฌธ์ ํ์ผ ๊ฒฝ๋ก ๋์ ์ฒญํฌ๋ง๋ค ๊ณ ์ ํ ID๋ฅผ ์ฌ์ฉํฉ๋๋ค.
์ฒญํฌ ID์์ ํ์ผ ๊ฒฝ๋ก๋ก์ ๋งคํ์ ๊ฐ์ ธ์ค๋ ค๋ฉด generateBundle ๋๋ writeBundle ํ
์์ ๋ฒ๋ค์ ํฌํจํด ์ถ๋ ฅํ ์ํฌํธ ๋งต์ ์ ๊ทผํ ์ ์์ต๋๋ค. ์ํฌํธ ๋งต์ ์ด๋ฆ์ build.rolldownOptions.experimental.chunkImportMap.fileName์ ์ง์ ๋ ๊ฐ์ด๋ฉฐ, ๊ธฐ๋ณธ๊ฐ์ importmap.json์
๋๋ค.
function accessImportMap() {
let config: ResolvedConfig
return {
name: 'access-import-map',
configResolved(resolvedConfig) {
config = resolvedConfig
},
generateBundle(options, bundle) {
const chunkImportMap =
config.build.rolldownOptions.experimental?.chunkImportMap
if (chunkImportMap) {
const importMapFilename =
typeof chunkImportMap === 'object' && chunkImportMap.fileName
? chunkImportMap.fileName
: 'importmap.json'
const importMap = bundle[importMapFilename]! as OutputAsset
const mapping = JSON.parse(importMap.source).imports
console.log(mapping)
// { "./entry.hash1.js": "./entry.hash2.js" }
}
},
}
}ํด๋ผ์ด์ธํธ-์๋ฒ ์ปค๋ฎค๋์ผ์ด์ โ
Vite 2.9๋ถํฐ ํด๋ผ์ด์ธํธ์์ ํต์ ์ ์ฒ๋ฆฌํ๋ ๋ฐ ๋์์ด ๋๋ ํ๋ฌ๊ทธ์ธ์ฉ ์ ํธ์ ์ ๊ณตํฉ๋๋ค.
์๋ฒ์์ ํด๋ผ์ด์ธํธ๋ก ์ ์ก โ
ํ๋ฌ๊ทธ์ธ ์ธก์์๋ server.ws.send๋ฅผ ์ฌ์ฉํด ์ด๋ฒคํธ๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋ธ๋ก๋์บ์คํธ ํ ์ ์์ต๋๋ค:
export default defineConfig({
plugins: [
{
// ...
configureServer(server) {
server.ws.on('connection', () => {
server.ws.send('my:greetings', { msg: 'hello' })
})
},
},
],
})์ฐธ๊ณ
๋ค๋ฅธ ํ๋ฌ๊ทธ์ธ๊ณผ์ ์ถฉ๋์ ํผํ๊ธฐ ์ํด ์ด๋ฒคํธ ์ด๋ฆ์ ํญ์ ์ ๋์ฌ๋ฅผ ๋ถ์ด๋ ๊ฒ ์ด ์ข์ต๋๋ค.
ํด๋ผ์ด์ธํธ ์ธก์์๋ hot.on์ ์ฌ์ฉํด ์ด๋ฒคํธ๋ฅผ ์์ ํ ์ ์์ต๋๋ค:
// ํด๋ผ์ด์ธํธ ์ธก
if (import.meta.hot) {
import.meta.hot.on('my:greetings', (data) => {
console.log(data.msg) // hello
})
}ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์ ์ก โ
ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก ์ด๋ฒคํธ๋ฅผ ๋ณด๋ผ ๋๋ hot.send๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค:
// ํด๋ผ์ด์ธํธ ์ธก
if (import.meta.hot) {
import.meta.hot.send('my:from-client', { msg: 'Hey!' })
}์๋ฒ์์๋ server.ws.on์ ์ฌ์ฉํด ์ด๋ฒคํธ๋ฅผ ์์ ํฉ๋๋ค:
export default defineConfig({
plugins: [
{
// ...
configureServer(server) {
server.ws.on('my:from-client', (data, client) => {
console.log('Message from client:', data.msg) // Hey!
// ํ์ํ ๊ฒฝ์ฐ ํด๋น ํด๋ผ์ด์ธํธ์๋ง ์๋ตํฉ๋๋ค.
client.send('my:ack', { msg: 'Hi! I got your message!' })
})
},
},
],
})์ปค์คํ ์ด๋ฒคํธ ํ์ ์ ์ โ
๋ด๋ถ์ ์ผ๋ก Vite๋ CustomEventMap ์ธํฐํ์ด์ค๋ฅผ ํตํด ํ์ด๋ก๋ ํ์
์ ์ถ๋ก ํฉ๋๋ค. ์ด ์ธํฐํ์ด์ค๋ฅผ ํ์ฅํด ์ปค์คํ
์ด๋ฒคํธ ํ์
์ ์ ์ํ ์ ์์ต๋๋ค.
์ฐธ๊ณ
TypeScript ์ ์ธ ํ์ผ์ ๊ฐ์ ธ์ฌ ๋ .d.ts ํ์ฅ์๋ฅผ ํฌํจํด์ผ ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ํ์ฅํ๋ ค๋ ๋ชจ๋์ด ์ด๋ค ํ์ผ์ ์กด์ฌํ๋์ง TypeScript๊ฐ ์์ง ๋ชปํ ์ ์์ต๋๋ค.
import 'vite/types/customEvent.d.ts'
declare module 'vite/types/customEvent.d.ts' {
interface CustomEventMap {
'custom:foo': { msg: string }
// 'event-key': ํ์ด๋ก๋
}
}์ด ์ธํฐํ์ด์ค ํ์ฅ์ InferCustomEventPayload<T>์์ ์ด๋ฒคํธ T์ ๋ํ ํ์ด๋ก๋ ํ์
์ ์ถ๋ก ํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ์ด ์ธํฐํ์ด์ค๊ฐ ์ด๋ป๊ฒ ํ์ฉ๋๋์ง์ ๋ํ ์์ธํ ๋ด์ฉ์ HMR API ๋ฌธ์๋ฅผ ์ฐธ๊ณ ํด ์ฃผ์ธ์.
type CustomFooPayload = InferCustomEventPayload<'custom:foo'>
import.meta.hot?.on('custom:foo', (payload) => {
// payload ํ์
์ { msg: string }์
๋๋ค.
})
import.meta.hot?.on('unknown:event', (payload) => {
// payload ํ์
์ any์
๋๋ค.
})