Babel- compiler for writing the next generation of JavaScript

Babel- compiler for writing the next generation of JavaScript

2022-10-19 0 476
Resource Number 46090 Last Updated 2025-02-24
¥ 0USD Upgrade VIP
Download Now Matters needing attention
Can't download? Please contact customer service to submit a link error!
Value-added Service: Installation Guide Environment Configuration Secondary Development Template Modification Source Code Installation

The Babel featured in this issue is a tool chain primarily used to convert ECMAScript 2015+ code into backward compatible JavaScript versions in current and legacy browsers or environments.

Babel- compiler for writing the next generation of JavaScript插图

Babel is a tool to help you write code with the latest version of JavaScript. When features are not supported by your supported environment itself, Babel will help you compile those features to a supported version.

in:

// ES2020 nullish coalescing
function greet(input) {
return input ??  "Hello world";
}

out:

function greet(input) {
return input ! = null ?  input : "Hello world";
}

User Guide

There are many tools in the Babel toolchain that try to make it easy for you to use Babel, whether you’re an “end user” or building an integration of Babel itself.

The whole process of setting up includes:

1. Run these commands to install the package:

npm install --save-dev @babel/core  @babel/cli @babel/preset-env

2. Create a configuration file named babel.config.json (requires and above) in the root directory of the project with the following: v7.8.0

{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"edge": "17",
"firefox": "60",
"chrome": "67",
< span class = "HLJS - attr" > "safari" : < / span > < span class = "HLJS - string" > < / span > "11.1"
},
"useBuiltIns": "usage",
< span class = "HLJS attr" - > "corejs" < / span > : < span class = "HLJS - string" > < / span > "3.6.5"
}
]
]
}

or babel.config.js if you are using an older version of Babel

const presets = [
[
"@babel/preset-env",
{
targets: {
edge: "17",
firefox: "60",
chrome: "67",
safari: "11.1",
},
useBuiltIns: "usage",
corejs: "3.6.4",
},
].
];

module.exports = { presets }; 

and run this command to compile all the code in the src directory into lib:

./node_modules/.bin/babel src  --out-dir lib

Basic CLI usage

All the Babel modules you need are released as separate npm packages @babel (starting with version 7). This modular design allows for the use of a variety of tools, each designed for a specific use case. Here we will look at @babel/core and @babel/cli.

Core library

Babel’s core functionality is located in the @babel/core module. After installation:

npm install --save-dev  @babel/core

You can require to use it directly in your JavaScript program and use it like this:

const babel = require("@babel/core");

babel.transformSync("code", optionsObject); 

However, as an end user, you may want to install other tools that act as interfaces to your development process @babel/core and integrate well with your development process. Even so, you may still want to check out its documentation page for options, most of which can also be set up from other tools.

Command line tools

@babel/cli is a tool that allows you to use babel from the terminal. Here is the installation command and basic usage example:

npm install --save-dev @babel/core  @babel/cli

./node_modules/.bin/babel src --out-dir lib

This will parse all the JavaScript files in the src directory, apply any transformations we tell it, and output each file to the lib directory. Since we haven’t told it to apply any transformations, the output code will be the same as the input (without preserving the exact code style). We can specify the transformation we want by passing them as options.

Configuration Babel

Many other tools have similar configurations: ESLint (.eslintrc), Prettier (.prettierrc).

Allows all Babel API options. However, if this option requires JavaScript, you may need to use a JavaScript configuration file.

babel.config.json

Create a package.json file named babel.config.json in the root directory of the project (where).

{
"presets": [...] ,
"plugins": [...]
}
module.exports = function (api) {
api.cache(true);

const presets = [ ... ] ;
const plugins = [ ... ] ;

return {
presets,
plugins
};
}

.babelrc.json

Create a file in your project named.babelrc.json with the following contents.

{
  "presets": [...],
  "plugins": [...]
}

package.json

Alternatively, you can choose to specify your configuration internally using the following key.babelrc.json: package.jsonbabel

{
"name": "my-package",
< span class = "HLJS - attr" > "version" : < / span > < span class = "HLJS - string" > < / span > "1.0.0",
"babel": {
"presets": [ ... ] ,
"plugins": [ ... ] ,
}
}

JavaScript configuration file

