Babel Basic - ChoDragon9/posts GitHub Wiki

Babel

  • ๋‹ค์Œ ์„ธ๋Œ€์˜ ECMAScript(ECMA 262)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  • ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋™์ž‘ํ•˜๋Š” ๋ฒ„์ „์œผ๋กœ ์ปดํŒŒ์ผ ๊ฒฐ๊ณผ ์ฝ”๋“œ๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

ES2015 and beyond ์„ค์น˜ ๋ฐฉ๋ฒ•

npm i babel-preset-dev -D

babel-preset-env๋Š” es2015, es2016, es2017 ๋ฒ„์ „์„ ๋ชจ๋‘ ์ง€์›ํ•œ๋‹ค.

์˜ต์…˜์„ ์„ค์ •ํ•˜์ง€ ์•Š๊ณ  env๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ babel-preset-latest์™€ ๋™์ž‘์ด ๋™์ผํ•˜๋‹ค.

.babelrc์— ํ™˜๊ฒฝ์„ ์„ค์ •ํ•˜๊ฒŒ ๋˜๋ฉด ํ•ด๋‹นํ•˜๋Š” ๊ธฐ๋Šฅ์„ ์ปดํŒŒ์ผ ํ•ด์ค€๋‹ค.

{
  "presets": [
    ["env", {
      "targets": {
        "browsers": ["last 2 versions", "safari >= 7"]
      }
    }]
  ]
}

์œ„ ์„ค์ •์˜ ์˜๋ฏธ๋Š” ๊ฐ ๋ธŒ๋ผ์šฐ์ €์˜ ์ตœ์‹  2๊ฐœ์˜ ๋ฒ„์ „๊ณผ ์‚ฌํŒŒ๋ฆฌ ๋ฒ„์ „์ด 7์ด๊ฑฐ๋‚˜ ํฐ ๋ฒ„์ „์—์„œ ๋™์ž‘ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ export ํ•ด๋‹ฌ๋ผ๋Š” ์˜๋ฏธ์ด๋‹ค.

babel์€ browserslist ํŒŒ์„œ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ธŒ๋ผ์šฐ์ € ์ง€์› ๋ฒ”์œ„๋Š” browserslist๋ฅผ ์ฐธ๊ณ ํ•ด์„œ ์ž‘์„ฑํ•œ๋‹ค.

์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ์ •๋ฆฌํ•˜๋ฉด

  • "last 2 versions" : ๊ฐ ๋ธŒ๋ผ์šฐ์ €์˜ ์ตœ์‹  ๋‘ ๋ฒ„์ „
  • " > 5%" : ์ „์„ธ๊ณ„ ์‚ฌ์šฉ๋Ÿ‰ ํ†ต๊ณ„๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉ, 5% ์ดˆ๊ณผํ•˜๋Š” ๋ฒ„์ „ ์‚ฌ์šฉ, >= <= < ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • "not ie <= 8" : IE8 ์ดํ•˜ ๋ฒ„์ „ ์ œ์™ธ

Babel Polyfill

babel์€ ๋ฌธ๋ฒ•์ ์ธ ๋ถ€๋ถ„(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๊ฐ™์€) ์ปดํŒŒ์ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ƒˆ๋กœ์šด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” babel-polyfill์„ ์„ค์น˜ํ•ด์•ผ ํ•œ๋‹ค. babel ์‚ฌ์ดํŠธ์—์„œ๋Š” Promise, WeakMap, Array.from, Object.assign, Array.prototype.includes ์™€ ๊ฐ™์€ built-in ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์„ค์น˜ํ•˜๋ผ๊ณ  ๊ฐ€์ด๋“œ ํ•˜๊ณ  ์žˆ๋‹ค.

npm i babel-polyfill -D