Skip to content

Latest commit

ย 

History

History
115 lines (74 loc) ยท 4.46 KB

2022-09-29-module-federation-for-vite-and-rollup.md

File metadata and controls

115 lines (74 loc) ยท 4.46 KB

Module Federation for vite & rollup

์ €๋ฒˆ์ฃผ์—๋Š” Webpack 5์— ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์œผ๋กœ Module Federation์„ ํ†ตํ•ด Micro Frontend Architecture ๊ตฌ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฒˆ์ฃผ์—๋Š” Module Federation์„ ๋‹ค๋ฅธ build ๋„๊ตฌ(vite)์—์„œ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๊ฒช์€ ๊ณผ์ •์„ ์„ค๋ช…ํ•ด๋“œ๋ฆด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ vue ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑ์„ ์œ„ํ•ด ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์€ vue-cli(webpack), vite๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ๋‹ค๋‹ˆ๊ณ  ์žˆ๋Š” ํšŒ์‚ฌ์—์„œ๋Š” vue3 + vite, vue3 + webpack์„ ํ”„๋กœ์ ํŠธ๋“ค์ด ๊ตฌ์„ฑ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€๋ถ€๋ถ„ ํ”„๋กœ์ ํŠธ๋“ค์„ vue3 + vite๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋˜์–ด์žˆ๋Š” ์ƒํƒœ์ž…๋‹ˆ๋‹ค.

๋น„ํŠธ๋Š” Vue ์ฐฝ์‹œ์ž Evan You๊ฐ€ ๋งŒ๋“  ์ƒˆ๋กœ์šด ํ”„๋ก ํŠธ์—”๋“œ ๋„๊ตฌ๋กœ ํ”„๋ž‘์Šค์–ด๋กœ ๋น ๋ฅด๋‹ค๋ฅผ ์˜๋ฏธํ•˜๋ฉฐ ๋น ๋ฅด๊ณ  ๊ฐ„๊ฒฐํ•œ ๋ชจ๋˜ ์›น ํ”„๋กœ์ ํŠธ ๊ฐœ๋ฐœ ๊ฒฝํ—˜์— ์ดˆ์ ์„ ๋งž์ถฐ ํƒ„์ƒํ•œ ๋นŒ๋“œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค.

์ƒํ™ฉ์— ๋”ฐ๋ผ ESbuild์™€ Rollup ๋‘๊ฐ€์ง€๋ฅผ ๋ฒˆ๋“ค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ฐœ๋ฐœ์šฉ ์„œ๋ฒ„์—์„œ๋Š” Esbuild๋ฅผ ํ†ตํ•ด ๋ฒˆ๋“ค๋ง ํ•˜๊ณ  ํ”„๋Ÿฌ๋•์…˜ ๋ฒ„์ „์œผ๋กœ ๋นŒ๋“œ๋ฅผ ์œ„ํ•ด์„œ๋Š” Rollup์„ ํ†ตํ•ด ๋ฒˆ๋“ค๋ง ํ•ฉ๋‹ˆ๋‹ค.

Esbuild๋Š” ๊ต‰์žฅํžˆ ๋น ๋ฅธ ์†๋„๋กœ ๋ฒˆ๋“ค๋ง์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์œผ๋‚˜, ๋ฒˆ๋“ค๋ง์— ํ•„์ˆ˜์ ์œผ๋กœ ์š”๊ตฌ๋˜๋Š” ๊ธฐ๋Šฅ์ธ ์ฝ”๋“œ ๋ถ„ํ• (Code-splitting) ๋ฐ CSS์™€ ๊ด€๋ จ๋œ ์ฒ˜๋ฆฌ๊ฐ€ ์•„์ง ๋ฏธ๋น„ํ•ฉ๋‹ˆ๋‹ค. Vite์—์„œ ์‚ฌ์šฉ์ค‘์ธ Rollup์€ ์ด์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ๊ฒ€์ฆ๋˜์—ˆ๊ณ  ์œ ์—ฐํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” ๊ตฌํ˜„๋˜์–ด ์žˆ๊ธฐ์— ํ˜„์žฌ๋กœ์„œ๋Š” ์ด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ํ–ฅํ›„ Esbuild๊ฐ€ ์•ˆ์ •ํ™” ๋˜์—ˆ์„ ๋•Œ ์–ด๋–ค ํ”„๋กœ๋•์…˜ ๋ฒˆ๋“ค๋ง ๋„๊ตฌ๊ฐ€ ์ ์ ˆํ•  ๊ฒƒ์ธ์ง€ ๋‹ค์‹œ ๋…ผ์˜ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

