chore: improve bundling + refactor router (#10)

This commit changes how bundling works, by bundling all routes at once.
Static JS assets are now cached immutably.

Additionally this PR completly refactors how the router works to make it
more modular. This allows users to opt out of the default oak server,
and instead create their own.
This commit is contained in:
Luca Casonato
2021-06-01 23:19:33 +02:00
committed by GitHub
parent 8a58fe7ae0
commit 31dbd99bea
19 changed files with 249 additions and 92 deletions
+92 -20
View File
@@ -1,10 +1,10 @@
import { denoPlugin, esbuild } from "./deps.ts";
import { Page } from "./routes.ts";
import { Page } from "./page.ts";
let esbuildInitalized: boolean | Promise<void> = false;
async function ensureEsbuildInialized() {
if (esbuildInitalized === false) {
esbuildInitalized = await esbuild.initialize({
esbuildInitalized = esbuild.initialize({
wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm",
worker: false,
});
@@ -15,9 +15,93 @@ async function ensureEsbuildInialized() {
}
}
export async function bundle(page: Page): Promise<string> {
export class Bundler {
#pages: Page[];
#cache: Map<string, Uint8Array> | Promise<void> | undefined = undefined;
constructor(pages: Page[]) {
this.#pages = pages;
}
async bundle() {
const entryPoints: Record<string, string> = {};
for (const page of this.#pages) {
entryPoints[page.name] = `fresh:///${page.name}`;
}
await ensureEsbuildInialized();
const bundle = await esbuild.build({
plugins: [freshPlugin(this.#pages), denoPlugin({ loader: "portable" })],
write: false,
bundle: true,
minify: true,
treeShaking: true,
splitting: true,
platform: "neutral",
outfile: "",
jsxFactory: "h",
jsxFragment: "Fragment",
outdir: `/`,
entryPoints,
});
const cache = new Map<string, Uint8Array>();
for (const file of bundle.outputFiles) {
cache.set(file.path, file.contents);
}
this.#cache = cache;
return;
}
async cache(): Promise<Map<string, Uint8Array>> {
if (this.#cache === undefined) {
this.#cache = this.bundle();
}
if (this.#cache instanceof Promise) {
await this.#cache;
}
return this.#cache as Map<string, Uint8Array>;
}
async get(path: string): Promise<Uint8Array | null> {
const cache = await this.cache();
return cache.get(path) ?? null;
}
}
function freshPlugin(pageList: Page[]): esbuild.Plugin {
const runtime = new URL("../../runtime.ts", import.meta.url);
const contents = `
const pageMap = new Map<string, Page>();
for (const page of pageList) {
pageMap.set(`/${page.name}`, page);
}
return {
name: "fresh",
setup(build) {
build.onResolve({ filter: /fresh:\/\/.*/ }, function onResolve(
args: esbuild.OnResolveArgs,
): esbuild.OnResolveResult | null | undefined {
return ({
path: args.path,
namespace: "fresh",
});
});
build.onLoad(
{ filter: /.*/, namespace: "fresh" },
function onLoad(
args: esbuild.OnLoadArgs,
): esbuild.OnLoadResult | null | undefined {
const url = new URL(args.path);
const path = url.pathname;
const page = pageMap.get(path);
if (!page) return null;
const contents = `
import Page from "${page.url}";
import { h, render } from "${runtime.href}";
@@ -26,21 +110,9 @@ addEventListener("DOMContentLoaded", () => {
render(h(Page, props), document.body);
});
`;
await ensureEsbuildInialized();
const bundle = await esbuild.build({
plugins: [denoPlugin({ loader: "portable" })],
write: false,
bundle: true,
minify: true,
treeShaking: true,
platform: "neutral",
outfile: "",
jsxFactory: "h",
jsxFragment: "Fragment",
stdin: {
contents,
sourcefile: `fresh://entrypoint/${page.name}`,
return { contents, loader: "js" };
},
);
},
});
return bundle.outputFiles[0].text;
};
}