Public
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Allows Observable notebooks to statically import ES modules as Runtime modules.

See https://observablehq.com/d/1ff60814f0002dd5 for details and examples.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
export const observable_esm = async (request: Request): Promise<Response> => {
const url = new URL(request.url);
url.protocol = "https";
url.hostname = "esm.run";
const body = `
import * as module from ${JSON.stringify(url)};
export default function define(runtime, observer) {
const main = runtime.module();
main.variable(observer("__module")).define("__module", () => module);
for(const key of Object.keys(module)) {
const alias = key === "default" ? "__" + key : key;
main.variable(observer(alias)).define(alias, () => module[key]);
}
return main;
}
`;
// Increase caching duration if we have a version specifier.
const maxAge = 60 * 60 * (url.pathname.slice(2).includes("@") ? 24 : 1);
return new Response(body, {
headers: {
"Content-Type": "application/javascript",
"Cache-Control": `public, max-age=${maxAge}`,
},
});
};
mootari-observable_esm.web.val.run
March 16, 2024