How to transpile files ES6 to ES5 with Babel?

3

I'm trying to follow a series of examples to learn ES6, but I can not get it to transpile correctly.

In my case I use the following line to do it

babel --watch index.js --out-file index_ES5.js

My file .eslintrc has the following configuration

{
  "parser": "babel-eslint",
  "plugins": ["import"],
  "extends": "airbnb",
  "env": {
    "node": true,
    "es6": true,
    "browser": true
  },
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": false
    }
  },
  "rules": {
    // evita que marque error al usar console
    "no-console": 0,
    // permite usar variables con _
    "no-underscore-dangle": 0,
    // máximo de línea: 99
    "max-len": [1, 99, 2],
    // obliga a usar punto y coma
    "semi": [1, "always"],
    // obliga a usar const sobre let
    "prefer-const": 2,
    // obliga a definir llaves en if/else
    "curly": [2, "all"],
    // prefiere === a ==
    "eqeqeq": [2, "allow-null"],
    "no-shadow": 1,
    // no exige que las funciones como callbacks
    // tengan un nombre
    "func-names": 0,
    // prefiere apóstrofe a comillas
    // y también para template literal
    "quotes": [
      2,
      "single",
      {
        "allowTemplateLiterals": true
      }
    ],
    // previene múltiples líneas en blanco
    "no-multiple-empty-lines": [ 2, {
      "max": 1,
      "maxEOF": 0,
      "maxBOF": 0
    }],
    // no activa el warning en parámetros rest
    "no-unused-vars": ["warn", {
      "ignoreRestSiblings": true
    }],
    "linebreak-style": "off"
  },
  "settings": {
    "node": {
      "extensions": [
        ".js"
      ],
      "moduleDirectory": [
        "node_modules",
        "./"
      ]
    },
    "import/resolver": {
      "node": {
        "extensions": [
          ".js"
        ],
        "moduleDirectory": [
          "node_modules",
          "./"
        ]
      }
    }
  }
} 
    
asked by Pedro Miguel Pimienta Morales 26.08.2017 в 20:48
source

1 answer

2

To compile ES6 to ES5 you need to install a preset , on time:

  • Install babel-preset-es2015 , for example, via npm :

    $ npm install babel-preset-es2015 –save-dev
    
  • Then you can use it:

    • Via CLI

      $ babel --watch index.js --presets es2015 --out-file index_ES5.js
      
    • Via .babelrc :
      You must create a file named .babelrc and that contains:

      {
        "presets": ["es2015"]
      }
      

      The latter being the most recommended, since it would avoid having to indicate the presets each time you execute babel

PD : The .eslintrc file is for the ESLint ( corrector and highlighter of syntax errors ).

    
answered by 26.08.2017 / 21:41
source