Skip to content

Commit db8ee53

Browse files
committed
ES6 -> JavaScript module with "module": "esmodule"
As follows rescript-lang/rescript#6709
1 parent 229dce1 commit db8ee53

14 files changed

+59
-58
lines changed

data/api/latest/js.json

+10-10
Original file line numberDiff line numberDiff line change
@@ -9057,8 +9057,8 @@
90579057
"id": "Js.WeakMap",
90589058
"name": "WeakMap",
90599059
"docstrings": [
9060-
"Provides bindings for ES6 WeakMap",
9061-
"ES6 WeakMap API"
9060+
"Provides bindings for JavaScript WeakMap",
9061+
"WeakMap API"
90629062
],
90639063
"items": [
90649064
{
@@ -9074,8 +9074,8 @@
90749074
"id": "Js.Map",
90759075
"name": "Map",
90769076
"docstrings": [
9077-
"Provides bindings for ES6 Map",
9078-
"ES6 Map API"
9077+
"Provides bindings for JavaScript Map",
9078+
"Map API"
90799079
],
90809080
"items": [
90819081
{
@@ -9091,8 +9091,8 @@
90919091
"id": "Js.WeakSet",
90929092
"name": "WeakSet",
90939093
"docstrings": [
9094-
"Provides bindings for ES6 WeakSet",
9095-
"ES6 WeakSet API"
9094+
"Provides bindings for JavaScript WeakSet",
9095+
"WeakSet API"
90969096
],
90979097
"items": [
90989098
{
@@ -9108,8 +9108,8 @@
91089108
"id": "Js.Set",
91099109
"name": "Set",
91109110
"docstrings": [
9111-
"Provides bindings for ES6 Set",
9112-
"ES6 Set API"
9111+
"Provides bindings for JavaScript Set",
9112+
"Set API"
91139113
],
91149114
"items": [
91159115
{
@@ -10138,7 +10138,7 @@
1013810138
"kind": "type",
1013910139
"name": "symbol",
1014010140
"docstrings": [
10141-
"Js symbol type (only available in ES6)"
10141+
"Js symbol type"
1014210142
],
1014310143
"signature": "type symbol"
1014410144
},
@@ -14984,4 +14984,4 @@
1498414984
}
1498514985
]
1498614986
}
14987-
}
14987+
}

misc_docs/syntax/decorator_module.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var root = Path.dirname("/User/github");
3333

3434
`@module` also supports [import attributes](https://github.com/tc39/proposal-import-attributes). It looks like this:
3535

36-
<CodeTab labels={["ReScript", "JS Output (ES6)"]}>
36+
<CodeTab labels={["ReScript", "JS Output (Module)"]}>
3737
```rescript
3838
@module({from: "./myJson.json", with: {type_: "json", \"some-exotic-identifier": "someValue"}})
3939
external myJson: Js.Json.t = "default"

pages/docs/manual/latest/array-and-list.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ var x3 = Belt_Array.concatMany([y]);
122122
```
123123
</CodeTab>
124124

125-
> Note that array spreads compiles to `Belt.Array.concatMany` right now. This is likely to change to native ES6 array spreads in the future.
125+
> Note that array spreads compiles to `Belt.Array.concatMany` right now. This is likely to change to native array spreads in the future.
126126
127127
## List
128128

pages/docs/manual/latest/build-configuration.mdx

+5-4
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ The command itself is called from inside `lib/bs`.
122122

123123
## package-specs
124124

125-
Output to either CommonJS (the default) or ES6 modules. Example:
125+
Output to either CommonJS (the default) or JavaScript module. Example:
126126

127127
```json
128128
{
@@ -133,7 +133,8 @@ Output to either CommonJS (the default) or ES6 modules. Example:
133133
}
134134
```
135135

136-
- `"module": "es6-global"` resolves `node_modules` using relative paths. Good for development-time usage of ES6 in conjunction with browsers like Safari and Firefox that support ES6 modules today. **No more dev-time bundling**!
136+
- `"module": "commonjs"` generates output as CommonJS format.
137+
- `"module": "esmodule"` generates output as JavaScript module format. Will be default value in next major.
137138
- `"in-source": true` generates output alongside source files. If you omit it, it'll generate the artifacts into `lib/js`. The output directory is not configurable otherwise.
138139

139140
This configuration only applies to you, when you develop the project. When the project is used as a third-party library, the consumer's own `rescript.json` `package-specs` overrides the configuration here, logically.
@@ -198,7 +199,7 @@ To enable genType, set `"gentypeconfig"` at top level in the project's `rescript
198199
```json
199200
{
200201
"gentypeconfig": {
201-
"module": "es6",
202+
"module": "esmodule",
202203
"moduleResolution": "node",
203204
"generatedFileExtension": ".gen.tsx",
204205
"debug": {
@@ -211,7 +212,7 @@ To enable genType, set `"gentypeconfig"` at top level in the project's `rescript
211212

212213
`generatedFileExtension`: File extension used for genType generated files (defaults to `".gen.tsx"`)
213214

214-
`module`: Module format used for the generated `*.gen.tsx` files (supports `"es6"` and `"commonjs"`)
215+
`module`: Module format used for the generated `*.gen.tsx` files (supports `"esmodule"` and `"commonjs"`)
215216

216217
`moduleResolution`: Module resolution strategy used in genType outputs. This may be required for compatibility with TypeScript projects. Specify the value as the same in `tsconfig.json`.
217218

pages/docs/manual/latest/build-pinned-dependencies.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ The build system respects the following rules for each package type:
3131
- Builds dev dependencies
3232
- Builds pinned dependencies
3333
- Runs custom rules
34-
- Package-specs like ES6/CommonJS overrides all its dependencies
34+
- Package-specs like JavaScript module or CommonJS overrides all its dependencies
3535

3636
**Pinned dependencies**
3737
- Warnings reported

pages/docs/manual/latest/import-from-export-to-js.mdx

+21-21
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ canonical: "/docs/manual/latest/import-from-export-to-js"
88

99
You've seen how ReScript's idiomatic [Import & Export](import-export.md) works. This section describes how we work with importing stuff from JavaScript and exporting stuff for JavaScript consumption.
1010

11-
**Note**: due to JS ecosystem's module compatibility issues, our advice of keeping your ReScript file's compiled JS output open in a tab applies here **more than ever**, as you don't want to subtly output the wrong JS module import/export code, on top of having to deal with Babel/Webpack/Jest/Node's CommonJS \<-> ES6 compatibility shims.
11+
**Note**: due to JS ecosystem's module compatibility issues, our advice of keeping your ReScript file's compiled JS output open in a tab applies here **more than ever**, as you don't want to subtly output the wrong JS module import/export code, on top of having to deal with Babel/Webpack/Jest/Node's CommonJS \<-> JavaScript module compatibility shims.
1212

1313
In short: **make sure your bindings below output what you'd have manually written in JS**.
1414

1515
## Output Format
1616

1717
We support 2 JavaScript import/export formats:
1818

19+
- JavaScript module: `import * from 'MyReScriptFile'` and `export let ...`.
1920
- CommonJS: `require('myFile')` and `module.exports = ...`.
20-
- ES6 modules: `import * from 'MyReScriptFile'` and `export let ...`.
2121

2222
The format is [configurable in via `rescript.json`](build-configuration.md#package-specs).
2323

@@ -27,19 +27,19 @@ The format is [configurable in via `rescript.json`](build-configuration.md#packa
2727

2828
Use the `module` [external](external.md):
2929

30-
<CodeTab labels={["ReScript", "JS Output (CommonJS)", "JS Output (ES6)"]}>
30+
<CodeTab labels={["ReScript", "JS Output (Module)", "JS Output (CommonJS)"]}>
3131

3232
```res example
3333
// Import nodejs' path.dirname
3434
@module("path") external dirname: string => string = "dirname"
3535
let root = dirname("/User/github") // returns "User"
3636
```
3737
```js
38-
var Path = require("path");
38+
import * as Path from "path";
3939
var root = Path.dirname("/User/github");
4040
```
4141
```js
42-
import * as Path from "path";
42+
var Path = require("path");
4343
var root = Path.dirname("/User/github");
4444
```
4545

@@ -57,30 +57,30 @@ Here's what the `external` does:
5757

5858
By omitting the string argument to `module`, you bind to the whole JS module:
5959

60-
<CodeTab labels={["ReScript", "JS Output (CommonJS)", "JS Output (ES6)"]}>
60+
<CodeTab labels={["ReScript", "JS Output (Module)", "JS Output (CommonJS)"]}>
6161

6262
```res example
6363
@module external leftPad: (string, int) => string = "./leftPad"
6464
let paddedResult = leftPad("hi", 5)
6565
```
6666
```js
67-
var LeftPad = require("./leftPad");
67+
import * as LeftPad from "./leftPad";
6868
var paddedResult = LeftPad("hi", 5);
6969
```
7070
```js
71-
import * as LeftPad from "./leftPad";
71+
var LeftPad = require("./leftPad");
7272
var paddedResult = LeftPad("hi", 5);
7373
```
7474

7575
</CodeTab>
7676

77-
Depending on whether you're compiling ReScript to CommonJS or ES6 module, **this feature will generate subtly different code**. Please check both output tabs to see the difference. The ES6 output here would be wrong!
77+
Depending on whether you're compiling ReScript to JavaScript module or CommonJS, **this feature will generate subtly different code**. Please check both output tabs to see the difference. The JavaScript module output here would be wrong!
7878

79-
### Import an ES6 Default Export
79+
### Import an `default` Export
8080

81-
Use the value `"default"` on the right hand side:
81+
Use the value `default` on the right hand side:
8282

83-
<CodeTab labels={["ReScript", "JS Output (ES6)"]}>
83+
<CodeTab labels={["ReScript", "JS Output (Module)"]}>
8484

8585
```res example
8686
@module("./student") external studentName: string = "default"
@@ -97,9 +97,9 @@ var studentName = Student;
9797

9898
**Since 11.1**
9999

100-
[Import attributes](https://github.com/tc39/proposal-import-attributes) can be used in ReScript, as long as ReScript is configured to output ES6. You do that by passing configuration to the `@module` attribute:
100+
[Import attributes](https://github.com/tc39/proposal-import-attributes) can be used in ReScript, as long as ReScript is configured to output JavaScript module. You do that by passing configuration to the `@module` attribute:
101101

102-
<CodeTab labels={["ReScript", "JS Output (ES6)"]}>
102+
<CodeTab labels={["ReScript", "JS Output (Module)"]}>
103103
```rescript
104104
@module({from: "./myJson.json", with: {type_: "json", \"some-exotic-identifier": "someValue"}})
105105
external myJson: Js.Json.t = "default"
@@ -141,7 +141,7 @@ let sub = (a, b) => a - b
141141

142142
Now let's dynamically import the add function in another module, e.g. `App.res`:
143143

144-
<CodeTab labels={["ReScript", "JS Output (ES6)"]}>
144+
<CodeTab labels={["ReScript", "JS Output (Module)"]}>
145145
```rescript
146146
// App.res
147147
let main = async () => {
@@ -165,7 +165,7 @@ async function main() {
165165

166166
### Dynamically Importing an Entire Module
167167
The syntax for importing a whole module looks a little different, since we are operating on the module syntax level; instead of using `Js.import`, you may simply `await` the module itself:
168-
<CodeTab labels={["ReScript", "JS Output (ES6)"]}>
168+
<CodeTab labels={["ReScript", "JS Output (Module)"]}>
169169
```rescript
170170
// App.res
171171
let main = async () => {
@@ -191,9 +191,9 @@ async function main() {
191191

192192
As mentioned in ReScript's idiomatic [Import & Export](import-export.md), every let binding and module is exported by default to other ReScript modules (unless you use a `.resi` [interface file](module#signatures)). If you open up the compiled JS file, you'll see that these values can also directly be used by a _JavaScript_ file too.
193193

194-
### Export an ES6 Default Value
194+
### Export a `default` Value
195195

196-
If your JS project uses ES6 modules, you're likely exporting & importing some default values:
196+
If your JS project uses JavaScript module, you're likely exporting & importing some default values:
197197

198198
```js
199199
// student.js
@@ -207,7 +207,7 @@ import studentName from 'student.js';
207207

208208
A JavaScript default export is really just syntax sugar for a named export implicitly called `default` (now you know!). So to export a default value from ReScript, you can just do:
209209

210-
<CodeTab labels={["ReScript", "JS Output (CommonJS)", "JS Output (ES6)"]}>
210+
<CodeTab labels={["ReScript", "JS Output (Module)", "JS Output (CommonJS)"]}>
211211

212212
```res example
213213
// ReScriptStudent.res
@@ -218,7 +218,7 @@ var $$default = "Bob";
218218

219219
exports.$$default = $$default;
220220
exports.default = $$default;
221-
// informal transpiler-compatible marker of a default export compiled from ES6
221+
// informal transpiler-compatible marker of a default export compiled from JavaScript module
222222
exports.__esModule = true;
223223
```
224224
```js
@@ -239,4 +239,4 @@ You can then import this default export as usual on the JS side:
239239
import studentName from 'ReScriptStudent.js';
240240
```
241241

242-
If your JavaScript's ES6 default import is transpiled by Babel/Webpack/Jest into CommonJS `require`s, we've taken care of that too! See the CommonJS output tab for `__esModule`.
242+
If your JavaScript's default import is transpiled by Babel/Webpack/Jest into CommonJS `require`s, we've taken care of that too! See the CommonJS output tab for `__esModule`.

pages/docs/manual/latest/installation.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bun create rescript-app
118118
],
119119
"package-specs": [
120120
{
121-
"module": "es6",
121+
"module": "esmodule",
122122
"in-source": true
123123
}
124124
],

pages/docs/manual/latest/typescript-integration.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ The minimal configuration of genType is following:
132132
```json
133133
{
134134
"gentypeconfig": {
135-
"module": "es6",
135+
"module": "esmodule",
136136
"moduleResolution": "node",
137137
"generatedFileExtension": ".gen.tsx"
138138
}
@@ -153,7 +153,7 @@ And don't forget to make sure `allowJs` is set to `true` in the project's `tscon
153153

154154
Make sure to set the same `moduleResolution` value in both `rescript.json` and `tsconfig.json`, so that the output of genType is done with the preferred module resolution.
155155

156-
For example if the TypeScript project uses ES Modules with `Node16` / `NodeNext` module resolution:
156+
For example if the TypeScript project uses JavaScript modules with `Node16` / `NodeNext` module resolution:
157157

158158
```json
159159
// tsconfig.json

pages/docs/react/latest/styling.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ let style =
4545
Use a `%%raw` expression to import CSS files within your ReScript / React component code:
4646

4747
```rescript
48-
// in a CommonJS setup
49-
%%raw("require('./styles/main.css')")
50-
51-
// or with ES6
48+
// in a JS module setup
5249
%%raw("import './styles/main.css'")
50+
51+
// or with CommonJS
52+
%%raw("require('./styles/main.css')")
5353
```
5454

5555
## CSS Modules

pages/docs/react/v0.10.0/styling.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ let style =
4545
Use a `%%raw` expression to import CSS files within your ReScript / React component code:
4646

4747
```rescript
48-
// in a CommonJS setup
49-
%%raw("require('./styles/main.css')")
50-
51-
// or with ES6
48+
// in a JS module setup
5249
%%raw("import './styles/main.css'")
50+
51+
// or with CommonJS
52+
%%raw("require('./styles/main.css')")
5353
```
5454

5555
## CSS Modules

pages/docs/react/v0.11.0/styling.mdx

+4-4
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ let style =
4545
Use a `%%raw` expression to import CSS files within your ReScript / React component code:
4646

4747
```rescript
48-
// in a CommonJS setup
49-
%%raw("require('./styles/main.css')")
50-
51-
// or with ES6
48+
// in a JS module setup
5249
%%raw("import './styles/main.css'")
50+
51+
// or with CommonJS
52+
%%raw("require('./styles/main.css')")
5353
```
5454

5555
## CSS Modules

src/Playground.res

+1-1
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,7 @@ module Settings = {
10231023
<div className="mt-6">
10241024
<div className=titleClass> {React.string("Module-System")} </div>
10251025
<ToggleSelection
1026-
values=["nodejs", "es6"]
1026+
values=["commonjs", "esmodule"]
10271027
toLabel={value => value}
10281028
selected=config.module_system
10291029
onChange=onModuleSystemUpdate

src/bindings/RescriptCompilerApi.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -431,8 +431,8 @@ module Compiler = {
431431

432432
let setConfig = (t: t, config: Config.t): unit => {
433433
let moduleSystem = switch config.module_system {
434-
| "nodejs" => #nodejs->Some
435-
| "es6" => #es6->Some
434+
| "commonjs" => #nodejs->Some
435+
| "esmodule" => #es6->Some
436436
| _ => None
437437
}
438438

src/common/CompilerManagerHook.res

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,12 +407,12 @@ let useCompilerManager = (
407407

408408
// Note: The compiler bundle currently defaults to
409409
// commonjs when initiating the compiler, but our playground
410-
// should default to ES6. So we override the config
410+
// should default to esmodule. So we override the config
411411
// and use the `setConfig` function to sync up the
412412
// internal compiler state with our playground state.
413413
let config = {
414414
...instance->Compiler.getConfig,
415-
module_system: "es6",
415+
module_system: "esmodule",
416416
?open_modules,
417417
}
418418
instance->Compiler.setConfig(config)

0 commit comments

Comments
 (0)