Babel
lastmod 2022-04-08created 2016-04-0830 edits
(For the Python i18n library, see Python-babel)
As you may know, Babel is a tool for transpiling ES6/ES7 code into ECMAScript5 code. Babel consists of many small modules. Babel acts as a kind of box that holds these modules, and it uses small modules (ex. presets) to compile code.
Categories
- babel.config.json
- Babel:Config
- @babel/core
- @babel/polyfill (Babel:Polyfill) - When the program first starts, a polyfill checks for functions the current browser does not support and attaches them to the prototype of each object.
- As of Babel 7.4.0, @babel/polyfill is deprecated.
Installation
webpack
Via config
{
module: {
rules: [
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
}
For more information see thebabel/babel-loaderrepo.
Create babel.config.json configuration file
You've configured Babel, but it doesn't actually do anything yet. Create ababel.config.jsonconfig in your project root and enable some presets.
To start, you can use the env preset, which enables transforms for ES2015+.
In order to enable the preset you have to define it in your babel.config.json file, like this:
Simple example
Add babel:
Edit .babelrc:
Add a Babel build command to package.json:
{
"name": "es6-project",
"version": "1.0.0",
"scripts": {
"build": "babel src/js -w -d dist/js"
},
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1"
}
}
Babel build options:
-
-w - Watches all files in the target folder for changes and transpiles them automatically. (Short form of the
--watchoption) -
-d - Specifies the folder where the transpiled output is saved. (Short form of the
--out-diroption)
Compatibility table
See also
Favorite site
- Babel web site
- Babel REPL
- [Recommended] Understanding Babel | by landvibe | Medium
- Basic Babel usage for ECMAScript 6
- 2015 年の JavaScript と babel の話(るびま)
Guide
- [Recommended] Create the latest JavaScript web frontend development environment with Webpack and Babel 1 (Webpack, JavaScript, Babel)
- (Tool) (Translation) Everything about Babel
- Using ES6 code with Babel
References
-
Create_the_latest_JavaScript_web_frontend_development_environment_with_Webpack_and_Babel.pdf ↩
- @babel/polyfill
- @babel/preset-env
- @babel/preset-flow
- @babel/preset-typescript
- Babel-jest
- Babel-loader
- Computer Language
- ESLint
- Flow
- JavaScript
- JavaScript:Object
- Jest
- Left-pad
- Npm:Module
- Programming
- Python-babel
- Scripting Language
- Ts-loader
- TypeScript
- TypeScript:Babel
- Vue CLI
- Vue.js
- Vue:ProjectTemplate:Vue2 Babel
- Vue:ProjectTemplate:Vue2 Babel ESLint
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass Jest
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass Jest Cypress
- Vue:ProjectTemplate:Vue2 Babel ESLint TypeScript Router Vuex Sass Jest Cypress PWA
- VueCLI:Plugins
- Web development
- Webpack