Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(en): merge website/main into babeljs.cn/cn-v7 @ 806e6890 #206

Open
wants to merge 14 commits into
base: cn-v7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _redirects
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ https://babel.netlify.com/* https://babeljs.io/:splat 301!
/docs/babel-plugin-proposal-class-properties /docs/babel-plugin-transform-class-properties
/docs/babel-plugin-proposal-private-methods /docs/babel-plugin-transform-private-methods
/docs/babel-plugin-proposal-numeric-separator /docs/babel-plugin-transform-numeric-separator
/docs/babel-plugin-proposal-dynamic-import /docs/babel-plugin-transform-dynamic-import
/docs/babel-plugin-proposal-logical-assignment-operators /docs/babel-plugin-transform-logical-assignment-operators
/docs/babel-plugin-proposal-nullish-coalescing-operator /docs/babel-plugin-transform-nullish-coalescing-operator
/docs/babel-plugin-proposal-optional-chaining /docs/babel-plugin-transform-optional-chaining
Expand All @@ -93,7 +94,7 @@ https://babel.netlify.com/* https://babeljs.io/:splat 301!

# Blog rewrites
/7.24.0 /blog/2024/02/28/7.24.0
/7.23.0 /blog/2023/09/25/7.22.0
/7.23.0 /blog/2023/09/25/7.23.0
/7.22.0 /blog/2023/05/26/7.22.0
/7.21.0 /blog/2023/02/20/7.21.0
/7.20.0 /blog/2022/10/27/7.20.0
Expand Down
6 changes: 5 additions & 1 deletion docs/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ It is based on [ESTree spec][] with the following deviations:
- [Program][] and [BlockStatement][] contain additional `directives` field with [Directive][] and [DirectiveLiteral][]
- [ClassMethod][], [ClassPrivateMethod][], [ObjectProperty][], and [ObjectMethod][] value property's properties in [FunctionExpression][] is coerced/brought into the main method node.
- [ChainExpression][] is replaced with [OptionalMemberExpression][] and [OptionalCallExpression][]
- [ImportExpression][] is replaced with a [CallExpression][] whose `callee` is an [Import] node.
- [ImportExpression][] is replaced with a [CallExpression][] whose `callee` is an [Import] node. This change will be reversed in Babel 8.
- [ExportAllDeclaration][] with `exported` field is replaced with an [ExportNamedDeclaration][] containing an [ExportNamespaceSpecifier][] node.

:::tip
There is now an `estree` plugin which reverts these deviations
Expand All @@ -134,6 +135,7 @@ AST for JSX code is based on [Facebook JSX AST][].
[propertydefinition]: https://github.com/estree/estree/blob/master/es2022.md#propertydefinition
[chainexpression]: https://github.com/estree/estree/blob/master/es2020.md#chainexpression
[importexpression]: https://github.com/estree/estree/blob/master/es2020.md#importexpression
[exportalldeclaration]: https://github.com/estree/estree/blob/master/es2020.md#exportalldeclaration
[privateidentifier]: https://github.com/estree/estree/blob/master/es2022.md#privateidentifier
[stringliteral]: https://github.com/babel/babel/tree/main/packages/babel-parser/ast/spec.md#stringliteral
[numericliteral]: https://github.com/babel/babel/tree/main/packages/babel-parser/ast/spec.md#numericliteral
Expand All @@ -157,6 +159,8 @@ AST for JSX code is based on [Facebook JSX AST][].
[optionalcallexpression]: https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md#optionalcallexpression
[callexpression]: https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md#callexpression
[import]: https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md#import
[exportnameddeclaration]: https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md#exportnameddeclaration
[exportnamespacespecifier]: https://github.com/babel/babel/blob/main/packages/babel-parser/ast/spec.md#exportnamespacespecifier
[facebook jsx ast]: https://github.com/facebook/jsx/blob/master/AST.md

### Semver
Expand Down
45 changes: 45 additions & 0 deletions docs/plugin-bugfix-firefox-class-in-computed-class-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
id: babel-plugin-bugfix-firefox-class-in-computed-class-key
title: "@babel/plugin-bugfix-firefox-class-in-computed-class-key"
sidebar_label: bugfix-firefox-class-in-computed-class-key
---

