Page MenuHomePhorge

assets.ts
No OneTemporary

Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None

assets.ts

import type { VFile } from "vfile";
import { from, type Observable } from "rxjs";
import { read } from "to-vfile";
import { fromFileUrl, join, relative } from "@std/path";
import postcss from "postcss";
import cssnano from "cssnano";
import postcssImport from "postcss-import";
import { create3 } from "xxhash64";
import postcssAssets from "#/postcss-assets.ts";
const PRODUCTION = Boolean(Deno.env.get("BLOG_PRODUCTION"));
const assetCache = new Map<string, VFile>();
const css = postcss([postcssImport, postcssAssets, cssnano]);
export async function asset(path: string): Promise<string> {
const fullPath = fromFileUrl(import.meta.resolve(path));
if (assetCache.has(fullPath)) {
return "/" + relative("dist", assetCache.get(fullPath)!.path);
}
const file = await read(fullPath);
if (file.extname === ".css") {
const res = await css.process(file.value, { from: file.path });
file.value = res.css;
}
const hasher = await create3();
hasher.update(PRODUCTION ? file.value : fullPath);
const hash = hasher.digest("bigint").toString(36);
file.cwd = Deno.cwd();
file.path = join(
"dist",
"assets",
[file.stem!, ".", hash, file.extname!].join(""),
);
assetCache.set(fullPath, file);
return "/" + relative("dist", assetCache.get(fullPath)!.path);
}
export function all(): Observable<VFile> {
return from(assetCache.values());
}

File Metadata

Mime Type
application/javascript
Expires
Sun, Oct 25, 04:27 (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
57
Default Alt Text
assets.ts (1 KB)

Event Timeline