์‹ค์ œ๋กœ ESbuild ๋กœ๋“œ๋งต์„ ์‚ดํŽด ๋ณด์‹œ๋ฉด Code Splitting ๊ทธ๋ฆฌ๊ณ  CSS content type ์™€ ๊ฐ™์€ ์ด์Šˆ๋“ค์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ์ง„ํ–‰์ค‘์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ๋กค์—… ์ˆ˜์ค€์—์„œ ํ•ด๊ฒฐํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋…ผ์˜ํ•  ๊ฐ€์น˜๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด์ง€๋งŒ Vite์˜ ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚ฉ๋‹ˆ๋‹ค.

vite์—์„œ module federation์˜ (์œ ์‚ฌํ•œ) ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค.

(์‹ค์ œ๋กœ ํ˜„์—…์—์„œ๋Š” webpack๊ณผ ๊ฐ™์ด ๋งŽ์€ ๊ฐœ๋ฐœ์ž๋“ค์ด ์‚ฌ์šฉํ•˜๋ฉฐ ๊ฒ€์ฆ๋˜์ง€ ์•Š์•„์„œ ์‚ฌ์šฉํ•˜๊ธฐ ํž˜๋“ค๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.)

vite.config.js

import federation from '@originjs/vite-plugin-federation';

export default defineConfig({
  plugins: [
    vue(),
    federation({
      name: 'home',
      filename: 'remoteEntry.js',
      exposes: {
        './Component1': {
          name: 'component',
          import: './src/components/Component1.vue',
        },
      },
      remotes: {
        remote1: 'http://localhost:5000/assets/remoteEntry.js',
      },
      shared: ['vue'],
    }),
  ],
  build: {
    target: 'esnext',
    rollupOptions: {
      output: {
        format: 'esm',
        entryFileNames: 'assets/[name].js',
      },
    },
  },
});

federation

federation ์—์„œ๋Š” module federation๊ณผ ๋™์ผํ•œ ๊ตฌ์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • name

    ํ•ด๋‹น์•ฑ์˜ ์œ ์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์ค‘๋ณต๋œ ์ด๋ฆ„์ด ์žˆ์œผ๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

  • filename

    ํ•ด๋‹น ์•ฑ์„ ๋‹ค๋ฅธ ์•ฑ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด Manifest ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

  • remotes

    ํ•ด๋‹น ์•ฑ์ด ์‚ฌ์šฉํ•  ๋ฆฌ๋ชจํŠธ ์•ฑ๋“ค์„ ์ •์˜ํ•˜๋Š” ๊ณณ์ž…๋‹ˆ๋‹ค.

  • exposes

    ์™ธ๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด expose ์‹œํ‚ฌ ๋ชจ๋“ˆ๋“ค์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

  • shared

    ๋Ÿฐํƒ€์ž„์— Federated๋œ ์•ฑ ๊ฐ„์— ๊ณต์œ ํ•˜๊ฑฐ๋‚˜ ๊ณต์œ ๋ฐ›์„ ์˜์กด์„ฑ ํŒจํ‚ค์ง€๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

Evan You์˜ ๋ง์ฒ˜๋Ÿผ rollup ๋‹จ๊ณ„์—์„œ build ์‹œ์ผœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์˜ต์…˜์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค.

  • target

    ๋ธŒ๋ผ์šฐ์ € ํ˜ธ์™„์„ฑ์„ ์œ„ํ•ด es๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

  • rollupOptions

    • format

      ์ƒ์„ฑ๋œ ๋ฒˆ๋“คํŒŒ์ผ์˜ ํ˜•์‹์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

    • entryFileNames

      ์ง„์ž…์ ์ด ๋  ํŒŒ์ผ ์œ„์น˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

์ œ๊ฐ€ ๋งŒ๋“  micro-vue-vite-demo

vite ๊ณต์‹ ๋ฌธ์„œ(๋ฒˆ์—ญ)