Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403import type { BuildConfig, ESBUILD, LocalState } from "./types.ts";import type { FullPackageVersion, PackageJson } from "@bundle/utils/types";import type { Disposable, AsyncDisposable } from "@bundle/utils/dispose";
import { VirtualFileSystemPlugin } from "./plugins/fs.ts";import { ExternalPlugin } from "./plugins/external.ts";import { TarballPlugin } from "./plugins/tar.ts";import { PackagePlugin } from "./plugins/package.ts";import { AliasPlugin } from "./plugins/alias.ts";import { HttpPlugin } from "./plugins/http.ts";import { CdnPlugin } from "./plugins/cdn.ts";
import { Context, fromContext, toContext, withContext } from "./context/context.ts";
import { BUILD_ERROR, INIT_LOADING, LOGGER_ERROR, LOGGER_LOG, LOGGER_WARN, dispatchEvent } from "./configs/events.ts";import { createConfig } from "./configs/config.ts";import { PLATFORM_AUTO } from "./configs/platform.ts";
import { DEFAULT_CDN_HOST, getCDNUrl } from "./utils/cdn-format.ts";import { useFileSystem, type IFileSystem } from "./utils/filesystem.ts";import { createNotice } from "./utils/create-notice.ts";
import { init } from "./init.ts";import { bytes } from "@bundle/utils/fmt";
import { AsyncDisposableStack } from "@bundle/utils/dispose";
/** * Default build config */export const BUILD_CONFIG: BuildConfig = { "entryPoints": ["/index.tsx"], "cdn": DEFAULT_CDN_HOST, "polyfill": false,
"remapFalse": { "packageRemapFalse": "error", "importRemapFalse": "stub", "warnOnStubbedRemapFalse": true, },
"esbuild": { "color": true, "globalName": "BundledCode",
"logLevel": "info", "sourcemap": false,
"target": ["esnext"], "format": "esm", "bundle": true, "minify": true,
"treeShaking": true, "platform": "browser",
"jsx": "transform" },
"ansi": "ansi", init: { platform: PLATFORM_AUTO }};
export interface BuildResult extends ESBUILD.BuildResult { outputs: ESBUILD.OutputFile[]; contents: ESBUILD.OutputFile[];
packageSizeArr: string[][]; totalInstallSize: string;};
/** * Build result with Explicit Resource Management support. * * Extends `BuildResult` with `Disposable` and `AsyncDisposable` so callers * can use `using` / `await using` to automatically clean up per-build * resources (abort background SWR fetches, release registered workers, etc.) * when the result goes out of scope. * * Prefer `await using` over `using` — the async variant awaits all cleanup * (including in-flight `op_cache_put` writes), while the sync variant fires * cleanup without waiting. * * @example One-shot build with automatic cleanup * ```ts * { * await using result = await build({ entryPoints: ["/index.tsx"] }); * console.log(result.contents[0].text); * } * // ← background fetches aborted, per-build scope disposed * ``` * * @example Manual disposal * ```ts * const result = await build({ entryPoints: ["/index.tsx"] }); * // … use result … * await result[Symbol.asyncDispose](); * ``` * * @see {@link stop} to release the global esbuild WASM worker after all builds. */export type DisposableBuildResult = BuildResult & Disposable & AsyncDisposable;
export interface BuildResultContext extends ESBUILD.BuildResult { state: Context<LocalState>};
export const TheFileSystem = useFileSystem();
/** * Bundles entry points using esbuild-wasm with the full plugin pipeline. * * Returns a {@link DisposableBuildResult} that implements the * [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management) * protocol. Use `await using` to automatically abort per-build background * fetches and release registered resources when the result goes out of scope. * * @example * ```ts * await using result = await build({ entryPoints: ["/index.tsx"] }); * console.log(result.contents[0].text); * // ← cleanup runs automatically at end of scope * ``` * * @example Without `using` (manual cleanup) * ```ts * const result = await build({ entryPoints: ["/index.tsx"] }); * console.log(result.contents[0].text); * await result[Symbol.asyncDispose](); * ``` */export async function build(opts: BuildConfig = {}, filesystem: Promise<IFileSystem<unknown>> = TheFileSystem): Promise<DisposableBuildResult> { if (!fromContext("initialized")) dispatchEvent(INIT_LOADING);
// -- Per-build resource lifecycle ------------------------------------------- // The disposable stack collects cleanup callbacks (abort controllers, // WASM handles, workers, …) that run in LIFO order when the build // finishes. // // IMPORTANT — registration order matters for LIFO disposal: // 1. `defer(() => abortController.abort())` (registered FIRST, below) // 2. Plugins `scope.adopt(bgPromise, awaiter)` during build (registered mid-build) // // LIFO disposal runs #2 first (adopt awaiters drain background // promises, allowing in-flight `cache.put()` ops to complete // naturally), then #1 (abort fires as final cleanup for anything // still lingering). // // This prevents leaked `op_cache_put` / `fetchCancelHandle` / // `CacheResponseResource` handles by ensuring all async work // settles before the build boundary closes. const disposables = new AsyncDisposableStack(); const abortController = new AbortController();
// Register abort FIRST so it sits at the BOTTOM of the LIFO stack // → fires LAST on dispose (after all adopted promises settle). disposables.defer(async () => { abortController.abort(); // Yield a microtask to let Deno clean up aborted fetch handles await Promise.resolve(); });
const StateContext = new Context<LocalState>({ filesystem: Context.opaque(await filesystem), assets: [], config: Context.opaque(createConfig("build", opts)), scope: Context.opaque(disposables), abort: Context.opaque(abortController),
failedExtensionChecks: new Set(), failedManifestUrls: new Set(), host: DEFAULT_CDN_HOST, versions: new Map(),
tarballInflight: new Map(), tarballMounts: new Map(), sideEffectsMatchersCache: new Map(),
packageManifests: new Map(), });
const LocalConfig = fromContext("config", StateContext)!; const { origin: host } = LocalConfig?.cdn && !/:/.test(LocalConfig?.cdn) ? getCDNUrl(LocalConfig?.cdn + ":") : getCDNUrl(LocalConfig?.cdn ?? DEFAULT_CDN_HOST);
toContext("host", host ?? DEFAULT_CDN_HOST, StateContext);
const { platform, version, ...initOpts } = LocalConfig.init ?? {}; const esbuildOpts = LocalConfig.esbuild ?? {}; const esbuild = await init(initOpts, [platform, version]);
// Stores content from all external outputed files, this is for checking the gzip size when dealing with CSS and other external files let build_result: ESBUILD.BuildResult;
try { if (!esbuild?.build) throw new Error("Initialization failed, couldn't access esbuild.build(...) function");
try { build_result = await esbuild.build({ entryPoints: LocalConfig?.entryPoints ?? [], loader: { ".png": "file", ".jpeg": "file", ".ttf": "file", ".svg": "text", ".html": "text", ".scss": "css" }, define: Object.assign({ "__NODE__": "false", "process.env.NODE_ENV": "\"production\"", }, esbuildOpts.define), write: false, outdir: "/", plugins: [ AliasPlugin(StateContext), ExternalPlugin(StateContext), TarballPlugin(StateContext), PackagePlugin(StateContext), VirtualFileSystemPlugin(StateContext), HttpPlugin(StateContext), CdnPlugin(withContext({ origin: host }, StateContext)), ], ...esbuildOpts, }); } catch (e) { const fail = e as ESBUILD.BuildFailure; if (fail.errors) { // Log errors with added color info. to the virtual console const ansiMsgs = await createNotice(fail.errors, "error", false) ?? []; dispatchEvent(LOGGER_ERROR, new Error(ansiMsgs.join("\n")));
const message = (ansiMsgs.length > 1 ? `${ansiMsgs.length} error(s) ` : "") + "(if you are having trouble solving this issue, please create a new issue in the repo, https://github.com/okikio/bundlejs)"; dispatchEvent(LOGGER_ERROR, new Error(message));
const htmlMsgs = await createNotice(fail.errors, "error") ?? []; throw { msgs: htmlMsgs }; } else throw e; }
const formatted = await formatBuildResult({ state: StateContext, ...build_result });
// ── Attach Explicit Resource Management symbols ──────────────────── // // Disposal is idempotent — `AsyncDisposableStack.disposeAsync()` // no-ops on subsequent calls, so it's safe if the caller invokes // both `[Symbol.dispose]()` and `[Symbol.asyncDispose]()`. return Object.assign(formatted, { [Symbol.dispose]() { void disposables.disposeAsync() }, async [Symbol.asyncDispose]() { await disposables.disposeAsync(); // Yield a macrotask to let Deno runtime finalize internal resource // cleanup (fetchCancelHandle, CacheResponseResource) that happens // asynchronously after abort + promise settlement. await new Promise<void>(r => setTimeout(r, 0)); }, }); } catch (e) { // Build failed — abort is already registered in the defer, but we call it // explicitly here for immediate cancellation of any in-flight work. // The subsequent disposeAsync() will no-op on the defer since abort already fired. abortController.abort(); // Caller won't receive a disposable result — clean up before re-throwing. try { await disposables.disposeAsync(); } catch { /* don't mask the original error */ }
const err = e as Error; if (!("msgs" in err)) { dispatchEvent(BUILD_ERROR, err); }
throw e; }}
export async function formatBuildResult(_ctx: BuildResultContext) { const LocalConfig = fromContext("config", _ctx.state)!;
const assets = fromContext("assets", _ctx.state)! ?? []; const packageManifests = fromContext('packageManifests', _ctx.state) ?? new Map<string, PackageJson | FullPackageVersion>();
try { const esbuild_opts = LocalConfig.esbuild ?? {}; let outputs: ESBUILD.OutputFile[] = []; let contents: ESBUILD.OutputFile[] = [];
if (_ctx.warnings?.length > 0) { // Log errors with added color info. to the virtual console const ansiMsgs = await createNotice(_ctx.warnings, "warning", false) ?? []; dispatchEvent(LOGGER_WARN, ansiMsgs.join("\n"));
const message = `${ansiMsgs.length} warning(s) `; dispatchEvent(LOGGER_WARN, message); }
// Create an array of assets and actual output files, this will later be used to calculate total file size outputs = await Promise.all( Array.from(assets ?? []) .concat(_ctx?.outputFiles as ESBUILD.OutputFile[]) );
contents = await Promise.all( outputs .map((file): ESBUILD.OutputFile | null => { if (/\.map$/.test(file.path)) return null;
// For debugging reasons, if the user chooses verbose, print all the content to the Shared Worker console if (esbuild_opts?.logLevel === "verbose") { const ignoreFile = /\.(wasm|png|jpeg|webp)$/.test(file.path); if (ignoreFile) { dispatchEvent(LOGGER_LOG, "Output File: " + file.path); } else { dispatchEvent(LOGGER_LOG, "Output File: " + file.path + "\n" + file.text); } }
return { path: file.path, get text() { return file.text }, get contents() { return file.contents }, hash: file.hash }; })
// Remove null output files .filter(x => x !== null && x !== undefined) as ESBUILD.OutputFile[] );
// Ensure a fresh filesystem on every run // await fs?.clear?.(); // console.log({ contentsLen: contents.length }) const packageSizeArr: [string, string][] = []; let totalInstallSize = 0;
// Collect tarball mounts for extractedSize fallback const tarballMounts = fromContext('tarballMounts', _ctx.state) ?? new Map();
for (const [name, manifest] of packageManifests) { // Prefer registry-reported unpackedSize; fall back to the actual // bytes extracted from the tarball (pkg.pr.new packages don't have // dist.unpackedSize in their manifest). let size: number | undefined = manifest?.dist?.unpackedSize;
if (typeof size !== "number") { // Search tarball mounts for a matching package for (const [, mount] of tarballMounts) { if (mount.manifest?.name === manifest?.name && mount.extractedSize > 0) { size = mount.extractedSize; break; } } }
if (typeof size === "number") { packageSizeArr.push([name, bytes.format(size)]); totalInstallSize += size; } }
return { // state: StateContext.target, config: LocalConfig,
/** * The output and asset files without unnecessary croft, e.g. `.map` sourcemap files */ contents,
/** * The output and asset files with `.map` sourcemap files */ outputs,
packageSizeArr, totalInstallSize: bytes.format(totalInstallSize),
errors: _ctx.errors, mangleCache: _ctx.mangleCache, metafile: _ctx.metafile, outputFiles: _ctx.outputFiles, warnings: _ctx.warnings, }; } catch (e) { const err = e as Error; if (!("msgs" in err)) { dispatchEvent(BUILD_ERROR, err); }
throw e; }}