Skip to content

Use more Yarn workspaces in scripts #7391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,18 @@ jobs:

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

- name: Test playground compiler
if: matrix.build_playground
run: node playground/playground_test.cjs
run: yarn workspace playground test

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

- name: "Upload artifacts: binaries"
if: matrix.upload_binaries
Expand Down Expand Up @@ -513,23 +510,36 @@ jobs:
with:
node-version-file: .nvmrc

- name: Make test directory
id: tmp-dir
shell: bash
run: |
if [[ "$RUNNER_OS" == "Windows" ]]; then
dir=$(powershell -Command "[System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString()" | tr -d '\r')
mkdir -p "$dir"
else
dir=$(mktemp -d)
fi
echo "path=$dir" >> "$GITHUB_OUTPUT"
cp -r tests/package_tests/installation_test/* "$dir"

- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: npm-packages
path: packages/test
path: ${{ steps.tmp-dir.outputs.path }}

- name: Install ReScript package
run: |
npm i --ignore-scripts --no-audit \
rescript-${{ needs.package.outputs.rescript_version }}.tgz
shell: bash
working-directory: packages/test
working-directory: ${{ steps.tmp-dir.outputs.path }}

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

publish:
needs: [package, installationTest]
Expand Down Expand Up @@ -564,5 +574,5 @@ jobs:
- name: Update Website Playground
env:
NEXT_REVALIDATE_SECRET_TOKEN: ${{ secrets.NEXT_REVALIDATE_SECRET_TOKEN }}
run: ./playground/website_update_playground.sh
run: yarn workspace playground revalidate
shell: bash
20 changes: 10 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,39 +241,39 @@ make playground
make playground-cmijs
```

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

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

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

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:

```
$ node
> require("./compiler.js");
> require("./packages/compilerCmij.js")
> let compiler = rescript_compiler.make()
> let result = compiler.rescript.compile(`Js.log(Sys.ocaml_version)`);
> let { rescript_compiler } = require("./compiler.js");
> require("./packages/compiler-builtins/cmij.js")
> let { rescript } = rescript_compiler.make()
> let result = rescript.compile(`Console.log(${rescript.version})`);
> eval(result.js_code);
4.06.2+BS
```

### Testing the Playground bundle

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

### Working on the Playground JS API

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.

```
make playground
yarn workspace playground build

# optionally run your test / arbitrary node script to verify your changes
node playground/playground_test.cjs
yarn workspace playground test
```

### Publishing the Playground Bundle on our KeyCDN
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ test-all: test test-gentype test-analysis test-tools
reanalyze:
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

lib: build
lib:
./scripts/buildRuntime.sh
./scripts/prebuilt.js

Expand All @@ -61,17 +61,17 @@ artifacts: lib
# Builds the core playground bundle (without the relevant cmijs files for the runtime)
playground:
dune build --profile browser
cp ./_build/default/compiler/jsoo/jsoo_playground_main.bc.js playground/compiler.cjs
cp ./_build/default/compiler/jsoo/jsoo_playground_main.bc.js packages/playground/compiler.js

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

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

format:
bash scripts/format.sh
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"typescript": "5.8.2"
},
"workspaces": [
"packages/*",
"tests/dependencies/**",
"tests/analysis_tests/**",
"tests/gentype_tests/**",
Expand Down
23 changes: 0 additions & 23 deletions packages/playground-bundling/package.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ lib/bs
.merlin
.bsb.lock
/node_modules/
lib/
packages/
/lib/
.DS_Store
package-lock.json
yarn.lock

/packages/
/compiler.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
# rescript-compiler-js
# ReScript Playground bundle

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

## Setup

Check the `bsconfig.json` and `package.json` files for the respective versions used for the compiler & packages.

It's useful to `npm link` the compiler locally to prevent mismatching compiler versions and therefore "stale artifact" warnings.

```
# Go to the root of the rescript-compiler project
cd ../..

npm link

# Link the local build in this project as well
cd packages/playground-bundling
npm link rescript
```
Check the `rescript.json` and `package.json` files for the respective versions used for the compiler & packages.

## Building

Run the following commands:
Run the following command:

```
npm run build
node scripts/generate_cmijs.js
yarn workspace playground build
```

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

```
require("playground/compiler.js")
require("packages/@rescript/react/cmij.js")
const { rescript_compiler } = require("./compiler.js");

let comp = rescript_compiler.make()
comp.rescript.compile("let a = <div/>")
require("./packages/compiler-builtins/cmij.js");
require("./packages/@rescript/react/cmij.js");

let comp = rescript_compiler.make();
comp.rescript.compile("let a = <div/>");
```

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.

21 changes: 21 additions & 0 deletions packages/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"private": true,
"name": "playground",
"type": "commonjs",
"scripts": {
"clean": "rescript clean",
"test": "node ./playground_test.cjs",
"build": "rescript clean && rescript build && node ./scripts/generate_cmijs.mjs && rollup -c",
"upload-bundle": "./scripts/upload_bundle.sh",
"revalidate": "./scripts/website_update_playground.sh"
},
"dependencies": {
"@rescript/react": "^0.13.1",
"rescript": "workspace:^"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^16.0.0",
"glob": "^11.0.1",
"rollup": "^4.32.0"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Playground bundle is UMD module
// It uses `module.exports` in current context, or fallback to `globalThis`
const { rescript_compiler } = require("./compiler.cjs");
const { rescript_compiler } = require("./compiler.js");

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

const compiler = rescript_compiler.make();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "playground",
"version": "0.1.0",
"bs-dependencies": ["@rescript/react"],
"package-specs": {
"module": "esmodule",
"in-source": false
},
"sources": {
"dir": "src",
"subdirs": true
},
"uncurried": true
"package-specs": {
"module": "esmodule",
"in-source": false,
"suffix": ".res.js"
},
"bs-dependencies": [
"@rescript/react"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { glob } from "glob";

const RESCRIPT_COMPILER_ROOT_DIR = path.join(import.meta.dirname, "..", "..");
const LIB_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "lib");
const PLAYGROUND_DIR = path.join(RESCRIPT_COMPILER_ROOT_DIR, "playground");

// Final target output directory where all the cmijs will be stored
const PACKAGES_DIR = path.join(PLAYGROUND_DIR, "packages");
const PACKAGES_DIR = path.join(import.meta.dirname, "packages");
const outputFolder = path.join(PACKAGES_DIR, "compiler-builtins", "stdlib");

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