๋ฐ˜์‘ํ˜•

Intro


  • 1๊ฐœ ํŒŒ์ผ์— ๋ชจ๋“  ์ฝ”๋“œ๊ฐ€ ๋“ค์–ด๊ฐ„ ๊ฒƒ ๋ณด๋‹ค ์ฝ”๋“œ๋“ค์ด ์ž˜๊ฐœ ์ชผ๊ฐœ์ ธ ์žˆ๋Š”๊ฒŒ ๋” ์ข‹๋‹ค.
  • ๋ชจ๋“ˆ์€ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋” ์ข‹์€ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.
  • ๋ชจ๋“ˆ์„ ์ž˜ ์•Œ์•„๋‘๋ฉด ์ฝ”๋“œ๋ฅผ ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ชจ๋“ˆ ๋•๋ถ„์— ์ฝ”๋“œ๋“ค์„ ์ž‘์€ ํŒŒ์ผ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ณ , import / export ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ชจ๋“ˆ์€ ํ•จ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ๋ชจ๋“ˆ ์Šค์ฝ”ํ”„์— ๋„ฃ๊ณ , ๋ชจ๋“ˆ ์Šค์ฝ”ํ”„๋ฅผ ํ†ตํ•ด ๋ชจ๋“ˆ ํ•จ์ˆ˜ ์‚ฌ์ด์—์„œ ๋ณ€์ˆ˜๋ฅผ ๊ณต์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋‹ค์ด๋‚˜๋ฏน ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜๋ฉด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋กœ๋”ฉ์„ ๋” ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Named Export


import { name } from "file";

 

  • ํŒŒ์ผ ํ˜น์€ ๋ชจ๋“ˆ์ด ์›ํ•˜๋Š” ๋งŒํผ์˜ named exports๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค
  • import ํ•˜๋ ค๋Š” ์ด๋ฆ„์€ export ํ–ˆ๋˜ ํ•จ์ˆ˜(๋ณ€์ˆ˜) ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค

 

// math.js
export const plus = (a, b) => a + b;
export const minus = (a, b) => a - b;
export const divide = (a, b) => a / b;

// main.js
import { plus } from './math';

 

as ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ import ํ•  ์ˆ˜ ์žˆ๋‹ค

import { plus as add } from './math';

 

* as name ํ‚ค์›Œ๋“œ๋กœ exportํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ import ํ•  ์ˆ˜ ์žˆ๋‹ค. * ํ‚ค์›Œ๋“œ๋กœ math.js ํŒŒ์ผ์— ์žˆ๋Š” ๋ชจ๋“  ๋‚ด์šฉ์„ importํ•œ ํ›„ myMath ๊ฐ์ฒด์— ๋„ฃ๋Š” ์˜ˆ์‹œ.

 

๋ชจ๋“  ํ•จ์ˆ˜๋ฅผ ํ•œ๋ฒˆ์— importํ•˜๋Š” ๊ฒฝ์šฐ โ–ผ

// math.js
export const plus = (a, b) => a + b;
export const minus = (a, b) => a - b;
export const divide = (a, b) => a / b;

// main.js
import * as myMath from './math';
myMath.plus(2, 2);

 

Default Export


import name from "file";

 

  • ๊ฐ ํŒŒ์ผ๋งˆ๋‹ค ํ•œ๊ฐœ์˜ default export๋งŒ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค
  • importํ•  ๋•Œ {} ์ค‘๊ด„ํ˜ธ๋ฅผ ์“ฐ์ง€ ์•Š์•„๋„ ๋œ๋‹ค
  • ์›ํ•˜๋Š” ์ด๋ฆ„์œผ๋กœ importํ•  ์ˆ˜ ์žˆ๋‹ค (์ฝ”๋“œ ์ผ๊ด€์„ฑ์„ ์œ„ํ•ด ํŒŒ์ผ ์ด๋ฆ„๊ณผ ๋™์ผํ•œ ์ด๋ฆ„ ์‚ฌ์šฉ ์ถ”์ฒœ)
  • default export์™€ named export๋ฅผ ์„ž์–ด์„œ ์“ธ ์ˆ˜ ์žˆ๋‹ค

 

