Skip to content

Commit 456260f

Browse files
committed
compose playground settings into a single workspace
1 parent 9617480 commit 456260f

15 files changed

+64
-81
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,21 +405,18 @@ jobs:
405405

406406
- name: Build playground compiler
407407
if: matrix.build_playground
408-
run: |
409-
opam exec -- node packages/playground-bundling/scripts/generate_cmijs.js
410-
opam exec -- dune build --profile browser
411-
cp ./_build/default/compiler/jsoo/jsoo_playground_main.bc.js playground/compiler.cjs
408+
run: opam exec -- make playground playground-cmijs
412409

413410
- name: Test playground compiler
414411
if: matrix.build_playground
415-
run: node playground/playground_test.cjs
412+
run: yarn workspace playground test
416413

417414
- name: Upload playground compiler to CDN
418415
if: ${{ matrix.build_playground && startsWith(github.ref, 'refs/tags/v') }}
419416
env:
420417
KEYCDN_USER: ${{ secrets.KEYCDN_USER }}
421418
KEYCDN_PASSWORD: ${{ secrets.KEYCDN_PASSWORD }}
422-
run: bash playground/upload_bundle.sh
419+
run: yarn workspace playground upload-bundle
423420

424421
- name: "Upload artifacts: binaries"
425422
if: matrix.upload_binaries
@@ -576,5 +573,5 @@ jobs:
576573
- name: Update Website Playground
577574
env:
578575
NEXT_REVALIDATE_SECRET_TOKEN: ${{ secrets.NEXT_REVALIDATE_SECRET_TOKEN }}
579-
run: ./playground/website_update_playground.sh
576+
run: yarn workspace playground revalidate
580577
shell: bash

