import { denoPlugin, esbuild, toFileUrl } from "./deps.ts"; import { Island } from "./types.ts"; let esbuildInitalized: boolean | Promise = false; async function ensureEsbuildInialized() { if (esbuildInitalized === false) { if (Deno.run === undefined) { esbuildInitalized = esbuild.initialize({ wasmURL: "https://unpkg.com/esbuild-wasm@0.11.19/esbuild.wasm", worker: false, }); } else { esbuild.initialize({}); } await esbuildInitalized; esbuildInitalized = true; } else if (esbuildInitalized instanceof Promise) { await esbuildInitalized; } } export class Bundler { #islands: Island[]; #cache: Map | Promise | undefined = undefined; constructor(islands: Island[]) { this.#islands = islands; } async bundle() { const entryPoints: Record = { "main": new URL("../../src/runtime/main.ts", import.meta.url).href, }; for (const island of this.#islands) { entryPoints[`island-${island.id}`] = island.url; } const absWorkingDir = Deno.cwd(); await ensureEsbuildInialized(); const bundle = await esbuild.build({ bundle: true, entryPoints, format: "esm", metafile: true, minify: true, outdir: ".", // This is requried to ensure the format of the outputFiles path is the same // between windows and linux absWorkingDir, outfile: "", platform: "neutral", plugins: [denoPlugin()], splitting: true, target: ["chrome90", "firefox88", "safari13"], treeShaking: true, write: false, }); // 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(); const absDirUrlLength = toFileUrl(absWorkingDir).href.length; for (const file of bundle.outputFiles) { cache.set( toFileUrl(file.path).href.substring(absDirUrlLength), file.contents, ); } this.#cache = cache; return; } async cache(): Promise> { if (this.#cache === undefined) { this.#cache = this.bundle(); } if (this.#cache instanceof Promise) { await this.#cache; } return this.#cache as Map; } async get(path: string): Promise { const cache = await this.cache(); return cache.get(path) ?? null; } // getPreloads(path: string): string[] { // return this.#preloads.get(path) ?? []; // } }