Skip to content

Commit f1833fa

Browse files
Merge pull request #759 from fhammerschmidt/installation-create-rescript-app-and-res-js
Quick setup section & .bs.js -> .res.js
2 parents 9d1448a + 28f27cb commit f1833fa

File tree

7 files changed

+113
-52
lines changed

7 files changed

+113
-52
lines changed

pages/docs/manual/latest/build-configuration.mdx

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Your source files need to be specified explicitly (we don't want to accidentally
3434
"sources": ["src", "examples"]
3535
}
3636
```
37+
3738
```json
3839
{
3940
"sources": {
@@ -59,22 +60,22 @@ You can mark your directories as dev-only (for e.g. tests). These won't be built
5960

6061
```json
6162
{
62-
"sources" : {
63-
"dir" : "test",
64-
"type" : "dev"
63+
"sources": {
64+
"dir": "test",
65+
"type": "dev"
6566
}
6667
}
6768
```
6869

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.
7071
Here, the file `src/MyMainModule.res` is exposed to outside consumers, while all other files are private.
7172

7273
```json
7374
{
7475
"sources": {
7576
"dir": "src",
7677
"public": ["MyMainModule"]
77-
},
78+
}
7879
}
7980
```
8081

@@ -140,27 +141,22 @@ This configuration only applies to you, when you develop the project. When the p
140141
## suffix
141142

142143
**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+
144146
- `".js`
145147
- `".mjs"`
146148
- `".cjs"`
147149
- `".res.js"`
148150
- `".res.mjs"`
149151
- `".res.cjs"`
150-
- `".bs.js"`
151-
- `".bs.mjs"`
152-
- `".bs.cjs"`
153-
154-
Currently prefer `.bs.js` for now.
155152

156153
### Design Decisions
157154

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:
155+
Generating JS files with the `.res.js` suffix means that, on the JS side, you can do `const myReScriptFile = require('./TheFile.res.js')`. The benefits:
159156

160157
- It's immediately clear that we're dealing with a generated JS file here.
161-
- It avoids clashes with a potential `theFile.js` file in the same folder.
158+
- It avoids clashes with a potential `TheFile.js` file in the same folder.
162159
- It avoids the need of using a build system loader for ReScript files. This + in-source build means integrating a ReScript project into your pure JS codebase **basically doesn't touch anything in your build pipeline at all**.
163-
- [genType](/docs/gentype/latest/introduction) requires `bs.js` for compiled JS artifacts. If you are using `genType`, you need to use `bs.js` for now.
164160

165161
## uncurried
166162

pages/docs/manual/latest/converting-from-js.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ Add this file to `rescript.json`:
8282
},
8383
```
8484

85-
Open an editor tab for `src/Main.bs.js`. Do a command-line `diff -u src/main.js src/Main.bs.js`. Aside from whitespaces, you should see only minimal, trivial differences. You're already a third of the way done!
85+
Open an editor tab for `src/Main.res.js`. Do a command-line `diff -u src/main.js src/Main.res.js`. Aside from whitespaces, you should see only minimal, trivial differences. You're already a third of the way done!
8686

87-
**Always make sure** that at each step, you keep the ReScript output `.bs.js` file open to compare against the existing JavaScript file. Our compilation output is very close to your hand-written JavaScript; you can simply eye the difference to catch conversion bugs!
87+
**Always make sure** that at each step, you keep the ReScript output `.res.js` file open to compare against the existing JavaScript file. Our compilation output is very close to your hand-written JavaScript; you can simply eye the difference to catch conversion bugs!
8888

8989
## Step 3: Extract Parts into Idiomatic ReScript
9090

@@ -225,9 +225,9 @@ exports.queryResult = queryResult;
225225

226226
We hurrily typed `school` as a polymorphic `'whatever` and let its type be inferred by its usage below. The inference is technically correct, but within the context of bringing it a value from JavaScript, slightly dangerous. This is just the interop trick we've shown in the [`external`](external) page.
227227

228-
Anyway, the file passes the type checker again. Check the `.bs.js` output, diff with the original `.js`; we've now converted a file over to ReScript!
228+
Anyway, the file passes the type checker again. Check the `.res.js` output, diff with the original `.js`; we've now converted a file over to ReScript!
229229

230-
Now, you can delete the original, hand-written `main.js` file, and grep the files importing `main.js` and change them to importing `Main.bs.js`.
230+
Now, you can delete the original, hand-written `main.js` file, and grep the files importing `main.js` and change them to importing `Main.res.js`.
231231

232232
## (Optional) Step 5: Cleanup
233233

@@ -283,7 +283,7 @@ We've:
283283
- typed the payload as a record with only the `student` field
284284
- typed `getStudentById` as the sole method of `student`
285285

286-
Check that the `.bs.js` output didn't change. How rigidly to type your JavaScript code is up to you; we recommend not typing them too elaborately; it's sometime an endless chase, and produces diminishing returns, especially considering that the elaborate-ness might turn off your potential teammates.
286+
Check that the `.res.js` output didn't change. How rigidly to type your JavaScript code is up to you; we recommend not typing them too elaborately; it's sometime an endless chase, and produces diminishing returns, especially considering that the elaborate-ness might turn off your potential teammates.
287287

288288
## Tips & Tricks
289289

pages/docs/manual/latest/import-export.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Unlike JavaScript, ReScript doesn't have or need import statements:
1717
let studentMessage = Student.message
1818
```
1919
```js
20-
var Student = require("./Student.bs");
20+
var Student = require("./Student.res.js");
2121
var studentMessage = Student.message
2222
```
2323

pages/docs/manual/latest/installation.mdx

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,98 @@ canonical: "/docs/manual/latest/installation"
99
## Prerequisites
1010

1111
- [Node.js](https://nodejs.org/) version >= 14
12-
- [npm](https://docs.npmjs.com/cli/) (which comes with Node.js) or [Yarn](https://yarnpkg.com/)
12+
- One of the following package managers
13+
- [npm](https://docs.npmjs.com/cli/) (comes with Node.js)
14+
- [yarn](https://yarnpkg.com/)
15+
- [pnpm](https://pnpm.io/)
16+
- [bun](https://bun.sh/)
1317

1418
## New Project
1519

16-
```sh
17-
git clone https://github.com/rescript-lang/rescript-project-template
18-
cd rescript-project-template
19-
npm install
20-
npm run res:build
21-
node src/Demo.bs.js
22-
```
20+
The fastest and easiest way to spin up a new ReScript project is with the [create-rescript-app](https://github.com/rescript-lang/create-rescript-app) project generator. You can start it with any of the aforementioned package managers or `npx`.
2321

24-
or use the create-rescript-app tool:
22+
<CodeTab labels={["npm", "npx", "yarn", "pnpm", "bun"]}>
2523

24+
```sh example
25+
npm create rescript-app
26+
```
27+
```sh
28+
npx create-rescript-app
29+
```
2630
```sh
27-
npm create rescript-app // Select basic template
28-
cd <your-rescript-project-name>
29-
npm run res:build
30-
node src/Demo.bs.js
31+
yarn create rescript-app
32+
```
33+
```sh
34+
pnpm create rescript-app
35+
```
36+
```sh
37+
bun create rescript-app
3138
```
3239

33-
That compiles your ReScript into JavaScript, then uses Node.js to run said JavaScript. **We recommend you use our unique workflow of keeping a tab open for the generated `.bs.js` file**, so that you can learn how ReScript transforms into JavaScript. Not many languages output clean JavaScript code you can inspect and learn from!
40+
</CodeTab>
41+
42+
- Follow the steps of the setup.
43+
- Trigger a ReScript build:
44+
```sh
45+
npm run res:build
46+
```
47+
- If you selected the basic theme, simply run it with:
48+
```sh
49+
node src/Demo.res.js
50+
```
51+
52+
That compiles your ReScript into JavaScript, then uses Node.js to run said JavaScript. **We recommend you use our unique workflow of keeping a tab open for the generated `.res.js` file**, so that you can learn how ReScript transforms into JavaScript. Not many languages output clean JavaScript code you can inspect and learn from!
3453

3554
During development, instead of running `npm run res:build` each time to compile, use `npm run res:dev` to start a watcher that recompiles automatically after file changes.
3655

3756
## Integrate Into an Existing JS Project
3857

39-
If you already have a JavaScript project into which you'd like to add ReScript:
58+
If you already have a JavaScript project into which you'd like to add ReScript you can do that in the following ways:
59+
60+
### Quick Setup
61+
62+
In the root directory of your project, execute:
63+
<CodeTab labels={["npm", "npx", "yarn", "pnpm", "bun"]}>
64+
65+
```sh
66+
npm create rescript-app
67+
```
68+
```sh
69+
npx create-rescript-app
70+
```
71+
```sh
72+
yarn create rescript-app
73+
```
74+
```sh
75+
pnpm create rescript-app
76+
```
77+
```sh
78+
bun create rescript-app
79+
```
80+
81+
</CodeTab>
4082

83+
`create-rescript-app` will tell you that a `package.json` file has been detected and ask you if it should install ReScript into your project. Just follow the steps accordingly.
84+
85+
### Manual Setup
4186
- Install ReScript locally:
87+
<CodeTab labels={["npm", "yarn", "pnpm", "bun"]}>
88+
4289
```sh
4390
npm install rescript
4491
```
45-
- Create a ReScript build configuration at the root:
92+
```sh
93+
yarn add rescript
94+
```
95+
```sh
96+
pnpm install rescript
97+
```
98+
```sh
99+
bun install rescript
100+
```
101+
102+
</CodeTab>
103+
- Create a ReScript build configuration file (called `rescript.json`) at the root:
46104
```json
47105
{
48106
"name": "your-project-name",
@@ -58,7 +116,7 @@ If you already have a JavaScript project into which you'd like to add ReScript:
58116
"in-source": true
59117
}
60118
],
61-
"suffix": ".bs.js",
119+
"suffix": ".res.js",
62120
"bs-dependencies": []
63121
}
64122
```

pages/docs/manual/latest/interop-with-js-build-systems.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ You can make ReScript JS files look even more idiomatic through the in-source +
3333
"module": "commonjs", // or whatever module system your project uses
3434
"in-source": true
3535
},
36-
"suffix": ".bs.js"
36+
"suffix": ".res.js"
3737
}
3838
```
3939

