java-script:babel
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| java-script:babel [2023/08/02 23:39] – created odefta | java-script:babel [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Babel ====== | ====== Babel ====== | ||
| + | |||
| + | ===== Use cases ===== | ||
| Babel was mainly used to transpile (compile) ES6 code into ES5 or older version of ES (ECMAScript). \\ | Babel was mainly used to transpile (compile) ES6 code into ES5 or older version of ES (ECMAScript). \\ | ||
| Line 11: | Line 13: | ||
| - **Minification**: | - **Minification**: | ||
| - **Code Transformations**: | - **Code Transformations**: | ||
| + | |||
| + | ===== Install babel packages for a project ===== | ||
| + | |||
| + | This will create a **node-modules** folder containing a lot of modules: | ||
| + | |||
| + | < | ||
| + | npm install @babel/core @babel/ | ||
| + | </ | ||
| + | |||
| + | Output: | ||
| + | < | ||
| + | added 248 packages, and audited 249 packages in 40s | ||
| + | |||
| + | 58 packages are looking for funding | ||
| + | run `npm fund` for details | ||
| + | |||
| + | found 0 vulnerabilities | ||
| + | </ | ||
| + | |||
| + | ==== Create .babelrc ==== | ||
| + | |||
| + | Inside your project in the main directory create a file called **.babelrc**. \\ | ||
| + | This will indicate babel which presets to use to transpile the ES6 code in ES5 code: | ||
| + | |||
| + | <code javascript .babelrc> | ||
| + | { | ||
| + | " | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Run our transpiled code (from ES6 to ES5) ===== | ||
| + | |||
| + | This command will transpile and run the code on the fly: | ||
| + | |||
| + | < | ||
| + | npx babel-node ./ | ||
| + | </ | ||
| + | |||
| + | If the program has some arguments: | ||
| + | |||
| + | < | ||
| + | npx babel-node ./ | ||
| + | </ | ||
| + | |||
| + | ===== View transpiled code ===== | ||
| + | |||
| + | If we want to examine the transpiled code, we'll need to install the babel command line: | ||
| + | |||
| + | < | ||
| + | npm install @babel/cli | ||
| + | </ | ||
| + | |||
| + | Output: | ||
| + | < | ||
| + | added 29 packages, and audited 278 packages in 5s | ||
| + | |||
| + | 61 packages are looking for funding | ||
| + | run `npm fund` for details | ||
| + | |||
| + | found 0 vulnerabilities | ||
| + | </ | ||
| + | |||
| + | Next, add a script entry in the package.json file: | ||
| + | |||
| + | <code javascript> | ||
| + | { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Next, we can run: | ||
| + | < | ||
| + | npm run build | ||
| + | </ | ||
| + | |||
| + | Transpiled code are now in the dist folder: | ||
| + | < | ||
| + | |||
| + | > build | ||
| + | > babel src -d dist | ||
| + | |||
| + | Successfully compiled 2 files with Babel (423ms). | ||
| + | </ | ||
java-script/babel.1691019586.txt.gz · Last modified: (external edit)
