Welcome to the World of babel-cli
Babel is a powerful tool that helps you write code in the latest version of JavaScript while ensuring compatibility with older environments. The babel-cli package allows you to use Babel from the command line to transform your JavaScript code. Below are some important APIs and examples to get you started with babel-cli.
Installation
npm install -g @babel/cli @babel/core
Transform Files
Use babel-cli to transform files:
babel src --out-dir lib
Watch Mode
Automatically recompile files on change:
babel src --out-dir lib --watch
Source Maps
Generate source maps while transforming files:
babel src --out-dir lib --source-maps
Ignore Files
Ignore specific files or directories:
babel src --out-dir lib --ignore \"src/**/*.spec.js\"
Extensions
Compile files with specific extensions:
babel src --out-dir lib --extensions \".ts,.tsx\"
Config File
Specify a custom babel configuration file:
babel src --out-dir lib --config-file ./custom.babelrc
Copy Files
Copy non-compilable files to the output directory:
babel src --out-dir lib --copy-files
Verbose
Enable detailed output:
babel src --out-dir lib --verbose
App Example
Below is an example of using babel-cli in an application:
. ├── package.json ├── src │ ├── index.js │ ├── util.js ├── .babelrc └── lib (output folder) // .babelrc { \"presets\": [\"@babel/preset-env\"] } // src/index.js import { helper } from './util'; const message = 'Hello, Babel!'; console.log(helper(message)); // src/util.js export function helper(text) { return text.toUpperCase(); } // Command to compile ES6 to ES5 babel src --out-dir lib