4040
This will:
4141

4242
- Generate the JS files alongside your ReScript source files.
43-
- Use the file extension `.bs.js`, so that you can require these files on the JS side through `require('./MyFile.bs')`, without needing a loader.
43+
- Use the file extension `.res.js`, so that you can require these files on the JS side through `require('./MyFile.res.js')`, without needing a loader.
4444

4545
## Use Loaders on ReScript Side
4646

pages/docs/react/latest/router.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,10 @@ let make = () => {
8484
```
8585
```js
8686
import * as React from "react";
87-
import * as User from "./User.bs.js";
88-
import * as RescriptReactRouter from "@rescript/react/src/RescriptReactRouter.bs.js";
89-
import * as Home from "./Home.bs.js";
90-
import * as NotFound from "./NotFound.bs.js";
87+
import * as User from "./User.res.js";
88+
import * as RescriptReactRouter from "@rescript/react/src/RescriptReactRouter.res.js";
89+
import * as Home from "./Home.res.js";
90+
import * as NotFound from "./NotFound.res.js";
9191

9292
function App(Props) {
9393
var url = RescriptReactRouter.useUrl(undefined, undefined);

src/components/CodeExample.res

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,22 +222,29 @@ module Toggle = {
222222
})
223223
->Belt.Option.getWithDefault(React.null)
224224

225-
let buttonDiv = switch Js.Array2.find(multiple, tab => {
225+
// On a ReScript tab, always copy or open the ReScript code. Otherwise, copy the current selected code.
226+
let isReScript = tab =>
226227
switch tab.lang {
227228
| Some("res") | Some("rescript") => true
228229
| _ => false
229230
}
230-
}) {
231+
232+
let buttonDiv = switch Js.Array2.findi(multiple, (tab, index) =>
233+
tab->isReScript || index === selected
234+
) {
231235
| Some({code: ""}) => React.null
232236
| Some(tab) =>
233237
let playgroundLinkButton =
234-
<Next.Link
235-
href={`/try?code=${LzString.compressToEncodedURIComponent(tab.code)}}`} target="_blank">
236-
// ICON Link to PLAYGROUND
237-
<Icon.ExternalLink
238-
className="text-gray-30 mt-px hover:cursor-pointer hover:text-gray-60 hover:bg-gray-30 w-6 h-6 p-1 rounded transition-all duration-300 ease-in-out"
239-
/>
240-
</Next.Link>
238+
tab->isReScript
239+
? <Next.Link
240+
href={`/try?code=${LzString.compressToEncodedURIComponent(tab.code)}}`}
241+
target="_blank">
242+
// ICON Link to PLAYGROUND
243+
<Icon.ExternalLink
244+
className="text-gray-30 mt-px hover:cursor-pointer hover:text-gray-60 hover:bg-gray-30 w-6 h-6 p-1 rounded transition-all duration-300 ease-in-out"
245+
/>
246+
</Next.Link>
247+
: React.null
241248

242249
let copyButton = <CopyButton code={tab.code} />
243250

0 commit comments

Comments
 (0)