java-script:babel
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| java-script:babel [2023/08/02 23:54] – odefta | java-script:babel [2025/01/02 18:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 34: | Line 34: | ||
| ==== Create .babelrc ==== | ==== Create .babelrc ==== | ||
| - | Inside your project in the main directory create a file called .babelrc | + | Inside your project in the main directory create a file called | 
| This will indicate babel which presets to use to transpile the ES6 code in ES5 code: | This will indicate babel which presets to use to transpile the ES6 code in ES5 code: | ||
| Line 46: | Line 46: | ||
| This command will transpile and run the code on the fly: | This command will transpile and run the code on the fly: | ||
| - | (5 5 are program arguments) | + | |
| + | < | ||
| + | npx babel-node ./ | ||
| + | </ | ||
| + | |||
| + | If the program | ||
| < | < | ||
| npx babel-node ./ | 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.1691020465.txt.gz · Last modified:  (external edit)
                
                