This example should demonstrate the concept of babel.
-
Create an empty folder and
cd
into it. -
Initialize NPM in the new folder:
npm init
-
Install babel and Env preset using NPM:
npm install babel-cli babel-preset-env --save-dev
-
Create the file
.babelrc
with the following content.
{
"presets": [
["env", {
"targets": {
"browsers": ["explorer >= 8"]
}
}]
]
}
-
Note the browserslist browser query
explorer >= 8
. Try the query interactively in browserl.ist. -
Add a
build
script topackage.json
:
...
"scripts": {
"build": "babel src.js --out-file dest.js"
},
...
-
Run babel with
npm run build
and compare the source filesrc.js
with the generated filedest.js
. Note the fundamental changes that babel had to make to your code to make it compatible with IE 8. -
Change the browser query in
.babelrc
tochrome >= 58
. -
Run babel again and compare the source file
src.js
with the generated filedest.js
. Note how similar the code is because of the recent Chrome version.