Skip to content

Commit f504740

Browse files
committed
refactor generate_cmijs script
1 parent f255c9d commit f504740

File tree

4 files changed

+64
-71
lines changed

4 files changed

+64
-71
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ 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 packages/playground/compiler.js
64+
cp -f ./_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

packages/playground/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"clean": "rescript clean",
77
"test": "node ./playground_test.cjs",
8-
"build": "rescript clean && rescript build && node ./scripts/generate_cmijs.mjs && rollup -c",
8+
"build": "rescript clean && rescript build && node scripts/generate_cmijs.mjs && rollup -c",
99
"upload-bundle": "./scripts/upload_bundle.sh",
1010
"revalidate": "./scripts/website_update_playground.sh"
1111
},
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// @ts-check
2+
3+
import * as child_process from "node:child_process";
4+
import * as fs from "node:fs";
5+
import * as path from "node:path";
6+
7+
export const compilerRootDir = path.join(
8+
import.meta.dirname,
9+
"..",
10+
"..",
11+
"..",
12+
);
13+
14+
// The playground-bundling root dir
15+
export const playgroundDir = path.join(import.meta.dirname, "..");
16+
17+
// Final target output directory where all the cmijs will be stored
18+
export const playgroundPackagesDir = path.join(playgroundDir, "packages");
19+
20+
/**
21+
* @param {string} cmd
22+
*/
23+
export function exec(cmd) {
24+
console.log(`>>>>>> running command: ${cmd}`);
25+
child_process.execSync(cmd, {
26+
cwd: playgroundDir,
27+
encoding: "utf8",
28+
stdio: "inherit",
29+
});
30+
console.log("<<<<<<");
31+
}

packages/playground/scripts/generate_cmijs.mjs

Lines changed: 31 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,92 +13,54 @@
1313
* playground bundle.
1414
*/
1515

16-
import * as child_process from "node:child_process";
1716
import * as fs from "node:fs";
1817
import * as path from "node:path";
1918

2019
import resConfig from "../rescript.json" with { type: "json" };
20+
import {
21+
exec,
22+
compilerRootDir,
23+
playgroundPackagesDir,
24+
} from "./common.mjs";
2125

22-
const RESCRIPT_COMPILER_ROOT_DIR = path.join(
23-
import.meta.dirname,
24-
"..",
25-
"..",
26-
"..",
27-
);
26+
exec("yarn rescript clean");
27+
exec("yarn rescript");
2828

29-
// The playground-bundling root dir
30-
const PLAYGROUND_DIR = path.join(import.meta.dirname, "..");
31-
32-
// Final target output directory where all the cmijs will be stored
33-
const PACKAGES_DIR = path.join(PLAYGROUND_DIR, "packages");
29+
// We need to build the compiler's builtin modules as a separate cmij.
30+
// Otherwise we can't use them for compilation within the playground.
31+
buildCmij(compilerRootDir, "compiler-builtins");
3432

35-
// Making sure this directory exists, since it's not checked in to git
36-
fs.mkdirSync(PACKAGES_DIR, { recursive: true });
33+
const packages = resConfig["bs-dependencies"];
34+
for (const pkgName of packages) {
35+
buildCmij(
36+
path.join(compilerRootDir, "node_modules", pkgName),
37+
pkgName,
38+
);
39+
}
3740

3841
/**
39-
* @param {string} cmd
42+
* @param {string} pkgDir
43+
* @param {string} pkgName
4044
*/
41-
function e(cmd) {
42-
console.log(`>>>>>> running command: ${cmd}`);
43-
child_process.execSync(cmd, {
44-
cwd: PLAYGROUND_DIR,
45-
encoding: "utf8",
46-
stdio: [0, 1, 2],
47-
});
48-
console.log("<<<<<<");
49-
}
50-
51-
e("yarn rescript clean");
52-
e("yarn rescript");
53-
54-
const packages = resConfig["bs-dependencies"];
55-
56-
// We need to build the compiler's builtin modules as a separate cmij.
57-
// Otherwise we can't use them for compilation within the playground.
58-
function buildCompilerCmij() {
59-
const rescriptLibOcamlFolder = path.join(
60-
RESCRIPT_COMPILER_ROOT_DIR,
45+
function buildCmij(pkgDir, pkgName) {
46+
const libOcamlFolder = path.join(
47+
pkgDir,
6148
"lib",
6249
"ocaml",
6350
);
6451

65-
const outputFolder = path.join(PACKAGES_DIR, "compiler-builtins");
52+
const outputFolder = path.join(playgroundPackagesDir, pkgName);
6653
fs.mkdirSync(outputFolder, { recursive: true });
6754

6855
const cmijFile = path.join(outputFolder, "cmij.js");
69-
70-
e(
71-
`find ${rescriptLibOcamlFolder} -name "*.cmi" -or -name "*.cmj" | xargs -n1 basename | xargs js_of_ocaml build-fs -o ${cmijFile} -I ${rescriptLibOcamlFolder}`,
72-
);
56+
const inputFiles = fs.readdirSync(libOcamlFolder).filter(isCmij).join(" ");
57+
exec(`js_of_ocaml build-fs -o ${cmijFile} -I ${libOcamlFolder} ${inputFiles}`);
7358
}
7459

75-
function buildThirdPartyCmijs() {
76-
for (const pkg of packages) {
77-
const libOcamlFolder = path.join(
78-
RESCRIPT_COMPILER_ROOT_DIR,
79-
"node_modules",
80-
pkg,
81-
"lib",
82-
"ocaml",
83-
);
84-
const libEs6Folder = path.join(
85-
RESCRIPT_COMPILER_ROOT_DIR,
86-
"node_modules",
87-
pkg,
88-
"lib",
89-
"es6",
90-
);
91-
const outputFolder = path.join(PACKAGES_DIR, pkg);
92-
fs.mkdirSync(outputFolder, { recursive: true });
93-
94-
const cmijFile = path.join(outputFolder, "cmij.js");
95-
96-
e(`find ${libEs6Folder} -name '*.js' -exec cp {} ${outputFolder} \\;`);
97-
e(
98-
`find ${libOcamlFolder} -name "*.cmi" -or -name "*.cmj" | xargs -n1 basename | xargs js_of_ocaml build-fs -o ${cmijFile} -I ${libOcamlFolder}`,
99-
);
100-
}
60+
/**
61+
* @param {string} basename
62+
* @return {boolean}
63+
*/
64+
function isCmij(basename) {
65+
return /\.cm(i|j)$/.test(basename);
10166
}
102-
103-
buildCompilerCmij();
104-
buildThirdPartyCmijs();

0 commit comments

Comments
 (0)