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/build-configuration.mdx
+10-8
Original file line number
Diff line number
Diff line change
@@ -34,6 +34,7 @@ Your source files need to be specified explicitly (we don't want to accidentally
34
34
"sources": ["src", "examples"]
35
35
}
36
36
```
37
+
37
38
```json
38
39
{
39
40
"sources": {
@@ -59,22 +60,22 @@ You can mark your directories as dev-only (for e.g. tests). These won't be built
59
60
60
61
```json
61
62
{
62
-
"sources": {
63
-
"dir": "test",
64
-
"type": "dev"
63
+
"sources": {
64
+
"dir": "test",
65
+
"type": "dev"
65
66
}
66
67
}
67
68
```
68
69
69
-
You can also explicitly allow which modules can be seen from outside. This feature is especially useful for library authors who want to have a single entry point for their users.
70
+
You can also explicitly allow which modules can be seen from outside. This feature is especially useful for library authors who want to have a single entry point for their users.
70
71
Here, the file `src/MyMainModule.res` is exposed to outside consumers, while all other files are private.
71
72
72
73
```json
73
74
{
74
75
"sources": {
75
76
"dir": "src",
76
77
"public": ["MyMainModule"]
77
-
},
78
+
}
78
79
}
79
80
```
80
81
@@ -140,7 +141,8 @@ This configuration only applies to you, when you develop the project. When the p
140
141
## suffix
141
142
142
143
**Since 11.0**: The suffix can now be freely chosen. However, we still suggest you stick to the convention and use
143
-
one of the following:
144
+
one of the following:
145
+
144
146
-`".js`
145
147
-`".mjs"`
146
148
-`".cjs"`
@@ -151,11 +153,11 @@ one of the following:
151
153
-`".bs.mjs"`
152
154
-`".bs.cjs"`
153
155
154
-
Currently prefer `.bs.js` for now.
156
+
Currently prefer `.res.js` for now.
155
157
156
158
### Design Decisions
157
159
158
-
Generating JS files with the `.bs.js` suffix means that, on the JS side, you can do `const myReScriptFile = require('./theFile.bs')`. The benefits:
160
+
Generating JS files with the `.res.js` suffix means that, on the JS side, you can do `const myReScriptFile = require('./theFile.res')`. The benefits:
159
161
160
162
- It's immediately clear that we're dealing with a generated JS file here.
161
163
- It avoids clashes with a potential `theFile.js` file in the same folder.
`genType` is a code generation tool that lets you export ReScript values and types to use in TypeScript (TS), and import TS values and types into ReScript.
9
+
The ReScript compiler includes a code generation tool that lets you export ReScript values and types to use in TypeScript (TS), and import TS values and types into ReScript. It is called `genType`.
10
10
11
11
Converter functions between the two runtime representations are generated when required based on the type of the values.
12
12
In particular, conversion of [rescript-react](/docs/react/latest/introduction) components both ways is supported, with automatic generation of the wrappers.
13
13
14
-
Here's an article describing how to use `genType` as part of a migration strategy where a tree of components is gradually converted to ReScript bottom-up (old article containing Reason / BuckleScript): [Adopting Reason: strategies, dual sources of truth, and why genType is a big deal](https://medium.com/p/c514265b466d).
15
-
16
14
The implementation of genType performs a type-directed transformation of ReScript programs after compilation. The transformed programs operate on data types idiomatic to JS.
17
15
18
-
For example, a ReScript function operating on a ReScript variant `type t = | A(int) | B(string)` (which is represented as custom objects with tags at runtime) is exported to a JS function operating on the corresponding JS object of type `{tag: "A"; value: number}
19
-
| {tag: "B"; value: string}`.
16
+
For example, a ReScript function operating on a ReScript variant `type t = | A(int) | B(string)` (which is represented as custom objects with tags at runtime) is exported to a JS function operating on the corresponding JS object of type `{ TAG: "A"; _0: number } | { TAG: "B"; _0: string };`.
20
17
21
18
## A Quick Example
22
19
@@ -34,7 +31,7 @@ First we'll set up a rescript-react component:
34
31
@genType
35
32
type color =
36
33
| Red
37
-
| Blue;
34
+
| Blue
38
35
39
36
@genType
40
37
@react.component
@@ -43,44 +40,40 @@ let make = (~name: string, ~color: color) => {
const result =React.createElement(MyCompBS.make, $props);
77
-
returnresult
78
-
};
68
+
exportconst make:React.ComponentType<{
69
+
readonly color:color;
70
+
readonly name:string;
71
+
}> =MainBS.make;
79
72
```
80
73
81
74
genType automatically maps the `color` variant to TS via a string union type `color = "Red" | "Blue"`, and also provides all the converters to convert between the ReScript & TS representation as well.
82
75
83
-
Therefore way we can seamlessly use ReScript specific data structures within TS without writing the converter code by hand!
76
+
Therefore we can seamlessly use ReScript specific data structures within TS without writing the converter code by hand!
84
77
85
78
Within our TypeScript application, we can now import and use the React component in the following manner:
86
79
@@ -89,17 +82,113 @@ Within our TypeScript application, we can now import and use the React component
For detailed information, head to the [Getting Started](/docs/manual/latest/gentype-getting-started) or [Usage](/docs/manual/latest/gentype-usage) section.
102
97
103
-
## Development
98
+
## Setup
99
+
100
+
Since compiler v10.1, there's no need to install anything. For compiler 10.0 or older, install the binaries via `npm` (or `yarn`):
101
+
102
+
```
103
+
npm install gentype --save-dev
104
+
105
+
# Verify installed gentype binary
106
+
npx gentype --help
107
+
```
108
+
109
+
Add a `gentypeconfig` section to your `rescript.json` (See [Configuration](#configuration) for details):
110
+
111
+
```json
112
+
"gentypeconfig": {
113
+
"language": "typescript",
114
+
"shims": {},
115
+
"generatedFileExtension": ".gen.tsx",
116
+
"module": "es6",
117
+
"debug": {
118
+
"all": false,
119
+
"basic": false
120
+
}
121
+
}
122
+
```
123
+
124
+
## Configuration
125
+
126
+
Every `genType` powered project requires a configuration item `"gentypeconfig"`
127
+
at top level in the project's `rescript.json`. The configuration has following
128
+
structure:
129
+
130
+
```json
131
+
//...
132
+
"gentypeconfig": {
133
+
"language": "typescript",
134
+
"generatedFileExtension": ".gen.tsx",
135
+
"module": "es6"| "commonjs",
136
+
"shims": {
137
+
"ReasonReact": "ReactShim"
138
+
}
139
+
}
140
+
```
141
+
142
+
-**generatedFileExtension**
143
+
144
+
- File extension used for genType generated files (defaults to `.gen.tsx`)
145
+
146
+
-**language**
147
+
148
+
-`"typescript"` : the `language` setting is not required from compiler v10.1
149
+
150
+
-**module**
151
+
152
+
- Module format used for the generated `*.gen.tsx` files (supports `"es6"` and `"commonjs"`)
153
+
154
+
-**shims**
155
+
- Required only if one needs to export certain basic ReScript data types to JS when one cannot modify the sources to add annotations (e.g. exporting ReScript lists), and if the types are not first-classed in genType.
156
+
- Example: `Array<string>` with format: `"RescriptModule=JavaScriptModule"`
157
+
158
+
## Adding Shims
159
+
160
+
A shim is a TS file that provides user-provided definitions for library types.
161
+
162
+
Configure your shim files within `"gentypeconfig"` in your [`rescript.json`](https://github.com/rescript-lang/rescript-compiler/blob/master/jscomp/gentype_tests/typescript-react-example/rescript.json), and add relevant `.shims.ts` files in a directory which is visible by ReScript e.g. [`src/shims/`](https://github.com/rescript-lang/rescript-compiler/tree/master/jscomp/gentype_tests/typescript-react-example/src/shims). An example shim to export ReactEvent can be found [here](https://github.com/rescript-lang/rescript-compiler/blob/master/jscomp/gentype_tests/typescript-react-example/src/shims/ReactEvent.shim.ts).
163
+
164
+
## Testing the Whole Setup
165
+
166
+
Open any relevant `*.res` file and add `@genType` annotations to any bindings / values / functions to be used from JavaScript. If an annotated value uses a type, the type must be annotated too. See e.g. [Hooks.res](https://github.com/reason-association/genType/blob/master/examples/typescript-react-example/src/Hooks.res).
167
+
168
+
Save the file and rebuild the project via `npm run bs:build` or similar. You should now see a `*.gen.tsx` file with the same name (e.g. `MyComponent.res` -> `MyComponent.gen.tsx`).
169
+
170
+
Any values exported from `MyComponent.res` can then be imported from JS. For example:
- Emit prop types for the untyped back-end. To activate, add `"propTypes": true` and `"language": "untyped"` to the configuration.
189
+
190
+
## Limitations
191
+
192
+
-**in-source = true**. Currently only supports ReScript projects with [in-source generation](/docs/manual/latest/build-configuration#package-specs) and `.bs.js` file suffix.
104
193
105
-
Since ReScript v10.1, genType is part of the compiler's [GitHub repository](https://github.com/rescript-lang/rescript-compiler).
194
+
-**Limited namespace support**. Currently there's limited [namespace](/docs/manual/latest/build-configuration#name-namespace) support, and only `namespace:true` is possible, not e.g. `namespace:"custom"`.
0 commit comments