#!/usr/bin/env node // Builds the libraries a script may `require` (P3.5): each becomes one // self-contained script, kept as text, which the script engine evaluates // inside its own VM on the first `require`. The browser never runs it. // // Output: packages/core/src/scripting/vm-libs/.ts, a module whose // default export is that text, and licenses.txt beside them with the licence // of every package that went into one. The folder is not committed: `npm ci` // runs this (the "prepare" script). // // Each text is a function expression, `(function (module, exports, self, // crypto, EventTarget, Event) { ... return module.exports; })`. The VM's // `require` (vm-bootstrap.ts) gives the arguments: `self` and `crypto` hold // a random source and nothing else (crypto-js and uuid look for one there), // and the two event classes are stand-ins that do nothing (chai announces // its plugins through them). import { build } from "esbuild"; import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; import { dirname, join } from "node:path"; import { pathToFileURL } from "node:url"; /** The name a script requires, and the file of the package that is bundled. */ export const VM_LIBRARIES = { chai: "chai", "crypto-js": "crypto-js", lodash: "lodash", // The file without the locales, which is one script already. It is not // bundled: it loads a locale by a computed path, inside a `try`, and in the // VM that `require` refuses. moment: "node_modules/moment/min/moment.min.js", uuid: "uuid", }; const OUT = "packages/core/src/scripting/vm-libs"; /** The package a bundled file belongs to: the folder that follows the last `node_modules`. */ function packageOf(file) { const parts = file.split("/"); const at = parts.lastIndexOf("node_modules"); if (at === -1) return undefined; return parts.slice(0, at + (parts[at + 1].startsWith("@") ? 3 : 2)).join("/"); } export async function buildLibrary(entry) { const result = await build({ entryPoints: [entry], bundle: !entry.endsWith(".js"), format: "cjs", platform: "browser", target: "es2020", minify: true, legalComments: "none", write: false, metafile: true, logLevel: "silent", }); const packages = new Set(Object.keys(result.metafile.inputs).map(packageOf).filter(Boolean)); return { text: `(function (module, exports, self, crypto, EventTarget, Event) {\n${result.outputFiles[0].text}\nreturn module.exports;\n})`, packages: [...packages], }; } function licenceOf(folder) { const { name, version, license } = JSON.parse(readFileSync(join(folder, "package.json"), "utf8")); const file = readdirSync(folder).find((entry) => /^licen[cs]e/i.test(entry)); if (!file) throw new Error(`build-vm-libs: ${name} has no licence file`); return `${"-".repeat(80)}\nRun inside the script engine: ${name}@${version} (${license})\n\n${readFileSync(join(folder, file), "utf8")}`; } if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) { mkdirSync(OUT, { recursive: true }); const bundled = new Set(); const sizes = []; for (const [name, entry] of Object.entries(VM_LIBRARIES)) { const { text, packages } = await buildLibrary(entry); packages.forEach((folder) => bundled.add(folder)); writeFileSync( join(OUT, `${name}.ts`), `// Generated by scripts/build-vm-libs.mjs. Not committed.\nexport default ${JSON.stringify(text)};\n`, ); sizes.push(`${name} ${text.length}`); } writeFileSync(join(OUT, "licenses.txt"), [...bundled].sort().map(licenceOf).join("\n")); if (!existsSync(join(dirname(OUT), "host.ts"))) throw new Error(`build-vm-libs: run from the repository root`); console.log(`build-vm-libs: ${sizes.join(", ")} characters; ${bundled.size} packages`); }