Skip to content

Commit ea9197b

Browse files
author
Iwan
committed
Add basic examples for new import syntax
1 parent 10dff18 commit ea9197b

File tree

1 file changed

+63
-12
lines changed

1 file changed

+63
-12
lines changed

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

Lines changed: 63 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ You've seen how ReScript's idiomatic [Import & Export](import-export.md) works.
1010

1111
## Import From JavaScript
1212

13-
### Import a JavaScript Module's Content
13+
### Import a single export from a JavaScript module
1414

15-
Use the `bs.module` [external](external.md):
15+
Use [import](import.md):
1616

1717
<CodeTab labels={["ReScript", "JS Output"]}>
1818

1919
```res example
2020
// Import nodejs' path.dirname
21-
@bs.module("path") external dirname: string => string = "dirname"
21+
import {dirname: string => string} from "path"
2222
let root = dirname("/User/github") // returns "User"
2323
```
2424
```js
@@ -28,22 +28,53 @@ var root = Path.dirname("/User/github");
2828

2929
</CodeTab>
3030

31-
Here's what the `external` does:
31+
Here's what happens:
3232

33-
- `@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.
34-
- `external`: the general keyword for declaring a value that exists on the JS side.
33+
- `import`: the general keyword for importing a value that exists on the JS side.
34+
- `{ ... }`: import a named export
3535
- `dirname`: the binding name you'll use on the ReScript side.
3636
- `string => string`: the type signature of `dirname`.
37-
- `= "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.
37+
- `from "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.
3838

39-
### Import a JavaScript Module Itself (CommonJS)
39+
### Import multiple exports from a JavaScript Module
4040

41-
By omitting the string argument to `bs.module`, you bind to the whole JS module:
41+
<CodeTab labels={["ReScript", "JS Output"]}>
42+
43+
```res example
44+
// Import nodejs' path.dirname
45+
import {isAbsolute: string => bool, dirname: string => string} from "path"
46+
let root = dirname("/User/github") // returns "User"
47+
let isAbsolutePath = isAbsolute("C:/foo/..") // true
48+
```
49+
```js
50+
var Path = require("path");
51+
var root = Path.dirname("/User/github");
52+
var isAbsolutePath = Path.isAbsolute("C:/foo/..");
53+
```
54+
</CodeTab>
55+
56+
### Import an export with a more convenient alias
4257

4358
<CodeTab labels={["ReScript", "JS Output"]}>
4459

4560
```res example
46-
@bs.module external leftPad: string => int => string = "./leftPad"
61+
import {sep as platformSeperator: string} from "path"
62+
Js.log(platformSeperator) // `\` on Windows, `/` on POSIX
63+
```
64+
```js
65+
var Path = require("path");
66+
console.log(Path.sep);
67+
```
68+
</CodeTab>
69+
70+
### Import an entire JavaScript Module contents
71+
72+
you bind to the whole JS module by writing: `* as myModule`
73+
74+
<CodeTab labels={["ReScript", "JS Output"]}>
75+
76+
```res example
77+
import * as leftPad: (string, int) => string from "./leftPad"
4778
let paddedResult = leftPad("hi", 5)
4879
```
4980
```js
@@ -53,14 +84,14 @@ var paddedResult = LeftPad("hi", 5);
5384

5485
</CodeTab>
5586

56-
### Import a JavaScript Module Itself (ES6 Module Format)
87+
### Import a JavaScript Module default (ES6 Module Format)
5788

5889
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:
5990

6091
<CodeTab labels={["ReScript", "JS Output"]}>
6192

6293
```res example
63-
@bs.module("./student") external studentName: string = "default"
94+
import studentName: string from "./student"
6495
Js.log(studentName)
6596
```
6697
```js
@@ -70,6 +101,26 @@ console.log(Student.default);
70101

71102
</CodeTab>
72103

104+
### Import an export with variadic arguments
105+
106+
<CodeTab labels={["ReScript", "JS Output"]}>
107+
108+
```res example
109+
import {@variadic join: array<string> => string} from "path"
110+
let fullPath = join(["Users", "github"])
111+
```
112+
```js
113+
var Path = require("path");
114+
var fullPath = Path.join(["Users", "github"]);
115+
```
116+
</CodeTab>
117+
118+
### Import an export with an illegal ReScript identifier
119+
120+
```res example
121+
import {"Fragment" as make: component<{"children": element}>} from "react"
122+
```
123+
73124
## Export To JavaScript
74125

75126
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.

0 commit comments

Comments
 (0)