|
13 | 13 | * playground bundle. |
14 | 14 | */ |
15 | 15 |
|
16 | | -import * as child_process from "node:child_process"; |
17 | 16 | import * as fs from "node:fs"; |
18 | 17 | import * as path from "node:path"; |
19 | 18 |
|
20 | 19 | import resConfig from "../rescript.json" with { type: "json" }; |
| 20 | +import { |
| 21 | + exec, |
| 22 | + compilerRootDir, |
| 23 | + playgroundPackagesDir, |
| 24 | +} from "./common.mjs"; |
21 | 25 |
|
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"); |
28 | 28 |
|
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"); |
34 | 32 |
|
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 | +} |
37 | 40 |
|
38 | 41 | /** |
39 | | - * @param {string} cmd |
| 42 | + * @param {string} pkgDir |
| 43 | + * @param {string} pkgName |
40 | 44 | */ |
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, |
61 | 48 | "lib", |
62 | 49 | "ocaml", |
63 | 50 | ); |
64 | 51 |
|
65 | | - const outputFolder = path.join(PACKAGES_DIR, "compiler-builtins"); |
| 52 | + const outputFolder = path.join(playgroundPackagesDir, pkgName); |
66 | 53 | fs.mkdirSync(outputFolder, { recursive: true }); |
67 | 54 |
|
68 | 55 | 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}`); |
73 | 58 | } |
74 | 59 |
|
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); |
101 | 66 | } |
102 | | - |
103 | | -buildCompilerCmij(); |
104 | | -buildThirdPartyCmijs(); |
0 commit comments