module-export-import

๐Ÿ”—๋ชจ๋“ˆํ•™์Šต
๐Ÿ”—MDN๋ชจ๋“ˆ๋ฌธ์„œ

Info: ๐Ÿ“ข ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œย ๋ชจ๋“ˆ ์ด๋ž€ย ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌ์„ฑํ•˜๋Š” ๊ตฌ์„ฑ์š”์†Œ์˜ ์ผ๋ถ€
๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ๋‚˜, ๋‚ด๊ฐ€ ์ž˜๊ฒŒ ์ชผ๊ฐœ ๋†“์€ ์ฝ”๋“œ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์“ด๋‹ค


  • export,default

โœ๏ธmodule.html

<!DOCTYPE html>
<html lang="ko">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <h1>modules</h1>
  <button>say</button>
  <script>
    document.querySelector('button').addEventListener('click', () => { say("๋ง๊ณ ") })
    function say(user) {
      alert(`'hello',${user}`)
    }
  </script>
</body>

</html>

์ผ๋ฐ˜ html ๋ฌธ์„œ์ž‘์„ฑํ›„ ๋ชจ๋“ˆํ™” ํ•œ๋‹ค


โœ๏ธsay.js

/*export ๋ฅผ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜ ์•ž์— ๋ถ™์ด๋ฉด ์™ธ๋ถ€ ๋ชจ๋“ˆ์—์„œ ํ•ด๋‹น ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜์— ์ ‘๊ทผ๊ฐ€๋Šฅ*/
export function say(user){
  alert(`Hello,${user}!`)
}

โœ๏ธmodule.html

...
<body>

  <script type="module">
    /* import ๋ฅผ ์‚ฌ์šฉํ•ด say.js์˜ ํ•จ์ˆ˜ say๋ฅผ ์‚ฌ์šฉ */

    import {say} from './say.js';  
	   document.querySelector('button').addEventListener('click', () => { say("๋ง๊ณ ") })

	</script>
</body>
</html>

์‹คํ–‰ํ™”๋ฉด

Info: ๐Ÿ“ข ์ฃผ์˜์‚ฌํ•ญ : ๋ผ์ด๋ธŒ์„œ๋ฒ„์—์„œ ํ• ๊ฒƒ

  • ๋ชจ๋“ˆ์€ ๋กœ์ปฌ ํŒŒ์ผ์—์„œ ๋™์ž‘ํ•˜์ง€ ์•Š๊ณ , HTTP ๋˜๋Š” HTTPS ํ”„๋กœํ† ์ฝœ์„ ํ†ตํ•ด์„œ๋งŒ ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.
  • ๋ชจ๋“ˆ์€ ์ž์‹ ๋งŒ์˜ ์Šค์ฝ”ํ”„๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋ชจ๋“ˆ ๋‚ด๋ถ€์—์„œ ์ •์˜ํ•œ ๋ณ€์ˆ˜๋‚˜ ํ•จ์ˆ˜๋Š” ๋‹ค๋ฅธ ์Šคํฌ๋ฆฝํŠธ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ๋„ย <script type="module">
    ์„ ์‚ฌ์šฉํ•ด ๋ชจ๋“ˆ์„ ๋งŒ๋“ค๋ฉด ๋…๋ฆฝ์ ์ธ ์Šค์ฝ”ํ”„๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.
  • ๋™์ผํ•œ ๋ชจ๋“ˆ์ด ์—ฌ๋Ÿฌ ๊ณณ์—์„œ ์‚ฌ์šฉ๋˜๋”๋ผ๋„ ๋ชจ๋“ˆ์€ ์ตœ์ดˆ ํ˜ธ์ถœ ์‹œ ๋‹จ ํ•œ ๋ฒˆ๋งŒ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ์‹คํ–‰ ํ›„ ๊ฒฐ๊ณผ๋Š” ์ด ๋ชจ๋“ˆ์„ ๊ฐ€์ ธ๊ฐ€๋ ค๋Š” ๋ชจ๋“  ๋ชจ๋“ˆ์— ๋‚ด๋ณด๋‚ด ์ง‘๋‹ˆ๋‹ค.

[์—ฌ๋Ÿฌ ๊ฐ์ฒด export & import ํ•˜๊ธฐ]

โœ๏ธmain.js

function funcA() {
	console.log("functionAA");
}
function funcB(test) {
	console.log(test, "functionbb");
}
const varC = "hello";
export { funcA, funcB, varC };

import ํ•  ๋•Œ๋Š” {} ๋ฅผ ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
โœ๏ธmodule.html

<script type="module">
  import {funcA,funcB,varC} from './main.js';  
  funcA()
  funcB(varC)
</script>

์‹คํ–‰ํ™”๋ฉด


default export์™€ named export ์ฐจ์ด์ 

1. Default Export

  • default ๋กœ ์„ ์–ธ๋œ ๋ชจ๋“ˆ์€ ํ•˜๋‚˜์˜ ํŒŒ์ผ์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜ ๋˜๋Š” ํด๋ž˜์Šค ๋“ฑ๋งŒ export ํ•  ์ˆ˜ ์žˆ๋‹ค.
//app.js
const app= () => {
     ...
}export default app
  • import ํ•  ๋•Œ๋Š” ์•„๋ฌด ์ด๋ฆ„์œผ๋กœ๋‚˜ import ๊ฐ€๋Šฅํ•˜๋‹ค.
//app-child1.js
import ypp from '../app'

๋˜๋Š”

//app-child2.js
import bpp from '../app'

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ๊ฐ€๋Šฅ

2. named export

  • ํ•œ ํŒŒ์ผ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜/ํด๋ž˜์Šค ๋“ฑ๋“ฑ์„ export ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
export class MyFirstClass { /* ... */ }
export class MySecondClass { /* ... */ }
  • ๋‹ค๋งŒ, import ์‹œ {} ์•ˆ์—๋‹ค๊ฐ€ export ๋œ ์ด๋ฆ„๊ณผ ๋™์ผํ•˜๊ฒŒ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
import { MyFirstClass, MySecondClass } from './MyClass'
  • ๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ import ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์•„๋ž˜์ฒ˜๋Ÿผย as๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
import { MyFirstClass as BlahBlahClass, MySecondClass } from './MyClass'
  • asย ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•œ ํŒŒ์ผ์— ์žˆ๋Š” ํด๋ž˜์Šค/๋ณ€์ˆ˜๋“ค์„ ํ•œ ๋ฒˆ์— import ํ•  ์ˆ˜ ์žˆ๋‹ค.
import * as Hello from './MyClass'
// ์ด๋ ‡๊ฒŒ import ํ•˜๋ฉด Hello.MyFirstClass ์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•จ

๋Œ“๊ธ€ ๋‚จ๊ธฐ๊ธฐ