28 lines
819 B
TypeScript
28 lines
819 B
TypeScript
|
import { globSync } from "glob";
|
||
|
import { readFileSync } from "node:fs";
|
||
|
import { env } from "node:process";
|
||
|
import { join } from "node:path";
|
||
|
|
||
|
export const assetMap: Map<string, string> = new Map();
|
||
|
|
||
|
export function reloadAssets() {
|
||
|
assetMap.clear();
|
||
|
|
||
|
globSync("dist/_assets/*.manifest")
|
||
|
.map(
|
||
|
(manifest) =>
|
||
|
JSON.parse(readFileSync(manifest).toString()) as { [orig: string]: string },
|
||
|
)
|
||
|
.forEach((mapping) =>
|
||
|
Object.entries(mapping).forEach(([orig, hashed]) => assetMap.set(orig, hashed)),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function asset(path: string): string {
|
||
|
const realPath = assetMap.has(path) ? assetMap.get(path) : path;
|
||
|
|
||
|
return env.ASSETS_HOSTS
|
||
|
? new URL(realPath, env.ASSETS_HOSTS).toString()
|
||
|
: join("/_assets/", realPath);
|
||
|
}
|