Skip to content

Commit 450a709

Browse files
authored
add a script to serve playground bundle locally (#7834)
* add a script to serve playground bundle locally * fix routing * fix, tested * add a manual
1 parent d911ca6 commit 450a709

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed

CONTRIBUTING.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,17 @@ $ node
286286

287287
Run `yarn workspace playground test` for a quick sanity check to see if all the build artifacts are working together correctly. When releasing the playground bundle, the test will always be executed before publishing to catch regressions.
288288

289+
You can test the bundle within the playground by running a web server and playground locally.
290+
291+
```sh
292+
# Serve playground bundles locally
293+
yarn workspace playground serve-bundle
294+
295+
# And run the website with "PLAYGROUND_BUNDLE_ENDPOINT"
296+
cd path/to/rescript-lang.org
297+
PLAYGROUND_BUNDLE_ENDPOINT=http://localhost:8888 npm run dev
298+
```
299+
289300
### Working on the Playground JS API
290301

291302
Whenever you are modifying any files in the ReScript compiler, or in the `jsoo_playground_main.ml` file, you'll need to rebuild the source and recreate the JS bundle.

packages/playground/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"clean": "rescript clean",
77
"test": "node ./playground_test.cjs",
88
"build": "rescript clean && rescript legacy build && node scripts/generate_cmijs.mjs && rollup -c",
9-
"upload-bundle": "node scripts/upload_bundle.mjs"
9+
"upload-bundle": "node scripts/upload_bundle.mjs",
10+
"serve-bundle": "node serve-bundle.mjs"
1011
},
1112
"dependencies": {
1213
"@rescript/react": "^0.13.1",
@@ -15,6 +16,7 @@
1516
"devDependencies": {
1617
"@rollup/plugin-node-resolve": "^16.0.0",
1718
"glob": "^11.0.1",
19+
"h3": "2.0.0-beta.4",
1820
"rollup": "^4.32.0"
1921
}
2022
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { stat, readFile } from "node:fs/promises";
2+
import * as path from "node:path";
3+
import { H3, serve, serveStatic } from "h3";
4+
5+
import compilerBundle from "./compiler.js";
6+
7+
const compilerVersion = compilerBundle.rescript_compiler.version;
8+
const localVersion = "v" + compilerVersion.toString();
9+
/**
10+
* @param {string} id
11+
*/
12+
function toLocalPath(id) {
13+
const originalId = id.slice(localVersion.length + 1);
14+
if (originalId === "/compiler.js") {
15+
return path.join(import.meta.dirname, originalId);
16+
}
17+
return path.join(
18+
import.meta.dirname,
19+
"packages",
20+
originalId,
21+
);
22+
}
23+
24+
const versionContent = new H3()
25+
.get("/**", event => {
26+
return serveStatic(event, {
27+
getContents: id => {
28+
const localPath = toLocalPath(id);
29+
return localPath && readFile(localPath);
30+
},
31+
getMeta: async id => {
32+
const localPath = toLocalPath(id);
33+
const stats = await stat(localPath).catch(() => {});
34+
if (stats?.isFile()) {
35+
return {
36+
size: stats.size,
37+
mtime: stats.mtimeMs,
38+
};
39+
}
40+
},
41+
});
42+
});
43+
44+
const app = new H3()
45+
.get("/playground-bundles/versions.json", () => [localVersion])
46+
.mount(`/${localVersion}`, versionContent);
47+
48+
serve(app, { port: 8888 });

yarn.lock

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,13 @@ __metadata:
11171117
languageName: node
11181118
linkType: hard
11191119

1120+
"cookie-es@npm:^2.0.0":
1121+
version: 2.0.0
1122+
resolution: "cookie-es@npm:2.0.0"
1123+
checksum: 10c0/3b2459030a5ad2bc715aeb27a32f274340670bfc5031ac29e1fba804212517411bb617880d3fe66ace2b64dfb28f3049e2d1ff40d4bec342154ccdd124deaeaa
1124+
languageName: node
1125+
linkType: hard
1126+
11201127
"cross-spawn@npm:^7.0.0, cross-spawn@npm:^7.0.3, cross-spawn@npm:^7.0.6":
11211128
version: 7.0.6
11221129
resolution: "cross-spawn@npm:7.0.6"
@@ -1280,6 +1287,13 @@ __metadata:
12801287
languageName: node
12811288
linkType: hard
12821289

1290+
"fetchdts@npm:^0.1.6":
1291+
version: 0.1.7
1292+
resolution: "fetchdts@npm:0.1.7"
1293+
checksum: 10c0/7497671ee28b3e7cc49d037a7c11e6f98e53e6e2abd85f26e8fac3fae4142c3c0d91352dc46198530f73b16480585605c63e3d112c34210ff2c75be1199668a0
1294+
languageName: node
1295+
linkType: hard
1296+
12831297
"fill-range@npm:^7.1.1":
12841298
version: 7.1.1
12851299
resolution: "fill-range@npm:7.1.1"
@@ -1501,6 +1515,23 @@ __metadata:
15011515
languageName: node
15021516
linkType: hard
15031517

1518+
"h3@npm:2.0.0-beta.4":
1519+
version: 2.0.0-beta.4
1520+
resolution: "h3@npm:2.0.0-beta.4"
1521+
dependencies:
1522+
cookie-es: "npm:^2.0.0"
1523+
fetchdts: "npm:^0.1.6"
1524+
rou3: "npm:^0.7.3"
1525+
srvx: "npm:^0.8.7"
1526+
peerDependencies:
1527+
crossws: ^0.4.1
1528+
peerDependenciesMeta:
1529+
crossws:
1530+
optional: true
1531+
checksum: 10c0/ac40213784bab18a96509048d82338763484dddab303763ab549c1fc2e41c8c83a00013154fd9861042b9016f91dc0be3e6c12e553e7450910dc692c17e59403
1532+
languageName: node
1533+
linkType: hard
1534+
15041535
"has-flag@npm:^4.0.0":
15051536
version: 4.0.0
15061537
resolution: "has-flag@npm:4.0.0"
@@ -2412,6 +2443,7 @@ __metadata:
24122443
"@rescript/react": "npm:^0.13.1"
24132444
"@rollup/plugin-node-resolve": "npm:^16.0.0"
24142445
glob: "npm:^11.0.1"
2446+
h3: "npm:2.0.0-beta.4"
24152447
rescript: "workspace:^"
24162448
rollup: "npm:^4.32.0"
24172449
languageName: unknown
@@ -2680,6 +2712,13 @@ __metadata:
26802712
languageName: node
26812713
linkType: hard
26822714

2715+
"rou3@npm:^0.7.3":
2716+
version: 0.7.3
2717+
resolution: "rou3@npm:0.7.3"
2718+
checksum: 10c0/f20224a724838c8a0cbdd8b37462ad684b59f87a8314aa7e6a1a892f96d806bcf504bcb9108f9d812cdb6194e1a347adc15f2c379238bfbc3b723e33dc71ca46
2719+
languageName: node
2720+
linkType: hard
2721+
26832722
"safe-buffer@npm:^5.1.0":
26842723
version: 5.2.1
26852724
resolution: "safe-buffer@npm:5.2.1"
@@ -2839,6 +2878,17 @@ __metadata:
28392878
languageName: node
28402879
linkType: hard
28412880

2881+
"srvx@npm:^0.8.7":
2882+
version: 0.8.7
2883+
resolution: "srvx@npm:0.8.7"
2884+
dependencies:
2885+
cookie-es: "npm:^2.0.0"
2886+
bin:
2887+
srvx: bin/srvx.mjs
2888+
checksum: 10c0/67b0b0acbb660df966dd0486e93eba456535190b38a8817f45afbfc4a8892146d69bf8183cdc270bfbbc360169df26f4b636d29ca6b28a4c5eec20d3432ab317
2889+
languageName: node
2890+
linkType: hard
2891+
28422892
"ssri@npm:^12.0.0":
28432893
version: 12.0.0
28442894
resolution: "ssri@npm:12.0.0"

0 commit comments

Comments
 (0)