Skip to content

Commit 26eeca6

Browse files
authored
Use more Yarn workspaces in scripts (#7391)
* Add packages to workspaces * add comment about yarn * use Yarn workspace instead npm link * use an isolated directory for installation test * remove unused script * exclude installation test template from workspaces * compose playground settings into a single workspace * allow overwrite binaries * don't rebuild entriely on building playground * revert unintended change * fix installation test * fix upload script * use better variable name
1 parent 220b3d0 commit 26eeca6

25 files changed

+548
-166
lines changed

.github/workflows/ci.yml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -406,21 +406,18 @@ jobs:
406406

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

414411
- name: Test playground compiler
415412
if: matrix.build_playground
416-
run: node playground/playground_test.cjs
413+
run: yarn workspace playground test
417414

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

425422
- name: "Upload artifacts: binaries"
426423
if: matrix.upload_binaries
@@ -513,23 +510,36 @@ jobs:
513510
with:
514511
node-version-file: .nvmrc
515512

513+
- name: Make test directory
514+
id: tmp-dir
515+
shell: bash
516+
run: |
517+
if [[ "$RUNNER_OS" == "Windows" ]]; then
518+
dir=$(powershell -Command "[System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString()" | tr -d '\r')
519+
mkdir -p "$dir"
520+
else
521+
dir=$(mktemp -d)
522+
fi
523+
echo "path=$dir" >> "$GITHUB_OUTPUT"
524+
cp -r tests/package_tests/installation_test/* "$dir"
525+
516526
- name: Download artifacts
517527
uses: actions/download-artifact@v4
518528
with:
519529
name: npm-packages
520-
path: packages/test
530+
path: ${{ steps.tmp-dir.outputs.path }}
521531

522532
- name: Install ReScript package
523533
run: |
524534
npm i --ignore-scripts --no-audit \
525535
rescript-${{ needs.package.outputs.rescript_version }}.tgz
526536
shell: bash
527-
working-directory: packages/test
537+
working-directory: ${{ steps.tmp-dir.outputs.path }}
528538

529539
- name: Test installation
530-
run: npx rescript -h && npx rescript build && cat src/Test.bs.js
540+
run: npx rescript -h && npx rescript build && cat src/Test.res.js
531541
shell: bash
532-
working-directory: packages/test
542+
working-directory: ${{ steps.tmp-dir.outputs.path }}
533543

534544
publish:
535545
needs: [package, installationTest]
@@ -564,5 +574,5 @@ jobs:
564574
- name: Update Website Playground
565575
env:
566576
NEXT_REVALIDATE_SECRET_TOKEN: ${{ secrets.NEXT_REVALIDATE_SECRET_TOKEN }}
567-
run: ./playground/website_update_playground.sh
577+
run: yarn workspace playground revalidate
568578
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ test-all: test test-gentype test-analysis test-tools
5151
reanalyze:
5252
reanalyze.exe -set-exit-code -all-cmt _build/default/compiler _build/default/tests -exclude-paths compiler/outcome_printer,compiler/ml,compiler/frontend,compiler/ext,compiler/depends,compiler/core,compiler/common,compiler/cmij,compiler/bsb_helper,compiler/bsb
5353

54-
lib: build
54+
lib:
5555
./scripts/buildRuntime.sh
5656
./scripts/prebuilt.js
5757

@@ -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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
"typescript": "5.8.2"
8888
},
8989
"workspaces": [
90+
"packages/*",
9091
"tests/dependencies/**",
9192
"tests/analysis_tests/**",
9293
"tests/gentype_tests/**",

packages/playground-bundling/package.json

Lines changed: 0 additions & 23 deletions
This file was deleted.

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/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"private": true,
3+
"name": "playground",
4+
"type": "commonjs",
5+
"scripts": {
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"
11+
},
12+
"dependencies": {
13+
"@rescript/react": "^0.13.1",
14+
"rescript": "workspace:^"
15+
},
16+
"devDependencies": {
17+
"@rollup/plugin-node-resolve": "^16.0.0",
18+
"glob": "^11.0.1",
19+
"rollup": "^4.32.0"
20+
}
21+
}

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`);

0 commit comments

Comments
 (0)