Skip to content
Merged
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
6 changes: 4 additions & 2 deletions docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,13 @@ The `sourceRoot` fields to set in the generated source map, if one is desired.

### `sourceType`

Type: `"script" | "module" | "unambiguous"`<br />
Type: `"script" | "module" | "commonjs" | "unambiguous"`<br />
Default: "module"<br />

- `"script"` - Parse the file using the ECMAScript Script grammar. No `import`/`export` statements allowed, and files are not in strict mode.
- `"module"` - Parse the file using the ECMAScript Module grammar. Files are automatically strict, and `import`/`export` statements are allowed.
- `"commonjs"` - Parse the file as it will be run in a CommonJS environment. This option is recommended when transforming `.cjs` sources. See [Parser docs](./parser.md#options) for syntax
differences between `"script"` and `"commonjs"`.
- `"unambiguous"` - Consider the file a "module" if `import`/`export` statements are present, or else consider it a "script".

`unambiguous` can be quite useful in contexts where the type is unknown, but it can lead to
Expand Down Expand Up @@ -886,7 +888,7 @@ Here are some examples, on how matching works:
| File extension pattern (`*.ext`) | `foo/*.js` | `/src/foo/test.js`, `/src/foo/index.js/` | `/src/foo/test.ts`, `/src/foo/test.js.map` |
| Combined patterns | `**/test/*.js` | `/src/test/file.js`, `/src/a/b/test/file.js` | `/src/test.js`, `/src/test/sub/file.js` |

Here are examples, where `*` does *not* have a wildcard function:
Here are examples, where `*` does *not* have a wildcard function:

| Description | Pattern | Does Not Match |
| --------------------- | ---------------- | ----------------------------------------------------------- |
Expand Down
5 changes: 4 additions & 1 deletion docs/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ mind. When in doubt, use `.parse()`.

| Version | Changes |
| --------- | -------------------------------------------------- |
| `v7.28.0` | Added `sourceType: "commonjs"` |
| `v7.27.0` | Added `allowYieldOutsideFunction` |
| `v7.26.0` | Added `startIndex` |
| `v7.23.0` | Added `createImportExpressions` |
Expand Down Expand Up @@ -96,7 +97,9 @@ mind. When in doubt, use `.parse()`.
- **plugins**: Array containing the plugins that you want to enable.

- **sourceType**: Indicate the mode the code should be parsed in. Can be
one of `"script"`, `"module"`, or `"unambiguous"`. Defaults to `"script"`. `"unambiguous"` will make @babel/parser attempt to _guess_, based on the presence of ES6 `import` or `export` statements. Files with ES6 `import`s and `export`s are considered `"module"` and are otherwise `"script"`.
one of `"script"`, `"commonjs"`, `"module"` or `"unambiguous"`. Defaults to `"script"`. `"unambiguous"` will make @babel/parser attempt to _guess_, based on the presence of ES6 `import` or `export` statements. Files with ES6 `import`s and `export`s are considered `"module"` and are otherwise `"script"`.

The `"commonjs"` mode indicates that the code should be run in a CommonJS environment such as Node.js. It is mostly compatible with the `"script"` mode except that `return`, `new.target` and `using`/`await using` declarations are allowed in the top level.

- **sourceFilename**: Correlate output AST nodes with their source filename. Useful when generating code and source maps from the ASTs of multiple input files.

Expand Down
1 change: 1 addition & 0 deletions website/src/components/repl/ReplOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ class ExpandedContainer extends Component<Props, State> {
>
<option value="module">Module</option>
<option value="script">Script</option>
<option value="commonjs">CommonJS</option>
<option value="unambiguous">Unambiguous</option>
</select>
</label>
Expand Down
2 changes: 1 addition & 1 deletion website/src/components/repl/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export type PluginStateMap = {
[name: string]: PluginState;
};

export type SourceType = "script" | "module" | "unambiguous";
export type SourceType = "script" | "module" | "commonjs" | "unambiguous";

export type CompileConfig = {
envConfig: EnvConfig | undefined | null;
Expand Down