You can also use JavaScript to write babel.config.json and files:.babelrc.json

const presets = [ ... ] ;
const plugins = [ ... ] ;

module.exports = { presets, plugins }; 

You can access any Node.js API, such as dynamic configuration based on process environment:

const presets = [ ... ] ;
const plugins = [ ... ] ;

if (process.env["ENV"] === "prod") {
plugins.push(...) ;
}

module.exports = { presets, plugins }; 

Babel- compiler for writing the next generation of JavaScript插图1

Frequently Asked Questions

Why do arguments remap in this arrow function?

The arrow function is not a synonym for the ordinary function . arguments internal this arrow functions refer to their external functions , e.g.

const user = {
firstName: "Sebastian",
lastName: "McKenzie",
getFullName: () =>  {
// whoops!  `this` doesn't actually reference `user` here
return this.firstName + "  " + this.lastName;
},
// use the method shorthand in objects
getFullName2() {
return this.firstName + "  " + this.lastName;
},
}; 

Why is this remapped to undefined?

Babel assumes that all input code is an ES2015 module. The ES2015 module is implicitly strict mode, so this means that the top-level This is not in the window browser, nor is it exported in the node.

Upgrade from Babel 5.x to Babel 6

The core of Babel 6 is the plugin. Which plugins you need depends entirely on your specific configuration, but just add the following profile to get all the same conversions as in Babel 5:

{
"presets":  ["env", "react",  "stage-2"]
}
npm install babel-preset-env  babel-preset-react babel-preset-stage-2 --save-dev

—END—

Open Source license: MIT license

资源下载此资源为免费资源立即下载
Telegram:@John_Software

Disclaimer: This article is published by a third party and represents the views of the author only and has nothing to do with this website. This site does not make any guarantee or commitment to the authenticity, completeness and timeliness of this article and all or part of its content, please readers for reference only, and please verify the relevant content. The publication or republication of articles by this website for the purpose of conveying more information does not mean that it endorses its views or confirms its description, nor does it mean that this website is responsible for its authenticity.

Ictcoder Free source code Babel- compiler for writing the next generation of JavaScript https://ictcoder.com/kyym/babel-compiler-for-writing-the-next-generation-of-javascript.html

Share free open-source source code

Q&A
  • 1, automatic: after taking the photo, click the (download) link to download; 2. Manual: After taking the photo, contact the seller to issue it or contact the official to find the developer to ship.
View details
  • 1, the default transaction cycle of the source code: manual delivery of goods for 1-3 days, and the user payment amount will enter the platform guarantee until the completion of the transaction or 3-7 days can be issued, in case of disputes indefinitely extend the collection amount until the dispute is resolved or refunded!
View details
  • 1. Heptalon will permanently archive the process of trading between the two parties and the snapshots of the traded goods to ensure that the transaction is true, effective and safe! 2, Seven PAWS can not guarantee such as "permanent package update", "permanent technical support" and other similar transactions after the merchant commitment, please identify the buyer; 3, in the source code at the same time there is a website demonstration and picture demonstration, and the site is inconsistent with the diagram, the default according to the diagram as the dispute evaluation basis (except for special statements or agreement); 4, in the absence of "no legitimate basis for refund", the commodity written "once sold, no support for refund" and other similar statements, shall be deemed invalid; 5, before the shooting, the transaction content agreed by the two parties on QQ can also be the basis for dispute judgment (agreement and description of the conflict, the agreement shall prevail); 6, because the chat record can be used as the basis for dispute judgment, so when the two sides contact, only communicate with the other party on the QQ and mobile phone number left on the systemhere, in case the other party does not recognize self-commitment. 7, although the probability of disputes is very small, but be sure to retain such important information as chat records, mobile phone messages, etc., in case of disputes, it is convenient for seven PAWS to intervene in rapid processing.
View details
  • 1. As a third-party intermediary platform, Qichou protects the security of the transaction and the rights and interests of both buyers and sellers according to the transaction contract (commodity description, content agreed before the transaction); 2, non-platform online trading projects, any consequences have nothing to do with mutual site; No matter the seller for any reason to require offline transactions, please contact the management report.
View details

Related Article

make a comment
No comments available at the moment
Official customer service team

To solve your worries - 24 hours online professional service