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
let root = dirname("/User/github") // returns "User"
23
23
```
24
24
```js
@@ -28,22 +28,53 @@ var root = Path.dirname("/User/github");
28
28
29
29
</CodeTab>
30
30
31
-
Here's what the `external` does:
31
+
Here's what happens:
32
32
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
35
35
-`dirname`: the binding name you'll use on the ReScript side.
36
36
-`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.
38
38
39
-
### Import a JavaScript Module Itself (CommonJS)
39
+
### Import multiple exports from a JavaScript Module
40
40
41
-
By omitting the string argument to `bs.module`, you bind to the whole JS module:
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
42
57
43
58
<CodeTablabels={["ReScript", "JS Output"]}>
44
59
45
60
```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
+
<CodeTablabels={["ReScript", "JS Output"]}>
75
+
76
+
```res example
77
+
import * as leftPad: (string, int) => string from "./leftPad"
47
78
let paddedResult = leftPad("hi", 5)
48
79
```
49
80
```js
@@ -53,14 +84,14 @@ var paddedResult = LeftPad("hi", 5);
53
84
54
85
</CodeTab>
55
86
56
-
### Import a JavaScript Module Itself (ES6 Module Format)
87
+
### Import a JavaScript Module default (ES6 Module Format)
57
88
58
89
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:
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
+
73
124
## Export To JavaScript
74
125
75
126
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