math.js์— ์žˆ๋Š” ํ•จ์ˆ˜๋ฅผ ์ผ์ผ์ด import ํ•˜๋Š” ๊ฒฝ์šฐ โ–ผ

// math.js
export const plus = (a, b) => a + b;
export const minus = (a, b) => a - b;
export const divide = (a, b) => a / b;

// main.js
import { plus, minus, dvide } from './math';

 

์—ฌ๋Ÿฌ ํ•จ์ˆ˜(ํ˜น์€ ๋ณ€์ˆ˜)๋ฅผ 1๊ฐœ์˜ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ default export ํ•˜๋Š” ๊ฒฝ์šฐ โ–ผ

// math.js
const plus = (a, b) => a + b;
const minus = (a, b) => a - b;
const divide = (a, b) => a / b;
export default { plus, minus, divide };

// main.js
import math from './math';

 

1๊ฐœ ํ•จ์ˆ˜๋งŒ default export ํ•˜๋Š” ๊ฒฝ์šฐ โ–ผ

// db.js
const connectToDB = () => { /* magic */ };
export default connectToDB;

// main.js
import connect from './db';

 

named export์™€ default export๋ฅผ ํ•จ๊ป˜ ์“ฐ๋Š” ๊ฒฝ์šฐ โ–ผ

// -------- db.js --------
const connectToDB = () => { /* magic */ };
export const getUrl = () => { /* magic */ };
export default connectToDB;

// -------- main.js --------
import connect, { getUrl } from './db';

// default ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค
import { default as connect, getUrl } from './db';

// * ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋ชจ๋“  ๋‚ด์šฉ์„ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜๋„ ์žˆ๋‹ค
// ์ด๋•Œ default ํ”„๋กœํผํ‹ฐ๋Š” default export(connectToDB)๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค
import * as db from './db';
console.log(db); // { getUrl, default }

 

ํ•จ์ˆ˜ ์„ ์–ธ๋ถ€์™€ ๋–จ์–ด์ง„ ๊ณณ์—์„œ default export ํ•˜๋Š” ๊ฒฝ์šฐ โ–ผ

const sayHi = (name) => {
  console.log(`Hello ${name}`);
};

export { sayHi as default };

 

๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ


๋‹ค๋ฅธ ํŒŒ์ผ์— ์žˆ๋Š” named export์™€ default export๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ผ ์ˆ˜ ์žˆ๋‹ค. import → export ํ•˜๋Š” ๊ตฌ๋ฌธ์„ export ... from '...' ํ•œ ์ค„๋กœ ์ž‘์„ฑํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•˜๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค. ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ ํ•  ๋•Œ * ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด named export๋งŒ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋•Œ๋ฌธ์— default export ๊ตฌ๋ฌธ๋„ ๋ณ„๋„๋กœ ์ž‘์„ฑํ•ด์ค˜์•ผ ํ•œ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์‹œ์˜ ํด๋” ๊ตฌ์กฐ โ–ผ

math/
    โ”œโ”€โ”€ math.js
    โ””โ”€โ”€ index.js
user/
    โ””โ”€โ”€ calculator.js

 

์ˆ˜ํ•™ ๊ด€๋ จ ํ•จ์ˆ˜๋ฅผ ๋ชจ์•„๋†“์€ ํŒŒ์ผ — math/math.js โ–ผ

export const plus = (a, b) => a + b;
export const minus = (a, b) => a - b;
const divide = (a, b) => a / b;

export { divide as default } // export default (a, b) => a / b;

 

math.js ํŒŒ์ผ ๋‚ด์šฉ์„ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ — math/index.js โ–ผ

// 1) named export ๋ถˆ๋Ÿฌ์˜จ ํ›„ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ
export * from './math.js'; // { plus, minus }

// 2) export default๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ
export { default } from './math.js'; // { default }

 

