Skip to content

Commit 2c5d7e2

Browse files
authored
Add Dynamic import (#178)
* upgrade rescript to v11 * format files * add import * update tests * update CHANGELOG.md
1 parent 5e9abe4 commit 2c5d7e2

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Add `Dict.forEach`, `Dict.forEachWithKey` and `Dict.mapValues` https://github.com/rescript-association/rescript-core/pull/181
88
- Remove internal xxxU helper functions that are not needed anymore in uncurried mode. https://github.com/rescript-association/rescript-core/pull/191
99
- Rename `Object.empty` to `Object.make` for consistency.
10+
- Add dynamic `import`. https://github.com/rescript-association/rescript-core/pull/178
1011

1112
## 1.0.0
1213

src/RescriptCore.res

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,43 @@ external null: Core__Nullable.t<'a> = "#null"
5252
external undefined: Core__Nullable.t<'a> = "#undefined"
5353
external typeof: 'a => Core__Type.t = "#typeof"
5454

55+
/**
56+
`import(value)` dynamically import a value or function from a ReScript
57+
module. The import call will return a `promise`, resolving to the dynamically loaded
58+
value.
59+
60+
## Examples
61+
62+
`MathUtils.res` file:
63+
64+
```rescript
65+
let add = (a, b) => a + b
66+
let sub = (a, b) => a - b
67+
```
68+
In other file you can import the `add` value defined in `MathUtils.res`
69+
70+
```rescript
71+
let main = async () => {
72+
let add = await import(MathUtils.add)
73+
let onePlusOne = add(1, 1)
74+
Console.log(onePlusOne)
75+
}
76+
```
77+
78+
Compiles to:
79+
80+
```javascript
81+
async function main() {
82+
var add = await import("./MathUtils.mjs").then(function(m) {
83+
return m.add;
84+
});
85+
var onePlusOne = add(1, 1);
86+
console.log(onePlusOne);
87+
}
88+
```
89+
*/
90+
external import: 'a => promise<'a> = "#import"
91+
5592
type t<'a> = Js.t<'a>
5693
module MapperRt = Js.MapperRt
5794
module Internal = Js.Internal

test/ImportTests.mjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Test from "./Test.mjs";
4+
5+
async function main() {
6+
var eq = await import("./IntTests.mjs").then(function (m) {
7+
return m.eq;
8+
});
9+
return Test.run([
10+
[
11+
"ImportTests.res",
12+
5,
13+
22,
14+
55
15+
],
16+
"dynamic import - Int tests - eq"
17+
], 1, eq, 1);
18+
}
19+
20+
export {
21+
main ,
22+
}
23+
/* Test Not a pure module */

test/ImportTests.res

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
open RescriptCore
2+
3+
let main = async () => {
4+
let eq = await import(IntTests.eq)
5+
Test.run(__POS_OF__("dynamic import - Int tests - eq"), 1, eq, 1)
6+
}
7+
8+
main->ignore

0 commit comments

Comments
 (0)