This bugfix plugin transforms classes inside computed keys of other classes to workaround a [SpiderMonkey bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1887677) with private class elements.

:::tip
This plugin is included in `@babel/preset-env`, and Babel will automatically enable this plugin for you when your `targets` are affected by the browser bug.
:::

:::warning
Terser versions older than 5.30.2 will undo the transform done by this plugin. Make sure to use at least version 5.30.2, or set the Terser's [`compress.inline`](https://terser.org/docs/options/#compress-options) option to `false`.
:::

## Installation

```shell npm2yarn
npm install --save-dev @babel/plugin-bugfix-firefox-class-in-computed-class-key
```

## Usage

### With a configuration file (Recommended)

```json title="babel.config.json"
{
"plugins": ["@babel/plugin-bugfix-firefox-class-in-computed-class-key"]
}
```

### Via CLI

```sh title="Shell"
babel --plugins @babel/plugin-bugfix-firefox-class-in-computed-class-key script.js
```

### Via Node API

```js title="JavaScript"
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-bugfix-firefox-class-in-computed-class-key"],
});
```
2 changes: 1 addition & 1 deletion docs/plugin-proposal-decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ Selects the decorators proposal to use:
`"2023-11"`, `"2023-05"`, `"2023-01"`, `"2022-03"`, `"2021-12"`, `"2018-09"` or `"legacy"`.

