포스코DX X 비트교육센터 6기 - ReactJS


Babel이란?

image

es6 -> es5 로 변환시켜줌.

예시 - src/index.js


// 블록 스코프(es6)
const user = [{
  no: 1,
  name: "마이콜",
  email: "michol@gmail.com"
}, {
  no: 2,
  name: "둘리",
  email: "dooly@gmail.com"
}];

// 객체분해(es6)
function print({
  no,
  name,
  email
}) {
  // 템플릿 문자열(es6)
  console.log(`${no} : ${name} : ${email}`);
}

// for ~ of(es6)

for (let user of users) {
  console.log(user);
}

  • Babel Plugin: 변환 규칙

설치

` $ npm i -D @babel/core @babel/cli`

플러그인 적용 (예제 : ex03)

  • 블록 스코프 변수 플러그인(@babel/plugin-transform-block-scoping)
          $ npm i -D @babel/plugin-transform-block-scoping
          $ npx babel src/index.js -o dist/index.js --plugins @babel/plugin-transform-block-scoping

dist/index.js -> 블록스코프의 변화를 느낄 수 있다.

// 블록 스코프(es6)
var user = [{
  no: 1,
  name: "마이콜",
  email: "michol@gmail.com"
}, {
  no: 2,
  name: "둘리",
  email: "dooly@gmail.com"
}];

// 객체분해(es6)
function print({
  no,
  name,
  email
}) {
  // 템플릿 문자열(es6)
  console.log(`${no} : ${name} : ${email}`);
}

// for ~ of(es6)

for (var _user of users) {
  console.log(_user);
}

  • 객체분해-파라미터 플러그인(@babel/plugin-transform-parameters)
           $ npm i -D @babel/plugin-transform-parameters
            $ npx babel src/index.js -o dist/index2.js --plugins @babel/plugin-transform-block-scoping --plugins @babel/plugin-transform-parameters

dist/index2.js -> 파라미터의 변화를 느낄 수 있다.

// 블록 스코프(es6)
var user = [{
  no: 1,
  name: "마이콜",
  email: "michol@gmail.com"
}, {
  no: 2,
  name: "둘리",
  email: "dooly@gmail.com"
}];

// 객체분해(es6)
function print(_ref) {
  var {
    no,
    name,
    email
  } = _ref;
  // 템플릿 문자열(es6)
  console.log(`${no} : ${name} : ${email}`);
}

// for ~ of(es6)

for (var _user of users) {
  console.log(_user);
}

  • for ~ of 플러그인(@babel/plugin-transform-for-of)
           $ npm i -D @babel/plugin-transform-for-of
            $ npx babel src/index.js -o dist/index3.js --plugins @babel/plugin-transform-block-scoping --plugins @babel/plugin-transform-parameters --plugins @babel/plugin-transform-for-of

dist/index2.js -> for ~ of문의 변화를 느낄 수 있다.

function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function () {}; return { s: F, n: function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function (e) { throw e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function () { it = it.call(o); }, n: function () { var step = it.next(); normalCompletion = step.done; return step; }, e: function (e) { didErr = true; err = e; }, f: function () { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
// 블록 스코프(es6)
var user = [{
  no: 1,
  name: "마이콜",
  email: "michol@gmail.com"
}, {
  no: 2,
  name: "둘리",
  email: "dooly@gmail.com"
}];

// 객체분해(es6)
function print(_ref) {
  var {
    no,
    name,
    email
  } = _ref;
  // 템플릿 문자열(es6)
  console.log(`${no} : ${name} : ${email}`);
}

// for ~ of(es6)
var _iterator = _createForOfIteratorHelper(users),
  _step;
try {
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
    var _user = _step.value;
    console.log(_user);
  }
} catch (err) {
  _iterator.e(err);
} finally {
  _iterator.f();
}


컴포넌트 종류

image