1export function virtualFiles(
2 map: Record<
3 string,
4 | string
5 | esbuild.OnLoadResult
6 | (() => Awaitable<string | esbuild.OnLoadResult>)
7 >,
8) {
9 return {
10 name: "clover vfs",
11 setup(b) {
12 b.onResolve(
13 {
14 filter: new RegExp(
15 `^(?:${
16 Object.keys(map).map((file) => string.escapeRegExp(file)).join(
17 "|",
18 )
19 })\$`,
20 ),
21 },
22 ({ path }) => ({ path, namespace: "vfs" }),
23 );
24 b.onLoad(
25 { filter: /./, namespace: "vfs" },
26 async ({ path }) => {
27 let entry = map[path];
28 if (typeof entry === "function") entry = await entry();
29 return ({
30 resolveDir: ".",
31 loader: "ts",
32 ...typeof entry === "string" ? { contents: entry } : entry,
33 });
34 },
35 );
36 },
37 } satisfies esbuild.Plugin;
38}
39
40import * as string from "@clo/lib/string";
41import type { Awaitable } from "@clo/lib/ts";
42import * as esbuild from "esbuild";