sitegen/framework/bundle.ts

291 lines
9.3 KiB
TypeScript
Raw Normal View History

2025-08-01 20:16:21 -07:00
async function trackEsbuild(io: Io, metafile: esbuild.Metafile) {
await Promise.all(Object.keys(metafile.inputs)
.filter(file => !isIgnoredSource(file))
.map(file => io.trackFile(file)));
}
// This file implements client-side bundling, mostly wrapping esbuild.
export async function bundleClientJavaScript(
2025-07-31 21:35:36 -07:00
io: Io,
{ clientRefs, extraPublicScripts, dev = false }: {
2025-08-01 20:16:21 -07:00
clientRefs: string[];
extraPublicScripts: string[];
dev: boolean;
},
) {
const entryPoints = [
...new Set([
2025-08-01 20:16:21 -07:00
...clientRefs.map((x) => `src/${x}`),
...extraPublicScripts,
2025-07-31 21:35:36 -07:00
].map(toAbs)),
];
2025-07-31 21:35:36 -07:00
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: \n" +
invalidFiles.map((x) => path.join(cwd, x)).join("\n"),
);
}
2025-06-13 00:13:22 -07:00
const clientPlugins: esbuild.Plugin[] = [
2025-06-15 13:11:21 -07:00
projectRelativeResolution(),
2025-07-31 21:35:36 -07:00
markoViaBuildCache(),
2025-06-13 00:13:22 -07:00
];
const bundle = await esbuild.build({
2025-06-21 16:04:57 -07:00
assetNames: "/asset/[hash]",
bundle: true,
chunkNames: "/js/c.[hash]",
entryNames: "/js/[name]",
entryPoints,
format: "esm",
2025-06-21 16:04:57 -07:00
jsx: "automatic",
jsxDev: dev,
jsxImportSource: "#ssr",
logLevel: "silent",
metafile: true,
minify: !dev,
2025-07-07 09:42:04 -07:00
outdir: "out!",
2025-06-09 21:13:51 -07:00
plugins: clientPlugins,
write: false,
2025-06-15 13:11:21 -07:00
define: {
"ASSERT": "console.assert",
2025-06-21 16:04:57 -07:00
MIME_INLINE_DATA: JSON.stringify(mime.rawEntriesText),
2025-06-15 13:11:21 -07:00
},
2025-06-21 16:04:57 -07:00
}).catch((err: any) => {
err.message = `Client ${err.message}`;
throw err;
});
if (bundle.errors.length || bundle.warnings.length) {
throw new AggregateError(
bundle.errors.concat(bundle.warnings),
"JS bundle failed",
);
}
const publicScriptRoutes = extraPublicScripts.map((file) =>
2025-06-15 13:11:21 -07:00
"/js/" +
path.relative(hot.projectSrc, file).replaceAll("\\", "/").replace(
/\.client\.[tj]sx?/,
".js",
)
);
2025-06-15 13:11:21 -07:00
const { metafile, outputFiles } = bundle;
2025-08-01 20:16:21 -07:00
const p = []
p.push(trackEsbuild(io, metafile));
2025-07-31 21:35:36 -07:00
const scripts: Record<string, string> = {};
2025-06-15 13:11:21 -07:00
for (const file of outputFiles) {
2025-06-09 00:12:41 -07:00
const { text } = file;
let route = file.path.replace(/^.*!/, "").replaceAll("\\", "/");
2025-06-09 00:12:41 -07:00
const { inputs } = UNWRAP(metafile.outputs["out!" + route]);
2025-08-01 20:16:21 -07:00
const sources = Object.keys(inputs).filter((x) => !isIgnoredSource(x));
2025-06-09 00:12:41 -07:00
// Register non-chunks as script entries.
const chunk = route.startsWith("/js/c.");
if (!chunk) {
2025-06-15 13:11:21 -07:00
const key = hot.getScriptId(path.resolve(sources[sources.length - 1]));
route = "/js/" + key.replace(/\.client\.tsx?/, ".js");
2025-07-31 21:35:36 -07:00
scripts[key] = text;
}
2025-06-09 00:12:41 -07:00
// Register chunks and public scripts as assets.
if (chunk || publicScriptRoutes.includes(route)) {
2025-08-01 20:16:21 -07:00
p.push(io.writeAsset(route, text));
}
}
2025-08-01 20:16:21 -07:00
await Promise.all(p);
2025-07-31 21:35:36 -07:00
return scripts;
2025-06-08 15:12:04 -07:00
}
2025-06-13 00:13:22 -07:00
export type ServerPlatform = "node" | "passthru";
2025-08-01 20:16:21 -07:00
export interface ServerSideOptions {
entries: string[],
viewItems: sg.FileItem[]
viewRefs: incr.Ref<PreparedView>[],
styleMap: Map<string, incr.Ref<string>>;
scriptMap: incr.Ref<Record<string, string>>;
platform: ServerPlatform,
}
2025-06-09 21:13:51 -07:00
export async function bundleServerJavaScript(
2025-08-01 20:16:21 -07:00
{ viewItems, viewRefs, styleMap, scriptMap: wScriptMap, entries, platform }: ServerSideOptions
2025-06-09 21:13:51 -07:00
) {
2025-08-01 20:16:21 -07:00
const wViewSource = incr.work(async (_, viewItems: sg.FileItem[]) => {
const magicWord = "C_" + crypto.randomUUID().replaceAll("-", "_");
return {
magicWord,
file: [
...viewItems.map((view, i) => `import * as view${i} from ${JSON.stringify(view.file)}`),
`const styles = ${magicWord}[-2]`,
`export const scripts = ${magicWord}[-1]`,
"export const views = {",
...viewItems.map((view, i) => [
` ${JSON.stringify(view.id)}: {`,
` component: view${i}.default,`,
` meta: view${i}.meta,`,
` layout: view${i}.layout?.default ?? null,`,
` inlineCss: styles[${magicWord}[${i}]]`,
` },`,
].join("\n")),
"}",
].join("\n")
};
}, viewItems)
2025-06-13 00:13:22 -07:00
2025-08-01 20:16:21 -07:00
const wBundles = entries.map(entry => [entry, incr.work(async (io, entry) => {
const pkg = await io.readJson<{ dependencies: Record<string, string>; }>("package.json");
2025-06-13 00:13:22 -07:00
2025-08-01 20:16:21 -07:00
let magicWord = null as string | null;
// -- plugins --
const serverPlugins: esbuild.Plugin[] = [
virtualFiles({
// only add dependency when imported.
"$views": async () => {
const view = await io.readWork(wViewSource);
({ magicWord } = view);
return view.file;
},
}),
projectRelativeResolution(),
markoViaBuildCache(),
{
name: "replace client references",
setup(b) {
b.onLoad({ filter: /\.tsx?$/ }, async ({ path: file }) => ({
contents:
hot.resolveClientRefs(await fs.readFile(file, "utf-8"), file).code,
loader: path.extname(file).slice(1) as esbuild.Loader,
}));
},
},
2025-08-01 20:16:21 -07:00
{
name: "mark css external",
setup(b) {
b.onResolve(
{ filter: /\.css$/ },
() => ({ path: ".", namespace: "dropped" }),
);
b.onLoad(
{ filter: /./, namespace: "dropped" },
() => ({ contents: "" }),
);
},
2025-06-10 01:13:59 -07:00
},
2025-08-01 20:16:21 -07:00
];
2025-06-13 00:13:22 -07:00
2025-08-01 20:16:21 -07:00
const { metafile, outputFiles } = await esbuild.build({
bundle: true,
chunkNames: "c.[hash]",
entryNames: path.basename(entry, path.extname(entry)),
entryPoints: [
path.join(import.meta.dirname, "backend/entry-" + platform + ".ts"),
],
platform: "node",
format: "esm",
minify: false,
outdir: "out!",
plugins: serverPlugins,
splitting: true,
logLevel: "silent",
write: false,
metafile: true,
jsx: "automatic",
jsxImportSource: "#ssr",
jsxDev: false,
define: {
MIME_INLINE_DATA: JSON.stringify(mime.rawEntriesText),
CLOVER_SERVER_ENTRY: JSON.stringify(entry),
},
external: Object.keys(pkg.dependencies)
.filter((x) => !x.startsWith("@paperclover")),
});
await trackEsbuild(io, metafile)
let fileWithMagicWord: {
bytes: Buffer;
basename: string;
magicWord: string;
} | null = null;
for (const output of outputFiles) {
const basename = output.path.replace(/^.*?!(?:\/|\\)/, "");
const key = "out!" + basename.replaceAll("\\", "/");
// If this contains the generated "$views" file, then
// mark this file as the one for replacement. Because
// `splitting` is `true`, esbuild will not emit this
// file in more than one chunk.
if (magicWord && metafile.outputs[key].inputs["framework/lib/view.ts"]) {
ASSERT(!fileWithMagicWord);
fileWithMagicWord = {
basename,
bytes: Buffer.from(output.contents),
magicWord,
};
} else {
io.writeFile(basename, Buffer.from(output.contents))
}
2025-06-11 00:17:58 -07:00
}
2025-08-01 20:16:21 -07:00
return fileWithMagicWord;
}, entry)] as const);
2025-06-10 01:13:59 -07:00
2025-08-01 20:16:21 -07:00
const wProcessed = wBundles.map(async([entry, wBundle]) => {
if (!await wBundle) return;
await incr.work(async (io) => {
// Only the reachable resources need to be read and inserted into the bundle.
// This is what Map<string, incr.Ref> is for
const { basename, bytes, magicWord } = UNWRAP(await io.readWork(wBundle));
const views = await Promise.all(viewRefs.map(ref => io.readWork(ref)));
2025-06-10 01:13:59 -07:00
2025-08-01 20:16:21 -07:00
// Client JS
const scriptList = Object.entries(await io.readWork(wScriptMap));
const viewScriptsList = new Set(views.flatMap(view => view.clientRefs));
const neededScripts = scriptList.filter(([k]) => viewScriptsList.has(k));
2025-06-13 00:13:22 -07:00
2025-08-01 20:16:21 -07:00
// CSS
const viewStyleKeys = views.map((view) => view.styleKey);
const viewCssBundles = await Promise.all(
viewStyleKeys.map((key) => io.readWork(UNWRAP(styleMap.get(key), "Style key: " + key))));
const styleList = Array.from(new Set(viewCssBundles));
2025-06-10 01:13:59 -07:00
2025-08-01 20:16:21 -07:00
// Replace the magic word
const text = bytes.toString("utf-8").replace(
new RegExp(magicWord + "\\[(-?\\d+)\\]", "gs"),
(_, i) => {
i = Number(i);
// Inline the styling data
if (i === -2) {
return JSON.stringify(styleList.map((cssText) => cssText));
}
// Inline the script data
if (i === -1) {
return JSON.stringify(Object.fromEntries(neededScripts));
}
// Reference an index into `styleList`
return `${styleList.indexOf(viewCssBundles[i])}`;
},
);
2025-06-10 01:13:59 -07:00
2025-08-01 20:16:21 -07:00
io.writeFile(basename, text);
});
})
2025-06-10 01:13:59 -07:00
2025-08-01 20:16:21 -07:00
await Promise.all(wProcessed);
}
2025-06-21 16:04:57 -07:00
import * as esbuild from "esbuild";
2025-06-08 12:38:25 -07:00
import * as path from "node:path";
import process from "node:process";
2025-06-09 21:13:51 -07:00
import * as hot from "./hot.ts";
2025-08-01 20:16:21 -07:00
import {
isIgnoredSource,
markoViaBuildCache,
projectRelativeResolution,
virtualFiles,
} from "./esbuild-support.ts";
2025-07-31 21:35:36 -07:00
import { Io, toAbs, toRel } from "./incremental.ts";
2025-06-10 01:13:59 -07:00
import * as css from "./css.ts";
import * as fs from "#sitegen/fs";
2025-06-21 16:04:57 -07:00
import * as mime from "#sitegen/mime";
2025-08-01 20:16:21 -07:00
import * as incr from "./incremental.ts";
import * as sg from "#sitegen";import type { PreparedView } from "./generate2.ts";import { meta } from "@/file-viewer/pages/file.cotyledon_speedbump.tsx";