it was weird. i pressed delete on a subfolder, i think one of the pages.off folders that i was using. and then, suddenly, nvim on windows 7 decided to delete every file in the directory. they weren't shred off the space time continuum, but just marked deleted. i had to pay $80 to get access to a software that could see them. bleh! just seeing all my work, a little over a week, was pretty heart shattering. but i remembered that long ago, a close friend said i could call them whenever i was feeling sad. i finally took them up on that offer. the first time i've ever called someone for emotional support. but it's ok. i got it back. and the site framework is better than ever. i'm gonna commit and push more often. the repo is private anyways.
85 lines
2.4 KiB
TypeScript
85 lines
2.4 KiB
TypeScript
// This file implements client-side bundling, mostly wrapping esbuild.
|
|
import process from "node:process";
|
|
const plugins: esbuild.Plugin[] = [
|
|
// There are currently no plugins needed by 'paperclover.net'
|
|
];
|
|
|
|
export async function bundleClientJavaScript(
|
|
referencedScripts: string[],
|
|
extraPublicScripts: string[],
|
|
incr: Incremental,
|
|
dev: boolean = false,
|
|
) {
|
|
const entryPoints = [
|
|
...new Set([
|
|
...referencedScripts,
|
|
...extraPublicScripts,
|
|
]),
|
|
];
|
|
if (entryPoints.length === 0) return;
|
|
const invalidFiles = entryPoints
|
|
.filter((file) => !file.match(/\.client\.[tj]sx?/));
|
|
if (invalidFiles.length > 0) {
|
|
const cwd = process.cwd();
|
|
throw new Error(
|
|
"All client-side scripts should be named like '.client.ts'. Exceptions: " +
|
|
invalidFiles.map((x) => path.join(cwd, x)).join(","),
|
|
);
|
|
}
|
|
|
|
const bundle = await esbuild.build({
|
|
bundle: true,
|
|
chunkNames: "/js/c.[hash]",
|
|
entryNames: "/js/[name]",
|
|
assetNames: "/asset/[hash]",
|
|
entryPoints,
|
|
format: "esm",
|
|
minify: !dev,
|
|
outdir: "/out!",
|
|
plugins,
|
|
splitting: true,
|
|
write: false,
|
|
});
|
|
if (bundle.errors.length || bundle.warnings.length) {
|
|
throw new AggregateError(
|
|
bundle.errors.concat(bundle.warnings),
|
|
"JS bundle failed",
|
|
);
|
|
}
|
|
incr.invalidate("bundle-script");
|
|
const publicScriptRoutes = extraPublicScripts.map((file) =>
|
|
path.basename(file).replace(/\.client\.[tj]sx?/, "")
|
|
);
|
|
const promises: Promise<unknown>[] = [];
|
|
// TODO: add a shared build hash to entrypoints, derived from all the chunk hashes.
|
|
for (const file of bundle.outputFiles) {
|
|
let route = file.path.replace(/^.*!/, "").replaceAll("\\", "/");
|
|
const text = file.text;
|
|
// Register non-chunks as script entries.
|
|
const chunk = route.startsWith("/js/c.");
|
|
if (!chunk) {
|
|
route = route.replace(".client.js", ".js");
|
|
incr.put({
|
|
srcId: "bundle-script",
|
|
type: "script",
|
|
key: route.slice("/js/".length, -".js".length),
|
|
value: text,
|
|
});
|
|
}
|
|
if (chunk || publicScriptRoutes.includes(route)) {
|
|
promises.push(incr.putAsset({
|
|
srcId: "bundle-script",
|
|
key: route,
|
|
body: text,
|
|
}));
|
|
}
|
|
}
|
|
|
|
if (promises.length > 0) {
|
|
await Promise.all(promises);
|
|
}
|
|
}
|
|
|
|
import * as path from "node:path";
|
|
import * as esbuild from "esbuild";
|
|
import { Incremental } from "./incremental.ts";
|