You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/docs/manual/latest/import-export.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ A ReScript project's file names need to be unique.
31
31
32
32
By default, every file's type declaration, binding and module is exported, aka publicly usable by another file. **This also means those values, once compiled into JS, are immediately usable by your JS code**.
33
33
34
-
To only export a few selected things, use an[interface file](module.md#signatures).
34
+
To only export a few selected things, use a `.resi`[interface file](module.md#signatures).
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.
10
10
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<->ES6compatibilityshims.
-`@bs.module("path")`: pass the name of the JS module as a string; in this case, `"path"`. The string can be anything: `"./src/myJsFile"`, `"@myNpmNamespace/myLib"`, etc.
50
+
-`@bs.module("path")`: pass the name of the JS module; in this case, `"path"`. The string can be anything: `"./src/myJsFile"`, `"@myNpmNamespace/myLib"`, etc.
34
51
-`external`: the general keyword for declaring a value that exists on the JS side.
35
52
-`dirname`: the binding name you'll use on the ReScript side.
36
-
-`string => string`: the type signature of `dirname`.
53
+
-`string => string`: the type signature of `dirname`. Mandatory for `external`s.
37
54
-`= "dirname"`: the name of the variable inside the `path` JS module. There's repetition in writing the first and second `dirname`, because sometime the binding name you want to use on the ReScript side is different than the variable name the JS module exported.
38
55
39
-
### Import a JavaScript Module Itself (CommonJS)
56
+
### Import a JavaScript Module As a Single Value
40
57
41
58
By omitting the string argument to `bs.module`, you bind to the whole JS module:
@bs.module external leftPad: string => int => string = "./leftPad"
@@ -50,40 +67,41 @@ let paddedResult = leftPad("hi", 5)
50
67
var LeftPad =require("./leftPad");
51
68
var paddedResult =LeftPad("hi", 5);
52
69
```
70
+
```js
71
+
import*asLeftPadfrom"./leftPad";
72
+
var paddedResult =LeftPad("hi", 5);
73
+
```
53
74
54
75
</CodeTab>
55
76
56
-
### Import a JavaScript Module Itself (ES6 Module Format)
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!
78
+
79
+
### Import an ES6 Default Export (Since >= 8.3.0)
57
80
58
-
If your JS project is using ES6, you're likely using Babel to compile it to regular JavaScript. Babel's ES6 default export actually exports the default value under the name `default`. You'd bind to it like this:
As mentioned in ReScript's idiomatic [Import & Export](import-export.md), every let binding and module is exported by default to other ReScript modules. If you open up the compiled JS file, you'll see that these values can also directly be used by another _JavaScript_ file too.
76
-
77
-
We support 2 JS export formats:
98
+
### Export a Named Value
78
99
79
-
- CommonJS (usable from JS as `require('myFile')`).
80
-
- ES6 modules (usable from JS as `import * from 'myFile'`).
100
+
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.
81
101
82
-
The output format is [configurable in bsb](build-configuration.md#package-specs).
102
+
### Export an ES6 Default Value
83
103
84
-
### Export an ES6 default value
85
-
86
-
If your JS project is using ES6 modules, you're likely exporting & importing some default values:
104
+
If your JS project uses ES6 modules, you're likely exporting & importing some default values:
87
105
88
106
```js
89
107
// student.js
@@ -95,33 +113,38 @@ export default name = "Al";
95
113
importstudentNamefrom'student.js';
96
114
```
97
115
98
-
Technically, since a ReScript file maps to a module, there's no such thing as "default" export, only named ones. However, we've made an exception to support default module when you do the following:
116
+
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:
// informal transpiler-compatible marker of a default export compiled from ES6
111
130
exports.__esModule=true;
112
131
```
132
+
```js
133
+
var $$default ="Bob";
134
+
135
+
export {
136
+
$$default,
137
+
$$defaultasdefault,
138
+
}
139
+
```
113
140
114
141
</CodeTab>
115
142
116
-
You can then require the default as normal JS side:
143
+
You can then import this default export as usual on the JS side:
117
144
118
145
```js
119
146
// teacher2.js
120
-
importstudentNamefrom'FavoriteStudent.js';
147
+
importstudentNamefrom'ReScriptStudent.js';
121
148
```
122
149
123
-
**Note**: the above JS snippet _only_ works if you're using that ES6 import/export syntax in JS. If you're still using `require`, you'd need to do:
124
-
125
-
```js
126
-
let studentName =require('FavoriteStudent').default;
127
-
```
150
+
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`.
0 commit comments