Hakalon / babel-example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

babel-example

Update of babel 7

Because of the release of babel 7, there are two main changes you should know.

  1. No more babel-core, babel-cli and babel-preset-env.
    Now babel 7 using scope to seperate different module of babel like @babel/core, @babel/cli, @babel/preset-env.
    That is, you should use yarn or npm to download @babel/core instead of babel-core.

  2. There is no preset-es20XX like es2015, es2016 and other stages of preset.
    The only thing you need is @babel/preset-env which can offer what ever you need.

The more information about babel 7 is here, or here in chinese.

Some keywords you must understand

1. .babelrc : It's a JSON file recording your setting about babel, usually you will see some keyword like "preset", "plugins".
You need to add a file named .babelrc to your project directory, and the content is like below.

{
    "presets": [
        "@babel/preset-env"
    ]
}

The only thing you need to remember is that using "@babel/preset-env" on presets.

2. @babel/cli & @babel/node : When you are developing, you should install @babel/cli on your computer by yarn or npm Globally. It will provide you command like "babel" and "babel-node" to compile files.

3. @babel/core & @babel/preset-env : When you want to compile any file by babel, you should install @babel/core and @babel/preset-env first. They provide everything you need to compile files.
Be noticed that you should install it locally. (Actually, you can't even install them globally.)

4. @babel/polyfill : You should add it to your project dependency for your client. They will need it to use command "node" to run files that you compiled by babel.

More detail in chinese is here.

Installation

The first you should do is put .babelrc file to your project directory, then you can continue following the steps below.

For Developer :

Just like I mentioned, you should install @babel/cli & @babel/node on your computer by yarn or npm globally.

yarn global add @babel/cli @babel/node
// or
npm install @babel/cli @babel/node -g

Also, you will need @babel/core & @babel/preset-env for compiling.

yarn add @babel/core @babel/preset-env -D
// or
npm install @babel/core @babel/preset-env -D

For Client :

You should add @babel/polyfill as dependency for your client, they will need it to run file that be compiled by babel.

yarn add babel-polyfill
// or
npm install babel-polyfill --save

Usage

@babel/cli & @babel/node

If you want compile file by babel, you should use something like:

babel src/index.js -d build

After that, you also need to add one line to the file you just compiled.

require('@babel/polyfill')

Then your client can use node to run the file.

By the way, you can directly run js file with babel too.

babel-node src/index.js

Production

If you follow the step of Installation correctly, then after cloning your project, your client only need to use these command

yarn install --prod
// or
npm install -P

node build/index.js

About


Languages

Language:JavaScript 100.0%