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

7.6.0 #2099

Merged
merged 3 commits into from
Sep 13, 2019
Merged

7.6.0 #2099

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.0.0/config-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Babel has two parallel config file formats, which can be used together, or indep

## Project-wide configuration

New in Babel 7.x, Babel has as concept of a ["root"](options.md#root) directory, which defaults
New in Babel 7.x, Babel has a concept of a ["root"](options.md#root) directory, which defaults
to the current working directory. For project-wide configuration, Babel will automatically search
for a `"babel.config.js"` in this root directory. Alternatively, users can use an explicit
["configFile"](options.md#configfile) value to override the default config file search behavior.
Expand Down
8 changes: 4 additions & 4 deletions website/versioned_docs/version-7.0.0/core.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Given an [AST](https://astexplorer.net/), transform it.

```js
const sourceCode = "if (true) return;";
const parsedAst = babel.parse(sourceCode, { allowReturnOutsideFunction: true });
const parsedAst = babel.parse(sourceCode, { parserOpts: { allowReturnOutsideFunction: true } });
babel.transformFromAst(parsedAst, sourceCode, options, function(err, result) {
const { code, map, ast } = result;
});
Expand All @@ -167,7 +167,7 @@ Given an [AST](https://astexplorer.net/), transform it.

```js
const sourceCode = "if (true) return;";
const parsedAst = babel.parse(sourceCode, { allowReturnOutsideFunction: true });
const parsedAst = babel.parse(sourceCode, { parserOpts: { allowReturnOutsideFunction: true } });
const { code, map, ast } = babel.transformFromAstSync(parsedAst, sourceCode, options);
```

Expand All @@ -179,9 +179,9 @@ Given an [AST](https://astexplorer.net/), transform it.

```js
const sourceCode = "if (true) return;";
babel.parseAsync(sourceCode, { allowReturnOutsideFunction: true })
babel.parseAsync(sourceCode, { parserOpts: { allowReturnOutsideFunction: true } })
.then(parsedAst => {
return babel.transformFromAstSync(parsedAst, sourceCode, options);
return babel.transformFromAstAsync(parsedAst, sourceCode, options);
})
.then(({ code, map, ast }) => {
// ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import { addSideEffect } from "@babel/helper-module-imports";
addSideEffect(path, 'source');
```

### `import { named } from "source"`
### `import { named as _named } from "source"`

```js
import { addNamed } from "@babel/helper-module-imports";
// if the hintedName isn't set, the function will gennerate a uuid as hintedName itself such as '_named'
addNamed(path, 'named', 'source');
```

Expand Down
3 changes: 2 additions & 1 deletion website/versioned_docs/version-7.0.0/learn.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ building global apps in JavaScript.
"𠮷".match(/./u)[0].length == 2

// new form
"\u{20BB7}" == "𠮷" == "\uD842\uDFB7"
"\u{20BB7}" == "𠮷"
"𠮷" == "\uD842\uDFB7"

// new String ops
"𠮷".codePointAt(0) == 0x20BB7
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.0.0/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ npx babel-node test
>
> ```sh
> NODE_NO_READLINE=1 rlwrap --always-readline npx babel-node
>
> ```

### Usage

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const profile = (

import Preact from 'preact';

const profile = h("div", null,
const profile = Preact.h("div", null,
Preact.h("img", { src: "avatar.png", className: "profile" }),
Preact.h("h3", null, [user.firstName, user.lastName].join(" "))
);
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.0.0/runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This is meant to be used as a runtime `dependency` along with the Babel plugin [

## Why

Sometimes Babel may inject some code in the output that is the same and thus can be potentially re-used.
Sometimes Babel may inject some code in the output that is the same across files, and thus can be potentially re-used.

For example, with the class transform (without loose mode):

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.0.0/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ const fn = template`
`;

const ast = fn({
IMPORT_NAME: t.identifier("myModule");
IMPORT_NAME: t.identifier("myModule"),
});

console.log(generate(ast).code);
Expand Down
8 changes: 7 additions & 1 deletion website/versioned_docs/version-7.0.0/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,15 @@ Now the `env` preset will only load transformation plugins for features that are

## Polyfill

> 🚨 As of Babel 7.4.0, this package has been deprecated in favor of directly including `core-js/stable` (to polyfill ECMAScript features) and `regenerator-runtime/runtime` (needed to use transpiled generator functions):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that this should be backported, since it is only for Babel >= 7.4.0

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching this. I moved it to 7.4.0

> ```js
> import "core-js/stable";
> import "regenerator-runtime/runtime";
> ```

The [@babel/polyfill](polyfill.md) module includes [core-js](https://github.com/zloirock/core-js) and a custom [regenerator runtime](https://github.com/facebook/regenerator/blob/master/packages/regenerator-runtime/runtime.js) to emulate a full ES2015+ environment.

This means you can use new built-ins like `Promise` or `WeakMap`, static methods like `Array.from` or `Object.assign`, instance methods like `Array.prototype.includes`, and generator functions (provided you use the [regenerator](plugin-transform-regenerator.md) plugin). The polyfill adds to the global scope as well as native prototypes like `String` in order to do this.
This means you can use new built-ins like `Promise` or `WeakMap`, static methods like `Array.from` or `Object.assign`, instance methods like `Array.prototype.includes`, and generator functions (when used alongside the regenerator plugin). The polyfill adds to the global scope as well as native prototypes like `String` in order to do this.

For library/tool authors this may be too much. If you don't need the instance methods like `Array.prototype.includes` you can do without polluting the global scope altogether by using the [transform runtime](plugin-transform-runtime.md) plugin instead of `@babel/polyfill`.

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.0.0/v7-migration-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ See Babylon's [plugin options](parser.md#plugins).

It has been renamed to align with the `legacy` option of `@babel/plugin-proposal-decorators`. A new `decorators` plugin has been implemented, which implements the new decorators proposal.

The two versions of the proposals have different syntaxes, so it is highly reccomended to use `decorators-legacy` until the new semantics are implemented by Babel.
The two versions of the proposals have different syntaxes, so it is highly recommended to use `decorators-legacy` until the new semantics are implemented by Babel.

> Removed `classConstructorCall` plugin [#291](https://github.com/babel/babylon/pull/291) ![low](https://img.shields.io/badge/risk%20of%20breakage%3F-low-yellowgreen.svg)

Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.1.0/config-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Babel has two parallel config file formats, which can be used together, or indep

## Project-wide configuration

New in Babel 7.x, Babel has as concept of a ["root"](options.md#root) directory, which defaults
New in Babel 7.x, Babel has a concept of a ["root"](options.md#root) directory, which defaults
to the current working directory. For project-wide configuration, Babel will automatically search
for a `"babel.config.js"` in this root directory. Alternatively, users can use an explicit
["configFile"](options.md#configfile) value to override the default config file search behavior.
Expand Down
2 changes: 1 addition & 1 deletion website/versioned_docs/version-7.4.0/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const fn = template`
`;

const ast = fn({
IMPORT_NAME: t.identifier("myModule");
IMPORT_NAME: t.identifier("myModule"),
});

console.log(generate(ast).code);
Expand Down
139 changes: 76 additions & 63 deletions website/versioned_docs/version-7.5.0/plugin-transform-typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,81 @@ sidebar_label: transform-typescript
original_id: babel-plugin-transform-typescript
---

Does not type-check its input. For that, you will need to install and set up TypeScript.
This plugin adds support for the syntax used by the [TypeScript programming language][ts]. However, this plugin does not add the ability to type-check the JavaScript passed to it. For that, you will need to install and set up TypeScript.

## Example

**In**

```javascript
const x: number = 0;
```

**Out**

```javascript
const x = 0;
```

## Installation

```sh
npm install --save-dev @babel/plugin-transform-typescript
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["@babel/plugin-transform-typescript"]
}
```

### Via CLI

```sh
babel --plugins @babel/plugin-transform-typescript script.js
```

### Via Node API

```javascript
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-typescript"]
});
```


## Caveats

* Since Babel does not type-check, invalid TypeScript may successfully get transformed, and often in unexpected or invalid ways.
Because there are features of the TypeScript language which rely on the full type-system to be available to make changes at runtime. This section of caveats is quite long, however, it's worth noting that a few of these features are only found in older TypeScript codebases and have modern JavaScript equivalents which you are probably already using.

1. Since Babel does not type-check, code which is syntactically correct, but would fail the TypeScript type-checking may successfully get transformed, and often in unexpected or invalid ways.

1. This plugin does not support [`const enum`][const_enum]s because those require type information to compile.

**Workarounds**:
- Use the plugin [babel-plugin-const-enum](https://www.npmjs.com/package/babel-plugin-const-enum).
- Remove the `const`, which makes it available at runtime.

1. This plugin does not support [`export =`][exin] and [`import =`][exin], because those cannot be compiled to ES.next. These are a TypeScript only form of `import`/`export`.

**Workaround**: Convert to using `export default` and `export const`, and `import x, {y} from "z"`.

1. Changes to your `tsconfig.json` are not reflected in babel. The build process will always behave as though [`isolateModules`][iso-mods] is turned on, there are Babel-native alternative ways to set a lot of the [`tsconfig.json` options]((#typescript-compiler-options) however.

1. **Q**: Why doesn't Babel allow export of a `var` or `let`?

**A**: The TypeScript compiler dynamically changes how these variables are used depending on whether or not the value is mutated. Ultimately, this depends on a type-model and is outside the scope of Babel. A best-effort implementation would transform context-dependent usages of the variable to always use the `Namespace.Value` version instead of `Value`, in case it was mutated outside of the current file. Allowing `var` or `let` from Babel (as the transform is not-yet-written) is therefor is more likely than not to present itself as a bug when used as-if it was not `const`.


### Impartial Namespace Support

If you have existing code which uses the TypeScript-only [namespace][namespace] features. Babel supports a subset of TypeScript's namespace features. If you are considering writing new code which uses namespace, using the ES2015 `import`/`export` is recommended instead. It's [not going away][not-disappearing], but there are modern alternatives.

* Type-only `namespace`s should be marked with `declare` and will subsequently be safely removed.

Expand All @@ -21,10 +91,6 @@ Does not type-check its input. For that, you will need to install and set up Typ

**Workaround**: Use `const`. If some form of mutation is required, explicitly use an object with internal mutability.

**Q**: Why doesn't Babel allow export of a `var` or `let`?

**A**: The TypeScript compiler dynamically changes how these variables are used depending on whether or not the value is mutated. Ultimately, this depends on a type-model and is outside the scope of Babel. A best-effort implementation would transform context-dependent usages of the variable to always use the `Namespace.Value` version instead of `Value`, in case it was mutated outside of the current file. Allowing `var` or `let` from Babel (as the transform is not-yet-written) is therefor is more likely than not to present itself as a bug when used as-if it was not `const`.

* `namespace`s will not share their scope. In TypeScript, it is valid to refer to contextual items that a `namespace` extends without qualifying them, and the compiler will add the qualifier. In Babel, there is no type-model, and it is impossible to dynamically change references to match the established type of the parent object.

Consider this code:
Expand Down Expand Up @@ -84,63 +150,6 @@ Does not type-check its input. For that, you will need to install and set up Typ
}
```

* Does not support [`const enum`][const_enum]s because those require type information to compile.

**Workaround**: Remove the `const`, which makes it available at runtime.

* Does not support [`export =`][exin] and [`import =`][exin], because those cannot be compiled to ES.next.

**Workaround**: Convert to using `export default` and `export const`, and `import x, {y} from "z"`.

* Behaves as if the `--isolatedModules` option was passed to the TypeScript Compiler. This can't be worked around because Babel doesn't support cross-file analysis.

* Does not load `tsconfig.json` files. Some options are supported in alternative ways: see [`TypeScript Compiler Options`](#typescript-compiler-options)

## Example

**In**

```javascript
const x: number = 0;
```

**Out**

```javascript
const x = 0;
```

## Installation

```sh
npm install --save-dev @babel/plugin-transform-typescript
```

## Usage

### Via `.babelrc` (Recommended)

**.babelrc**

```json
{
"plugins": ["@babel/plugin-transform-typescript"]
}
```

### Via CLI

```sh
babel --plugins @babel/plugin-transform-typescript script.js
```

### Via Node API

```javascript
require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-typescript"]
});
```
## Options

### `isTSX`
Expand Down Expand Up @@ -232,3 +241,7 @@ equivalents in Babel can be enabled by some configuration options or plugins.
[exin]: https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
[fm]: https://github.com/Microsoft/dtslint/blob/master/docs/no-single-declare-module.md
[tsc-options]: https://www.typescriptlang.org/docs/handbook/compiler-options.html
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces-and-modules.html
[not-disappearing]: https://github.com/microsoft/TypeScript/issues/30994#issuecomment-484150549
[ts]: https://www.typescriptlang.org
[iso-mods]: https://www.typescriptlang.org/docs/handbook/compiler-options.html