'./math/math.js' ์ฒ˜๋Ÿผ ํด๋” ์ด๋ฆ„๊ณผ, ํ•ด๋‹น ํด๋” ์•ˆ์— ์žˆ๋Š” ํŒŒ์ผ ์ด๋ฆ„์ด ๊ฐ™๋‹ค๋ฉด import ํ•  ๋•Œ ๊ฐ™์€ ์ด๋ฆ„์„ ์ค‘๋ณตํ•ด์„œ ์ ๋Š” ์…ˆ์ด ๋œ๋‹ค. ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด import ๊ตฌ๋ฌธ์—์„œ './math' ํด๋”๋ช…๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๋œ๋‹ค.

 

์ฐธ๊ณ ๋กœ ํด๋”๋ฅผ import ํ•˜๋ฉด ๋ฒˆ๋“ค๋Ÿฌ๋Š” ํ•ด๋‹น ํด๋”์˜ index.* ํŒŒ์ผ์„ ์ฐพ๋Š”๋‹ค. a.com ๊ฐ™์€ ๋„๋ฉ”์ธ์„ ์ž…๋ ฅํ•ด์„œ ์›นํŽ˜์ด์ง€์— ์ ‘์†ํ–ˆ์„ ๋•Œ index.html ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๋Š” ๊ฒƒ๊ณผ ๋™์ผํ•œ ๊ฐœ๋….

 

๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐํ•œ ํ•จ์ˆ˜ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ — user/calculator.js โ–ผ

// (๋ฐฉ๋ฒ•1) * ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” import ์˜ˆ์‹œ
import divide, { minus, plus } from './math';

// (๋ฐฉ๋ฒ•2) * ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ import ์˜ˆ์‹œ
import * as math from './math'; // { minus, plus, default }

 

๐Ÿ’ก export default๋ฅผ ๋ถˆ๋Ÿฌ์˜จ ํ›„ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ผ ๋•Œ export { default as divide } ... ์ด๋ ‡๊ฒŒ as ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด named export๊ฐ€ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ import ๊ตฌ๋ฌธ์€ import { divide } ... ํ˜•ํƒœ๋กœ ์ž…๋ ฅํ•ด์•ผ ๋œ๋‹ค.

 

๐Ÿ’ก ๋ฆฌ์•กํŠธ import ๊ตฌ๋ฌธ์€ ํŒŒ์ผ ํ™•์žฅ์ž๋ฅผ ์ž…๋ ฅํ•˜์ง€ ์•Š๋Š”๋‹ค(React CRA ํฌํ•จ ์›นํŒฉ ๋ฒˆ๋“ค๋Ÿฌ ์ฒ˜๋ฆฌ๋ฐฉ์‹)

 

ํด๋” ๊ตฌ์กฐ ์ฐธ๊ณ  Josh Comeau โ–ผ

src/
โ””โ”€ components/
   โ””โ”€ FileViewer/
      โ”œโ”€ Directory.js
      โ”œโ”€ File.js
      โ”œโ”€ FileContent.js
      โ”œโ”€ FileViewer.helpers.js
      โ”œโ”€ FileViewer.js
      โ”œโ”€ index.js
      โ””โ”€ Sidebar.js

 

index.js ํŒŒ์ผ์—์„œ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ โ–ผ

// FileViewer.js์˜ named export๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ
export * from './FileViewer';

// FileViewer.js์˜ export default๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋‹ค์‹œ ๋‚ด๋ณด๋‚ด๊ธฐ
export { default } from './FileViewer';

 

๋น ๋ฅธ ๋กœ๋”ฉ์„ ์œ„ํ•œ ํŒ


ํ•„์š”ํ•œ ๊ฒƒ๋งŒ import ํ•œ๋‹ค

ํŠน์ • ํŒŒ์ผ์˜ ํŠน์ • ํ•จ์ˆ˜๋งŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋ผ๋ฉด(์•„๋ž˜ ์˜ˆ์‹œ์—์„  plus ํ•จ์ˆ˜) ํŒŒ์ผ์˜ ๋ชจ๋“  ๋‚ด์šฉ์„ ํ•œ๋ฒˆ์— import ํ•˜๋Š” default export๋ณด๋‹จ named export๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ํ•œํŽธ์œผ๋ก  ๋ชจ๋“ ๊ฑธ default export์— ๋‹ด์•„๋ฒ„๋ฆฌ๋Š” ๊ฒƒ๋ณด๋‹จ named export๋กœ ํ•จ์ˆ˜๋ฅผ ์ •๋ฆฌํ•˜๋Š”๊ฒŒ ๋” ๊น”๋”ํ•˜๊ธฐ๋„ ํ•˜๋‹ค.

