Skip to content

Commit 37f9f79

Browse files
authored
Run the java-web command through a launcher that turns on Node's compile cache (#33)
1 parent be8af85 commit 37f9f79

6 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Node, which is how the tests run.
3131
(static String calls), `cheerpOSAddStringFile`, `cjFileBlob`, `/files/` in
3232
IndexedDB and one VM per about:blank iframe. Keep these working.
3333
- The release form is an npm package of the built files only
34-
(`java-web.js`, `java-web-worker.js`, `node.js` (the Node entry and the
34+
(`java-web.js`, `java-web-worker.js`, `node.js` (the Node entry), `cli.js` (the
3535
`java-web` command), `boot.pack`, `jdk/<module>.jar`,
3636
`jdk/javac.pack`, `jdk/jdk.localedata.txt`, and OpenJDK's notices in `jdk/legal/`), versioned, with
3737
nothing built at install. A GitHub Actions workflow triggered by a release

‎docs/browser.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Using java-web in a page
22

33
The npm package holds the built files: `java-web.js`, `java-web-worker.js`,
4-
`boot.pack` and `jdk/` for pages, and `node.js` for Node ([node.md](node.md)). Serve `node_modules/@project516/java-web/dist/` as static files
4+
`boot.pack` and `jdk/` for pages, and `node.js` and `cli.js` (the `java-web` command) for Node ([node.md](node.md)). Serve `node_modules/@project516/java-web/dist/` as static files
55
and load `java-web.js` with a classic `<script>` tag. It finds the other files
66
next to itself, or where `cheerpjInit({ runtime: "/path/to/dist/" })` says.
77

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
"./dist/*": "./dist/*"
2727
},
2828
"bin": {
29-
"java-web": "dist/node.js"
29+
"java-web": "dist/cli.js"
3030
},
3131
"files": [
3232
"dist/java-web.js",
3333
"dist/java-web-worker.js",
3434
"dist/node.js",
35+
"dist/cli.js",
3536
"dist/node.d.ts",
3637
"dist/boot.pack",
3738
"dist/jdk/*.jar",

‎scripts/build-web.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ await build({
5555
define,
5656
});
5757
cpSync(join(root, "types", "node.d.ts"), join(dist, "node.d.ts"));
58+
// The java-web command. Node's compile cache covers only modules loaded after
59+
// it is on, so it comes before the VM: later runs skip compiling node.js.
60+
writeFileSync(join(dist, "cli.js"), `#!/usr/bin/env node
61+
import { enableCompileCache } from "node:module";
62+
enableCompileCache?.();
63+
const { main } = await import("./node.js");
64+
main(process.argv.slice(2));
65+
`, { mode: 0o755 });
5866

5967
mkdirSync(join(dist, "jdk"), { recursive: true });
6068

‎src/node.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ function heapSize(s: string): number {
440440
return unit ? Number(s.slice(0, -1)) * unit : Number(s);
441441
}
442442

443-
function main(argv: string[]) {
443+
// the java-web command, which dist/cli.js runs
444+
export function main(argv: string[]) {
444445
const o: NodeRunOptions = { classpath: ["."], props: {} };
445446
let i = 0;
446447
for (; i < argv.length; i++) {

‎test/image.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ const node = join(root, "dist", "node.js");
9696
const skip = existsSync(node) ? false : "run `pnpm build` first";
9797
const out = join(root, "build", "test-image");
9898

99-
test("dist/node.js runs a program as HotSpot does", { skip }, () => {
99+
test("dist/node.js and the java-web command run a program as HotSpot does", { skip }, () => {
100100
mkdirSync(out, { recursive: true });
101101
spawnSync(hotspot("javac"), ["-d", out, join(root, "test", "java", "Collections.java")], { stdio: "inherit" });
102102
const ref = spawnSync(hotspot("java"), ["-cp", out, "Collections"], { encoding: "utf8" });
103-
const got = spawnSync(process.execPath, [node, "-cp", out, "Collections"], { encoding: "utf8" });
104-
assert.deepEqual({ stdout: got.stdout, stderr: got.stderr, code: got.status }, { stdout: ref.stdout, stderr: ref.stderr, code: ref.status });
103+
for (const entry of [node, join(root, "dist", "cli.js")]) {
104+
const got = spawnSync(process.execPath, [entry, "-cp", out, "Collections"], { encoding: "utf8" });
105+
assert.deepEqual({ stdout: got.stdout, stderr: got.stderr, code: got.status }, { stdout: ref.stdout, stderr: ref.stderr, code: ref.status }, entry);
106+
}
105107
});
106108

107109
test("dist/node.js exports runNode, and javac runs on the packaged image", { skip }, async () => {

0 commit comments

Comments
 (0)