perf: add loading optimization with modulepreload (#15)

This commit aims to speed up loading of chunked modules using
the link rel=modulepreload directive.
This commit is contained in:
Luca Casonato
2021-06-02 12:55:30 +02:00
committed by GitHub
parent 26189cfdf3
commit 41307a5ac8
3 changed files with 54 additions and 10 deletions
+16
View File
@@ -18,6 +18,7 @@ async function ensureEsbuildInialized() {
export class Bundler {
#pages: Page[];
#cache: Map<string, Uint8Array> | Promise<void> | undefined = undefined;
#preloads = new Map<string, string[]>();
constructor(pages: Page[]) {
this.#pages = pages;
@@ -42,10 +43,21 @@ export class Bundler {
outfile: "",
jsxFactory: "h",
jsxFragment: "Fragment",
metafile: true,
outdir: `/`,
entryPoints,
});
const metafileOutputs = bundle.metafile!.outputs;
for (const path in metafileOutputs) {
const meta = metafileOutputs[path];
const imports = meta.imports
.filter(({ kind }) => kind === "import-statement")
.map(({ path }) => `/${path}`);
this.#preloads.set(`/${path}`, imports);
}
const cache = new Map<string, Uint8Array>();
for (const file of bundle.outputFiles) {
cache.set(file.path, file.contents);
@@ -69,6 +81,10 @@ export class Bundler {
const cache = await this.cache();
return cache.get(path) ?? null;
}
getPreloads(path: string): string[] {
return this.#preloads.get(path) ?? [];
}
}
function freshPlugin(pageList: Page[]): esbuild.Plugin {