Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix, tested
  • Loading branch information
cometkim committed Sep 3, 2025
commit 5a94c29b4b70b9c237a4bb58c2e46d06bd53b33b
43 changes: 18 additions & 25 deletions packages/playground/serve-bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,34 @@ import { stat, readFile } from "node:fs/promises";
import * as path from "node:path";
import { H3, serve, serveStatic } from "h3";

import { rescript_compiler } from "./compiler.js";

const compilerVersion = rescript_compiler.version;
const localVersion = `${compilerVersion}-local`;
import compilerBundle from "./compiler.js";

const compilerVersion = compilerBundle.rescript_compiler.version;
const localVersion = "v" + compilerVersion.toString();
/**
* @param {string} id
*/
function toLocalPath(id) {
if (id === "/compiler.js") {
return path.join(import.meta.dirname, id);
}
if (id.startsWith(`/${localVersion}`)) {
return path.join(
import.meta.dirname,
id.replace(`/${localVersion}`, "/packages"),
);
const originalId = id.slice(localVersion.length + 1);
if (originalId === "/compiler.js") {
return path.join(import.meta.dirname, originalId);
}
return undefined;
return path.join(
import.meta.dirname,
"packages",
originalId,
);
}

const app = new H3()
.get("/versions.json", () => {
return [localVersion];
})
// Supported paths
// - /compiler.js
// - /{compilerVersion}-local/{library}/cmij.js
.use("**", event => {
const versionContent = new H3()
.get("/**", event => {
return serveStatic(event, {
getContents: id => {
const localPath = toLocalPath(id);
return localPath && readFile(path.join(basePath, id));
return localPath && readFile(localPath);
},
getMeta: async id => {
const localPath = toLocalPath(id);
if (!localPath) {
return undefined;
}
const stats = await stat(localPath).catch(() => {});
if (stats?.isFile()) {
return {
Expand All @@ -52,4 +41,8 @@ const app = new H3()
});
});

const app = new H3()
.get("/playground-bundles/versions.json", () => [localVersion])
.mount(`/${localVersion}`, versionContent);

serve(app, { port: 8888 });
Loading