// math.js
export const plus = (a, b) => a + b;
export const minus = (a, b) => a - b;
export const divide = (a, b) => a / b;

// *.js
import { plus } from './math';

 

Dynamic Import ํ™œ์šฉ

๐Ÿ’ก import(module) ๊ตฌ๋ฌธ์€ ๋ชจ๋“ˆ์ด ๋‚ด๋ณด๋‚ด๋Š”๊ฒƒ์„ ๋ชจ๋‘ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋‹ด์€ ์ดํ–‰๋œ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ˜ธ์ถœ ์œ„์น˜์— ์ œํ•œ์ด ์—†์œผ๋ฏ€๋กœ, ์ฝ”๋“œ ์–ด๋””์„œ๋“  ๋™์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

import ๋ฐฉ์‹์€ ํŒŒ์ผ ์œ„์—์„œ๋ถ€ํ„ฐ ๋ชจ๋“  ๋‚ด์šฉ์„ import ํ•œ๋‹ค. ์ด๋Š” ๋กœ๋”ฉ ์†๋„๋ฅผ ๋Šฆ์ถ”๋Š” ์›์ธ์ด ๋œ๋‹ค. ์œ ์ €๊ฐ€ ์‚ฌ์šฉํ•˜๋“  ์•ˆ ํ•˜๋“  ์›น์‚ฌ์ดํŠธ์˜ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ๋‹ค์šด๋กœ๋“œํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

๋Œ€์‹  Dynamic Import๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์ œ ์œ ์ €๊ฐ€ ์‚ฌ์šฉํ•  ๋ชจ๋“ˆ๋งŒ import ํ•  ์ˆ˜ ์žˆ๋‹ค. ์•„๋ž˜ ์˜ˆ์‹œ์—์„œ ์œ ์ €๊ฐ€ ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ๋งŒ Math ๋ชจ๋“ˆ์„ import ํ•˜๊ณ  ์‚ฌ์šฉํ•œ๋‹ค. ์š”์ฒญ์ด ์žˆ์„ ๋•Œ๋งŒ ๋ชจ๋“ˆ์„ ๋กœ๋”ฉํ•˜๋Š” ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์ด๋‹ค.

// Dynamic Import ์˜ˆ์‹œ
function doMath() {
  import('./math').then((math) => math.plus(2, 2));
}
btn.addEventListener('click', doMath);

// async, await ํ™œ์šฉ
async function doMath() {
  const math = await import('./math');
  // named export๋ผ๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  // const { plus, minus } = await import('./math')

  // default exportํ•œ ๋ชจ๋“ˆ์€ default ํ”„๋กœํผํ‹ฐ๋กœ ์ฝ์„ ์ˆ˜ ์žˆ์Œ
  // const { default } = await import('./say.js')
  math.plus(2, 2);
}
btn.addEventListener('click', doMath);

 

๐Ÿ’ก ๋ฆฌ์•กํŠธ์—์„  React.lazy() ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ์ฝ”๋“œ ๋ถ„ํ•  → ๋ฒˆ๋“ค ํฌ๊ธฐ๋ฅผ ์ค„์—ฌ์„œ ์ดˆ๊ธฐ ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. React.lazy()๋Š” ์ฃผ๋กœ ๋ผ์šฐํŒ… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค. ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ import() ๊ตฌ๋ฌธ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ default export๋งŒ ์ง€์›ํ•œ๋‹ค. named export๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ์ถ”๊ฐ€์ ์ธ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

// OtherComponent๊ฐ€ default export ์ผ ๋•Œ
const OtherComponent = React.lazy(() => import('./OtherComponent'));

// OtherComponent๊ฐ€ named export ์ผ ๋•Œ
const OtherComponent = React.lazy(() =>
  import('./OtherComponent').then((module) => ({
    default: module.OtherComponent,
  })),
);

 

๋ ˆํผ๋Ÿฐ์Šค


 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•