Selects the decorators proposal to use:
- `"2023-11"` is the proposal version after the updates that reached consensus in the November 2023 TC30 meetings, intergrating [this change](https://github.com/pzuraq/ecma262/pull/12)
- `"2023-11"` is the proposal version after the updates that reached consensus in the November 2023 TC39 meetings, intergrating [this change](https://github.com/pzuraq/ecma262/pull/12)
- `"2023-05"` is the proposal version after the updates that reached consensus in the March and May 2023 TC39 meetings, integrating [these changes](https://github.com/pzuraq/ecma262/compare/e86128e13b63a3c2efc3728f76c8332756752b02...c4465e44d514c6c1dba810487ec2721ccd6b08f9).
- `"2023-01"` is the proposal version after the updates that reached consensus in the January 2023 TC39 meeting, integrating [`pzuraq/ecma262#4`](https://github.com/pzuraq/ecma262/pull/4).
- `"2022-03"` is the proposal version that reached consensus for Stage 3 in the March 2022 TC39 meeting. You can read more about it at [`tc39/proposal-decorators@8ca65c046d`](https://github.com/tc39/proposal-decorators/tree/8ca65c046dd5e9aa3846a1fe5df343a6f7efd9f8).
Expand Down
10 changes: 6 additions & 4 deletions docs/plugin-proposal-optional-chaining-assign.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ npm install --save-dev @babel/plugin-proposal-optional-chaining-assign
```json title="babel.config.json"
{
"plugins": [
"@babel/plugin-proposal-optional-chaining-assign",
{
"version": "2023-07"
}
[
"@babel/plugin-proposal-optional-chaining-assign",
{
"version": "2023-07"
}
]
]
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
id: babel-plugin-proposal-dynamic-import
title: "@babel/plugin-proposal-dynamic-import"
id: babel-plugin-transform-dynamic-import
title: "@babel/plugin-transform-dynamic-import"
sidebar_label: dynamic-import
---

Expand Down Expand Up @@ -74,7 +74,7 @@ will be transformed to
## Installation

```shell npm2yarn
npm install --save-dev @babel/plugin-proposal-dynamic-import
npm install --save-dev @babel/plugin-transform-dynamic-import
```

## Usage
Expand All @@ -84,7 +84,7 @@ npm install --save-dev @babel/plugin-proposal-dynamic-import
```json title="babel.config.json"
{
"plugins": [
"@babel/plugin-proposal-dynamic-import",
"@babel/plugin-transform-dynamic-import",
"@babel/plugin-transform-modules-commonjs"
]
}
Expand All @@ -93,15 +93,15 @@ npm install --save-dev @babel/plugin-proposal-dynamic-import
### Via CLI

```sh title="Shell"
babel --plugins=@babel/plugin-proposal-dynamic-import,@babel/plugin-transform-modules-amd script.js
babel --plugins=@babel/plugin-transform-dynamic-import,@babel/plugin-transform-modules-amd script.js
```

### Via Node API

```js title="JavaScript"
require("@babel/core").transformSync("code", {
plugins: [
"@babel/plugin-proposal-dynamic-import",
"@babel/plugin-transform-dynamic-import",
"@babel/plugin-transform-modules-systemjs"
],
});
Expand Down
131 changes: 89 additions & 42 deletions docs/plugin-transform-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ title: "@babel/plugin-transform-runtime"

A plugin that enables the re-use of Babel's injected helper code to save on codesize.

::::babel7

:::note
Instance methods such as `"foobar".includes("foo")` will only work with `core-js@3`. If you need to polyfill them, you can directly import `"core-js"` or use `@babel/preset-env`'s `useBuiltIns` option.
:::

::::

## Installation

Install it as development dependency.
Expand All @@ -35,10 +39,14 @@ Babel uses very small helpers for common functions such as `_extend`. By default

This is where the `@babel/plugin-transform-runtime` plugin comes in: all of the helpers will reference the module `@babel/runtime` to avoid duplication across your compiled output. The runtime will be compiled into your build.

::::babel7

Another purpose of this transformer is to create a sandboxed environment for your code. If you directly import [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](polyfill.md) and the built-ins it provides such as `Promise`, `Set` and `Map`, those will pollute the global scope. While this might be ok for an app or a command line tool, it becomes a problem if your code is a library which you intend to publish for others to use or if you can't exactly control the environment in which your code will run.

The transformer will alias these built-ins to `core-js` so you can use them seamlessly without having to require the polyfill.

::::

See the [technical details](#technical-details) section for more information on how this works and the types of transformations that occur.

## Usage
Expand Down Expand Up @@ -90,6 +98,21 @@ require("@babel/core").transformSync("code", {

## Options


### `absoluteRuntime`

`boolean` or `string`, defaults to `false`.

This allows users to run `transform-runtime` broadly across a whole project. By default, `transform-runtime` imports from `@babel/runtime/foo` directly, but that only works if `@babel/runtime` is in the `node_modules` of the file that is being compiled. This can be problematic for nested `node_modules`, npm-linked modules, or CLIs that reside outside the user's project, among other cases. To avoid worrying about how the runtime module's location is resolved, this allows users to resolve the runtime once up front, and then insert absolute paths to the runtime into the output code.

Using absolute paths is not desirable if files are compiled for use at a later time, but in contexts where a file is compiled and then immediately consumed, they can be quite helpful.

:::tip
You can read more about configuring plugin options [here](https://babeljs.io/docs/en/plugins#plugin-options)
:::

::::babel7

### `corejs`

`false`, `2`, `3` or `{ version: 2 | 3, proposals: boolean }`, defaults to `false`.
Expand Down Expand Up @@ -118,6 +141,12 @@ This option requires changing the dependency used to provide the necessary runti
| `2` | `npm install --save @babel/runtime-corejs2` |
| `3` | `npm install --save @babel/runtime-corejs3` |

:::caution

The `corejs` option will be removed in Babel 8. To inject polyfills, you can use [`babel-plugin-polyfill-corejs3`](https://github.com/babel/babel-polyfills/blob/main/packages/babel-plugin-polyfill-corejs3/README.md) or [`babel-plugin-polyfill-corejs2`](https://github.com/babel/babel-polyfills/blob/main/packages/babel-plugin-polyfill-corejs2/README.md) directly.

:::

### `helpers`

`boolean`, defaults to `true`.
Expand All @@ -126,6 +155,14 @@ Toggles whether or not inlined Babel helpers (`classCallCheck`, `extends`, etc.)

For more information, see [Helper aliasing](#helper-aliasing).

:::caution

The `helpers` option will be removed in Babel 8, as this plugin will only be used to inject helpers (including `regeneratorRuntime`, which will be handled as any other Babel helper).

:::

::::

### `moduleName`

<details>
Expand All @@ -147,42 +184,22 @@ This option controls which package of helpers `@babel/plugin-transform-runtime`

Note that specifying the [`corejs`](#corejs) option will internally enable the corresponding `babel-plugin-polyfill-corejs*` plugin, thus it has an effect on the final module name.

### `polyfill`

:::danger
This option was removed in v7.
:::
::::babel7

### `regenerator`

`boolean`, defaults to `true`.

Toggles whether or not generator functions are transformed to use a regenerator runtime that does not pollute the global scope.
In older Babel version, this option used to toggles whether or not generator functions were transformed to use a regenerator runtime that does not pollute the global scope.

For more information, see [Regenerator aliasing](#regenerator-aliasing).

### `useBuiltIns`

:::danger
This option was removed in v7.
:::caution
The `regenerator` option will be removed in Babel 8, as it will not be necessary anymore.
:::

### `useESModules`

::::babel8

:::danger
This option was removed in v8.
:::

::::

::::babel7

:::caution
This option has been deprecated and will be removed in Babel 8: starting from version `7.13.0`, `@babel/runtime`'s `package.json` uses `"exports"` option to automatically choose between CJS and ESM helpers.
:::

`boolean`, defaults to `false`.

<details>
Expand Down Expand Up @@ -219,46 +236,59 @@ export default function(instance, Constructor) {
}
```

:::caution
The `useESModules` option has been deprecated and will be removed in Babel 8: starting from version `7.13.0`, `@babel/runtime`'s `package.json` uses `"exports"` option to automatically choose between CJS and ESM helpers.
:::

::::

### `absoluteRuntime`
### `version`

`boolean` or `string`, defaults to `false`.
::::babel7

This allows users to run `transform-runtime` broadly across a whole project. By default, `transform-runtime` imports from `@babel/runtime/foo` directly, but that only works if `@babel/runtime` is in the `node_modules` of the file that is being compiled. This can be problematic for nested `node_modules`, npm-linked modules, or CLIs that reside outside the user's project, among other cases. To avoid worrying about how the runtime module's location is resolved, this allows users to resolve the runtime once up front, and then insert absolute paths to the runtime into the output code.
By default transform-runtime assumes that `@babel/[email protected]` is installed. If you have later versions of
`@babel/runtime` (or their corejs counterparts e.g. `@babel/runtime-corejs3`) installed or listed as a dependency, transform-runtime can use more advanced features.

Using absolute paths is not desirable if files are compiled for use at a later time, but in contexts where a file is compiled and then immediately consumed, they can be quite helpful.
For example if you depend on `@babel/runtime@^7.24.0` you can transpile your code with

:::tip
You can read more about configuring plugin options [here](https://babeljs.io/docs/en/plugins#plugin-options)
:::
```json title="babel.config.json"
{
"plugins": [
["@babel/plugin-transform-runtime", {
"version": "^7.24.0"
}]
]
}
```

### `version`
::::

By default transform-runtime assumes that `@babel/[email protected]` is installed. If you have later versions of
::::babel8

By default transform-runtime assumes that `@babel/[email protected]` is installed. If you have later versions of
`@babel/runtime` (or their corejs counterparts e.g. `@babel/runtime-corejs3`) installed or listed as a dependency, transform-runtime can use more advanced features.

For example if you depend on `@babel/[email protected]` you can transpile your code with

For example if you depend on `@babel/runtime@^8.1.0` you can transpile your code with

```json title="babel.config.json"
{
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"absoluteRuntime": false,
"corejs": 2,
"version": "^7.7.4"
}
]
["@babel/plugin-transform-runtime", {
"version": "^8.1.0"
}]
]
}
```

::::

which results in a smaller bundle size.

## Technical details

::::babel7

The `transform-runtime` transformer plugin does three things:

- Automatically requires `@babel/runtime/regenerator` when you use generators/async functions (toggleable with the `regenerator` option).
Expand Down Expand Up @@ -381,6 +411,8 @@ without worrying about where they come from.

### Helper aliasing

::::

Usually Babel will place helpers at the top of your file to do common tasks to avoid
duplicating the code around in the current file. Sometimes these helpers can get a
little bulky and add unnecessary duplication across files. The `runtime`
Expand Down Expand Up @@ -425,3 +457,18 @@ var Person = function Person() {
(0, _classCallCheck3.default)(this, Person);
};
```

## Removed options

:::babel8

The following options where removed in Babel 8.0.0:
- `corejs`
- `helpers`
- `regenerator`

:::

The following options where removed in Babel 7.0.0:
- `useBuiltIns`
- `polyfill`
Loading
Loading