# ์ฝ”๋“œ ๋ถ„ํ• 

์ฝ”๋“œ ๋ถ„ํ• (Code Splitting)์€ ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜(Single Page Application)์€ ์ดˆ๊ธฐ ์‹คํ–‰์‹œ์— ํ•„์š”ํ•œ ์›น ์ž์›์„ ๋ชจ๋‘ ๋‹ค์šด ๋ฐ›๋Š” ํŠน์ง•์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ ๋ถ„ํ• ์„ ํ™œ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ดˆ๊ธฐ ๋กœ๋”ฉ์‹œ์— ๋ชจ๋“  ์›น ์ž์›์„ ๋‹ค์šด๋ฐ›์ง€ ์•Š๊ณ  ํ•„์š”ํ•œ ์‹œ์ ์— ๋‹ค์šด ๋ฐ›์•„ ์„ฑ๋Šฅ ์ƒ์˜ ์ด์ ์ด ์ƒ๊น๋‹ˆ๋‹ค. ์ฐธ๊ณ ๋กœ ๋ทฐ์—์„œ ์ฝ”๋“œ ๋ถ„ํ• ์ด ๊ฐ€๋Šฅํ•œ ์ด์œ ๋Š” ๋ทฐ์˜ ๋น„๋™๊ธฐ ์ปดํฌ๋„ŒํŠธ ๋กœ๋”ฉ (opens new window)๊ณผ ์›นํŒฉ์˜ ์ฝ”๋“œ ์Šคํ”Œ๋ฆฌํŒ… ๊ธฐ์ˆ  (opens new window) ๋•ํƒ์ž…๋‹ˆ๋‹ค.

# ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์กฐ๊ฑด

๋‹ค๋งŒ ์ง€์—ฐ๋œ ๋กœ๋”ฉ(Lazy Loading) ๋ฐฉ๋ฒ•์„ ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์„ ๊ฐ–๊ณ  ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. ์‹ฑ๊ธ€ ํŒŒ์ผ ์ปดํฌ๋„ŒํŠธ ์ฒด๊ณ„ (.vue)
  2. ์›นํŒฉ - ๋ชจ๋“ˆ ๋ฒˆ๋“ค๋Ÿฌ (2.x ์ด์ƒ)
  3. ๋ฐ”๋ฒจ Syntax-dynamic-import ํ”Œ๋Ÿฌ๊ทธ์ธ (opens new window)

# ๋ฐ”๋ฒจ Syntax-dynamic-import ์„ค์น˜ ๋ฐฉ๋ฒ•

๋ทฐ CLI๋กœ ์ƒ์„ฑํ•œ ๊ฒฝ์šฐ์—๋Š” ์ด๋ฏธ 1๋ฒˆ๊ณผ 2๋ฒˆ์„ ๋งŒ์กฑํ•˜๊ณ  ๊ณ„์‹คํ…Œ์ง€๋งŒ 3๋ฒˆ์˜ ๊ฒฝ์šฐ์—๋Š” ๋ณ„๋„์˜ ์„ค์น˜๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ”๋ฒจ Syntax-dynamic-import ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์น˜๋Š” ์•„๋ž˜์˜ ์ ˆ์ฐจ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

  1. syntax-dynamic-import ํ”Œ๋Ÿฌ๊ทธ์ธ (opens new window) ์„ค์น˜
npm install --save-dev babel-plugin-syntax-dynamic-import
  1. .babelrc ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ํ”Œ๋Ÿฌ๊ทธ์ธ ์„ค์ • ์ถ”๊ฐ€
{
  "plugins": ["syntax-dynamic-import"]
}

# ๋ผ์šฐํ„ฐ์˜ ์ฝ”๋“œ ๋ถ„ํ•  ๋ฌธ๋ฒ•

์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ทฐ ๋ผ์šฐํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋ผ์šฐํŒ… ๋ณ„๋กœ ์ฝ”๋“œ๋ฅผ ๋ถ„ํ• ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

{
	path: 'url ์ด๋ฆ„',
	component: () => import('์ปดํฌ๋„ŒํŠธ ์ด๋ฆ„')
}

์œ„์™€ ๊ฐ™์€ ๋ฐฉ๋ฒ•์„ ์ง€์—ฐ๋œ ๋กœ๋”ฉ(Lazy Loading)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ทœ๋ชจ๊ฐ€ ์ปค์ ธ ์‹ฑ๊ธ€ ํŽ˜์ด์ง€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ดˆ๊ธฐ ํ™”๋ฉด ๋กœ๋”ฉ ์‹œ๊ฐ„์„ ์ค„์ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์™œ๋ƒ๋ฉด ํ™”๋ฉด์ด 10๊ฐœ์ธ ์›น ์•ฑ์ด ์žˆ๋Š”๋ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ฒ˜์Œ ์‹œ์ž‘ํ–ˆ์„ ๋•Œ ์“ฐ์ง€๋„ ์•Š์„ ๋‚˜๋จธ์ง€ ํ™”๋ฉด 9๊ฐœ๋ฅผ ๋‹ค ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ ๋ณด๋‹ค๋Š” ํŠน์ • ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ํ™”๋ฉด์˜ ๋‚ด์šฉ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ๋”ฉ ์†๋„ ๋ฉด์—์„œ ๋” ํšจ์œจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

์œ„ ํ™˜๊ฒฝ์„ ๊ตฌ์„ฑํ•˜๊ณ  ๋‚˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํŠน์ • URL์— ๋”ฐ๋ฅธ ์ฝ”๋“œ ๋ถ„ํ• ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

const routes = [
	{
    path: '/login',
    name: 'login',
    component: () => import('./LoginPage.vue')
  },
	{
    path: '/main',
    name: 'main',
    component: () => import('./MainPage.vue')
  },
];

new VueRouter({
	mode: 'history',
	routes: routes,
});