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-from-export-to-js.mdx
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -93,6 +93,39 @@ var studentName = Student;
93
93
94
94
</CodeTab>
95
95
96
+
### Use Import Attributes
97
+
98
+
**Since 11.1**
99
+
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:
importMyJsonJsonfrom"./myJson.json" with {"type": "json", "some-exotic-identifier": "someValue"};
112
+
113
+
var myJson = MyJsonJson;
114
+
115
+
console.log(myJson);
116
+
```
117
+
</CodeTab>
118
+
119
+
This above imports the local `./myJson.json` file, adding import attributes.
120
+
121
+
This is how it works:
122
+
1. Instead of passing a string or tuple to `@module`, pass a record.
123
+
2. This record should have a `from` key. The value of that is where you want the module to be imported from (just like the regular string to `@module` is).
124
+
3. It should also have a `with` key, with another record where you put all the import attributes you want emitted.
125
+
126
+
Notice `\"some-exotic-identifier"` - you'll need to escape any key that's not a valid ReScript record key.
127
+
Also notice `type_`. Since `type` is a reserved keyword in ReScript, you can use `type_` instead. It will be output as `type` in the JavaScript code.
128
+
96
129
## Dynamic Import
97
130
Leveraging JavaScript's [dynamic `import`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import) to reduce bundle size and lazy load code as needed is easy in ReScript. It's also a little bit more convenient than in regular JavaScript because you don't need to keep track of file paths manually with ReScript's module system.
0 commit comments