79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
export function virtualFiles(
|
|
map: Record<string, string | esbuild.OnLoadResult>,
|
|
) {
|
|
return {
|
|
name: "clover vfs",
|
|
setup(b) {
|
|
b.onResolve(
|
|
{
|
|
filter: new RegExp(
|
|
`^(?:${
|
|
Object.keys(map).map((file) => string.escapeRegExp(file)).join(
|
|
"|",
|
|
)
|
|
})\$`,
|
|
),
|
|
},
|
|
({ path }) => ({ path, namespace: "vfs" }),
|
|
);
|
|
b.onLoad(
|
|
{ filter: /./, namespace: "vfs" },
|
|
({ path }) => {
|
|
const entry = map[path];
|
|
return ({
|
|
resolveDir: ".",
|
|
loader: "ts",
|
|
...typeof entry === "string" ? { contents: entry } : entry,
|
|
});
|
|
},
|
|
);
|
|
},
|
|
} satisfies esbuild.Plugin;
|
|
}
|
|
|
|
export function banFiles(
|
|
files: string[],
|
|
) {
|
|
return {
|
|
name: "clover vfs",
|
|
setup(b) {
|
|
b.onResolve(
|
|
{
|
|
filter: new RegExp(
|
|
`^(?:${
|
|
files.map((file) => string.escapeRegExp(file)).join("|")
|
|
})\$`,
|
|
),
|
|
},
|
|
({ path, importer }) => {
|
|
throw new Error(
|
|
`Loading ${path} (from ${importer}) is banned!`,
|
|
);
|
|
},
|
|
);
|
|
},
|
|
} satisfies esbuild.Plugin;
|
|
}
|
|
|
|
export function projectRelativeResolution(root = process.cwd() + "/src") {
|
|
return {
|
|
name: "project relative resolution ('@/' prefix)",
|
|
setup(b) {
|
|
b.onResolve({ filter: /^@\// }, ({ path: id }) => {
|
|
return {
|
|
path: path.resolve(root, id.slice(2)),
|
|
};
|
|
});
|
|
b.onResolve({ filter: /^#/ }, ({ path: id, importer }) => {
|
|
return {
|
|
path: hot.resolveFrom(importer, id),
|
|
};
|
|
});
|
|
},
|
|
} satisfies esbuild.Plugin;
|
|
}
|
|
|
|
import * as esbuild from "esbuild";
|
|
import * as string from "#sitegen/string";
|
|
import * as path from "node:path";
|
|
import * as hot from "./hot.ts";
|