CONTRIBUTING.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -241,39 +241,39 @@ make playground
241241
make playground-cmijs
242242
```
243243

244-
Note that building the cmijs is based on the dependencies defined in `packages/playground-bundling/package.json`. In case you want to build some different version of e.g. `@rescript/react` or just want to add a new package, change the definition within the `package.json` file and run `make playground-cmijs` again.
244+
Note that building the cmijs is based on the dependencies defined in `packages/playground/package.json`. In case you want to build some different version of e.g. `@rescript/react` or just want to add a new package, change the definition within the `package.json` file and run `yarn workspace playground build` again.
245245

246246
After a successful compilation, you will find following files in your project:
247247

248248
- `playground/compiler.js` -> This is the ReScript compiler, which binds the ReScript API to the `window` object.
249-
- `playground/packages` -> Contains third party deps with cmij.js files (as defined in `packages/playground-bundling/bsconfig.json`)
250-
- `playground/compilerCmij.js` -> The compiler base cmij containing all the relevant core modules (`Js`, `Belt`, `Pervasives`, etc.)
249+
- `playground/packages/compiler-builtins` -> The compiler base cmij containing all the relevant core modules (`Js`, `Belt`, `Pervasives`, etc.)
250+
- `playground/packages/*` -> Contains third party deps with cmij.js files (as defined in `packages/playground/rescript.json`)
251251

252252
You can now use the `compiler.js` file either directly by using a `<script src="/path/to/compiler.js"/>` and `<script src="/path/to/packages/compilerCmij.js"/>` inside a html file, use a browser bundler infrastructure to optimize it, or use `nodejs` to run it on a command line:
253253

254254
```
255255
$ node
256-
> require("./compiler.js");
257-
> require("./packages/compilerCmij.js")
258-
> let compiler = rescript_compiler.make()
259-
> let result = compiler.rescript.compile(`Js.log(Sys.ocaml_version)`);
256+
> let { rescript_compiler } = require("./compiler.js");
257+
> require("./packages/compiler-builtins/cmij.js")
258+
> let { rescript } = rescript_compiler.make()
259+
> let result = rescript.compile(`Console.log(${rescript.version})`);
260260
> eval(result.js_code);
261-
4.06.2+BS
262261
```
263262

264263
### Testing the Playground bundle
265264

266-
Run `node playground/playground_test.cjs` 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.
265+
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.
267266

268267
### Working on the Playground JS API
269268

270269
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.
271270

272271
```
273272
make playground
273+
yarn workspace playground build
274274
275275
# optionally run your test / arbitrary node script to verify your changes
276-
node playground/playground_test.cjs
276+
yarn workspace playground test
277277
```
278278

279279
### Publishing the Playground Bundle on our KeyCDN

Makefile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ artifacts: lib
6161
# Builds the core playground bundle (without the relevant cmijs files for the runtime)
6262
playground:
6363
dune build --profile browser
64-
cp ./_build/default/compiler/jsoo/jsoo_playground_main.bc.js playground/compiler.cjs
64+
cp ./_build/default/compiler/jsoo/jsoo_playground_main.bc.js packages/playground/compiler.js
6565

6666
# Creates all the relevant core and third party cmij files to side-load together with the playground bundle
6767
playground-cmijs: artifacts
68-
node packages/playground-bundling/scripts/generate_cmijs.js
68+
yarn workspace playground build
6969

7070
# Builds the playground, runs some e2e tests and releases the playground to the
7171
# CDN (requires KEYCDN_USER and KEYCDN_PASSWORD set in the env variables)
7272
playground-release: playground playground-cmijs
73-
node playground/playground_test.cjs
74-
sh playground/upload_bundle.sh
73+
yarn workspace playground test
74+
yarn workspace playground upload-bundle
7575

7676
format:
7777
bash scripts/format.sh

packages/playground-bundling/.gitignore renamed to packages/playground/.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ lib/bs
2525
.merlin
2626
.bsb.lock
2727
/node_modules/
28-
lib/
29-
packages/
28+
/lib/
3029
.DS_Store
3130
package-lock.json
3231
yarn.lock
32+
33+
/packages/
34+
/compiler.js
Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,17 @@
1-
# rescript-compiler-js
1+
# ReScript Playground bundle
22

3-
This repo is useful to generate a bunch of cmij.js files for a list of dependencies ready to be used in the ReScript playground.
3+
This repo is useful to generate a bunch of `cmij.js` files for a list of dependencies ready to be used in the ReScript playground.
44

55
## Setup
66

7-
Check the `bsconfig.json` and `package.json` files for the respective versions used for the compiler & packages.
8-
9-
It's useful to `npm link` the compiler locally to prevent mismatching compiler versions and therefore "stale artifact" warnings.
10-
11-
```
12-
# Go to the root of the rescript-compiler project
13-
cd ../..
14-
15-
npm link
16-
17-
# Link the local build in this project as well
18-
cd packages/playground-bundling
19-
npm link rescript
20-
```
7+
Check the `rescript.json` and `package.json` files for the respective versions used for the compiler & packages.
218

229
## Building
2310

24-
Run the following commands:
11+
Run the following command:
2512

2613
```
27-
npm run build
28-
node scripts/generate_cmijs.js
14+
yarn workspace playground build
2915
```
3016

3117
All the cmij files will now be available in the `packages/` directory with a structure like this:
@@ -55,12 +41,13 @@ packages
5541
Let's assume our `compiler.js` file represents our playground bundle, you'd first load the compiler, and then load any cmij file:
5642

5743
```
58-
require("playground/compiler.js")
59-
require("packages/@rescript/react/cmij.js")
44+
const { rescript_compiler } = require("./compiler.js");
6045
61-
let comp = rescript_compiler.make()
62-
comp.rescript.compile("let a = <div/>")
46+
require("./packages/compiler-builtins/cmij.js");
47+
require("./packages/@rescript/react/cmij.js");
48+
49+
let comp = rescript_compiler.make();
50+
comp.rescript.compile("let a = <div/>");
6351
```
6452

6553
The script above will be able to successfully compile this React code, since all the `React` module functionality required by JSX was injected in the compiler's state.
66-

packages/playground-bundling/package.json renamed to packages/playground/package.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"private": true,
3-
"name": "playground-bundling",
4-
"type": "module",
3+
"name": "playground",
4+
"type": "commonjs",
55
"scripts": {
6-
"build": "rescript clean && rescript build && node ./scripts/generate_cmijs.js",
7-
"bundle": "rollup -c",
8-
"clean": "rescript clean"
6+
"clean": "rescript clean",
7+
"test": "node ./playground_test.cjs",
8+
"build": "rescript clean && rescript build && node ./scripts/generate_cmijs.mjs && rollup -c",
9+
"upload-bundle": "./scripts/upload_bundle.sh",
10+
"revalidate": "./scripts/website_update_playground.sh"
911
},
1012
"dependencies": {
1113
"@rescript/react": "^0.13.1",

playground/playground_test.cjs renamed to packages/playground/playground_test.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Playground bundle is UMD module
22
// It uses `module.exports` in current context, or fallback to `globalThis`
3-
const { rescript_compiler } = require("./compiler.cjs");
3+
const { rescript_compiler } = require("./compiler.js");
44

5-
require("./packages/compiler-builtins/cmij.cjs");
6-
require("./packages/@rescript/react/cmij.cjs");
5+
require("./packages/compiler-builtins/cmij.js");
6+
require("./packages/@rescript/react/cmij.js");
77

88
const compiler = rescript_compiler.make();
99

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
22
"name": "playground",
33
"version": "0.1.0",
4-
"bs-dependencies": ["@rescript/react"],
5-
"package-specs": {
6-
"module": "esmodule",
7-
"in-source": false
8-
},
94
"sources": {
105
"dir": "src",
116
"subdirs": true
127
},
13-
"uncurried": true
8+
"package-specs": {
9+
"module": "esmodule",
10+
"in-source": false,
11+
"suffix": ".res.js"
12+
},
13+
"bs-dependencies": [
14+
"@rescript/react"
15+
]
1416
}

packages/playground-bundling/rollup.config.mjs renamed to packages/playground/rollup.config.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { glob } from "glob";
44

55
const RESCRIPT_COMPILER_ROOT_DIR = path.join(import.meta.dirname, "..", "..");
66
const LIB_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "lib");
7-
const PLAYGROUND_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "playground");
7+
88
// Final target output directory where all the cmijs will be stored
9-
const PACKAGES_DIR = path.join(PLAYGROUND_DIR, "packages");
9+
const PACKAGES_DIR = path.join(import.meta.dirname, "packages");
1010
const outputFolder = path.join(PACKAGES_DIR, "compiler-builtins", "stdlib");
1111

1212
const entryPoint = await glob(`${LIB_DIR}/es6/*.js`);

packages/playground-bundling/scripts/generate_cmijs.js renamed to packages/playground/scripts/generate_cmijs.mjs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ const RESCRIPT_COMPILER_ROOT_DIR = path.join(
2626
"..",
2727
);
2828

29-
const PLAYGROUND_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "playground");
30-
3129
// The playground-bundling root dir
32-
const PROJECT_ROOT_DIR = path.join(import.meta.dirname, "..");
30+
const PLAYGROUND_DIR = path.join(import.meta.dirname, "..");
3331

3432
// Final target output directory where all the cmijs will be stored
3533
const PACKAGES_DIR = path.join(PLAYGROUND_DIR, "packages");
@@ -43,14 +41,13 @@ fs.mkdirSync(PACKAGES_DIR, { recursive: true });
4341
function e(cmd) {
4442
console.log(`>>>>>> running command: ${cmd}`);
4543
child_process.execSync(cmd, {
46-
cwd: PROJECT_ROOT_DIR,
44+
cwd: PLAYGROUND_DIR,
4745
encoding: "utf8",
4846
stdio: [0, 1, 2],
4947
});
5048
console.log("<<<<<<");
5149
}
5250

53-
e("yarn install");
5451
e("yarn rescript clean");
5552
e("yarn rescript");
5653

@@ -68,7 +65,7 @@ function buildCompilerCmij() {
6865
const outputFolder = path.join(PACKAGES_DIR, "compiler-builtins");
6966
fs.mkdirSync(outputFolder, { recursive: true });
7067

71-
const cmijFile = path.join(outputFolder, "cmij.cjs");
68+
const cmijFile = path.join(outputFolder, "cmij.js");
7269

7370
e(
7471
`find ${rescriptLibOcamlFolder} -name "*.cmi" -or -name "*.cmj" | xargs -n1 basename | xargs js_of_ocaml build-fs -o ${cmijFile} -I ${rescriptLibOcamlFolder}`,
@@ -94,7 +91,7 @@ function buildThirdPartyCmijs() {
9491
const outputFolder = path.join(PACKAGES_DIR, pkg);
9592
fs.mkdirSync(outputFolder, { recursive: true });
9693

97-
const cmijFile = path.join(outputFolder, "cmij.cjs");
94+
const cmijFile = path.join(outputFolder, "cmij.js");
9895

9996
e(`find ${libEs6Folder} -name '*.js' -exec cp {} ${outputFolder} \\;`);
10097
e(
@@ -103,10 +100,5 @@ function buildThirdPartyCmijs() {
103100
}
104101
}
105102

106-
function bundleStdlibJs() {
107-
e("yarn bundle");
108-
}
109-
110103
buildCompilerCmij();
111104
buildThirdPartyCmijs();
112-
bundleStdlibJs();

playground/upload_bundle.sh renamed to packages/playground/scripts/upload_bundle.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# This script will publish the compiler.js bundle / packages cmij.js files to our KeyCDN server.
44
# The target folder on KeyCDN will be the compiler.js' version number.
55
# This script requires `curl` / `openssl` to be installed.
66

7-
SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd -P)
7+
SCRIPT_PATH=${BASH_SOURCE[0]}
8+
SCRIPT_DIR=$(dirname "$(dirname "$current_script_path")")
89

910
# Get the actual version from the compiled playground bundle
10-
VERSION=$(cd $SCRIPT_DIR; node -e 'require("./compiler.js"); console.log(rescript_compiler.make().rescript.version)')
11+
VERSION=$(cd $SCRIPT_DIR; node -e 'require("/compiler.js"); console.log(rescript_compiler.make().rescript.version)')
1112

1213
if [ -z "${KEYCDN_USER}" ]; then
1314
echo "KEYCDN_USER environment variable not set. Make sure to set the environment accordingly."

playground/website_update_playground.sh renamed to packages/playground/scripts/website_update_playground.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#/usr/bin/sh
1+
#!/usr/bin/env bash
22

33
if [ -z "${NEXT_REVALIDATE_SECRET_TOKEN}" ]; then
44
echo "NEXT_REVALIDATE_SECRET_TOKEN environment variable not set."

yarn.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,9 +2340,9 @@ __metadata:
23402340
languageName: node
23412341
linkType: hard
23422342

2343-
"playground-bundling@workspace:packages/playground-bundling":
2343+
"playground@workspace:packages/playground":
23442344
version: 0.0.0-use.local
2345-
resolution: "playground-bundling@workspace:packages/playground-bundling"
2345+
resolution: "playground@workspace:packages/playground"
23462346
dependencies:
23472347
"@rescript/react": "npm:^0.13.1"
23482348
"@rollup/plugin-node-resolve": "npm:^16.0.0"

0 commit comments

Comments
 (0)