Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420/** Inspired by https://github.com/hardfist/neo-tools/blob/main/packages/bundler/src/plugins/http.ts *//** * HTTP Plugin for esbuild * * Handles HTTP/HTTPS URL resolution and content loading: * - Resolves relative imports within downloaded files * - Resolves bare imports via CdnResolution * - Fetches file content with extension probing * - Extracts assets (WASM, Workers) from fetched files * * @module * * @example Direct HTTP import * ```ts * import { something } from "https://esm.sh/lodash@4.17.21"; * // -> HTTP_NAMESPACE, loads content from URL * ``` * * @example Relative import from HTTP source * ```ts * // Inside https://esm.sh/lodash@4.17.21/index.js * import { debounce } from "./debounce.js"; * // -> Resolved to https://esm.sh/lodash@4.17.21/debounce.js * ``` */import type { ESBUILD, LocalState } from "../types.ts";import type { AsyncDisposableStack } from "@bundle/utils/dispose";
import { Context, fromContext, toContext, withContext } from "../context/context.ts";
import { fetchContent, fetchHeaders } from "@bundle/utils/fetch-and-cache";import { decode } from "@bundle/utils/encode-decode";
import { LOGGER_ERROR, LOGGER_INFO, dispatchEvent } from "../configs/events.ts";
import { DEFAULT_CDN_HOST, getCDNStyle, getCDNUrl } from "../utils/cdn-format.ts";import { setFile } from "../utils/filesystem.ts";
import { isBareImport, isAbsolute } from "@bundle/utils/path";import { toURLPath, urlJoin } from "@bundle/utils/url";import { looksLikeJSRSpec } from "@bundle/utils/jsr-spec";
/** HTTP Plugin Namespace */export const HTTP_NAMESPACE = "http-url";
export interface HttpResolutionState<T> extends LocalState<T> { build: ESBUILD.PluginBuild}
// ============================================================================// Fetch Wrappers// ============================================================================
/** * Fetches package content from a URL. * * Returns the **final URL** after any redirects, which is critical for * resolving relative imports within the fetched content. */export async function fetchPkg( url: string, opts: { fetchOpts?: RequestInit; retry?: number; cacheMode?: 'normal' | 'force' | 'reload' | 'no-store'; signal?: AbortSignal; scope?: AsyncDisposableStack } = {}): Promise<{ url: string; content: Uint8Array; contentType: string | null }> { const { fetchOpts, retry, cacheMode = 'normal', signal, scope } = opts; try { const result = await fetchContent(url, { init: fetchOpts, retries: retry, cacheMode, signal, scope, });
// Build descriptive log message const flags = [ result.fromCache && 'cached', result.redirected && 'redirected', ].filter(Boolean).join(', '); const flagStr = flags ? ` (${flags})` : ''; const redirectStr = result.redirected ? ` → ${result.url}` : ''; dispatchEvent(LOGGER_INFO, `Fetch${flagStr} ${url}${redirectStr}`);
return { url: result.url, content: result.content, contentType: result.contentType, }; } catch (e) { const err = e as Error; throw new Error(`[fetchPkg] Failed to fetch ${url}\n${err.message}`, { cause: err }); }}
/** * Fetches only headers from a URL (for extension probing). * Uses HEAD request with GET fallback for servers that don't support HEAD. * * Returns the **final URL** after any redirects. */export async function fetchPkgHeaders( url: string, opts: { retry?: number; cacheMode?: 'normal' | 'force' | 'reload' | 'no-store'; signal?: AbortSignal } = {}): Promise<{ url: string; contentType: string | null }> { try { const result = await fetchHeaders(url, { retries: opts.retry, cacheMode: opts.cacheMode, signal: opts.signal }); return { url: result.url, contentType: result.contentType, }; } catch (e) { const err = e as Error; throw new Error(`[fetchPkgHeaders] Failed to probe ${url}\n${err.message}`, { cause: err }); }}
// ============================================================================// Asset Discovery// ============================================================================
/** * Fetches assets referenced in JS files via `new URL("...", import.meta.url)` * * External assets like WASM files and Workers are discovered and fetched. * These are stored in the virtual file system for later bundling. * * @param path URL path for the original JS file (must be final URL after redirects) * @param content Content of the original JS file * @param StateContext Context with filesystem access * @returns Promise of settled results for each discovered asset */export async function fetchAssets<T>( path: string, content: Uint8Array<ArrayBuffer>, StateContext: Context<LocalState<T>>) { // Regex for `new URL("./path.js", import.meta.url)`, // Supports comments so you can add comments and the regex will ignore them const rgx = /new(?:\s|\n?)+URL\((?:\s*(?:\/\*(?:.*\n)*\*\/)?(?:\/\/.*\n)?)*(?:(?!\`.*\$\{)['"`](.*)['"`]),(?:\s*(?:\/\*(?:.*\n)*\*\/)?(?:\/\/.*\n)?)*import\.meta\.url(?:\s*(?:\/\*(?:.*\n)*\*\/)?(?:\/\/.*\n)?)*\)/g; const parentURL = new URL("./", path).toString();
const FileSystem = fromContext("filesystem", StateContext);
const code = decode(content); const matches = Array.from(code.matchAll(rgx)) as RegExpMatchArray[];
const promises = matches.map(async ([, assetURL]) => { const abort = fromContext("abort", StateContext); const scope = fromContext("scope", StateContext); const { content: asset, url } = await fetchPkg(urlJoin(parentURL, assetURL), { signal: abort.signal, scope });
// Store asset in virtual file system for bundle analyzer if (FileSystem) { const filePath = toURLPath(url); await setFile(FileSystem, filePath, asset); }
const hashBuffer = await crypto.subtle.digest("SHA-256", asset as BufferSource); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray .map(b => b.toString(16).padStart(2, "0")) .join("");
return { path: assetURL, contents: asset, get text() { return decode(asset as BufferSource); }, hash: hashHex }; });
return await Promise.allSettled(promises);}
// ============================================================================// Extension Probing// ============================================================================
/** Path variants to try when extension is missing */export const FilePaths = ["", "/index"];
/** File extensions to probe */export const FileEndings = ["", ".js", ".mjs", ".ts", ".tsx", ".cjs", ".jsx", ".mts", ".cts"];
/** All combinations of path + extension to try */export const AllEndingVariants = Array.from( new Set( FilePaths.flatMap(path => FileEndings.map(ext => path + ext)) ));
export const EndingVariantsLength = AllEndingVariants.length;
/** * Probes for the correct file extension when not explicitly provided. * * TypeScript files often don't have file extensions in imports, but servers * require the full path. This function tries multiple extensions until one works. * * @param path Base path to probe * @param headersOnly If true, only fetch headers (faster for probing) * @param StateContext Optional context for caching failed probes * @returns Object with resolved url, contentType, and optionally content */export async function determineExtension<T>( path: string, { headersOnly = true, StateContext = null }: { headersOnly?: boolean; StateContext?: Context<LocalState<T>> | null; } = {}): Promise<{ url: string; contentType: string | null; content?: Uint8Array }> { const failedExtChecks = StateContext ? fromContext("failedExtensionChecks", StateContext) : null; const failedSet = failedExtChecks ?? new Set<string>(); const abort = StateContext ? fromContext("abort", StateContext) : undefined; const scope = StateContext ? fromContext("scope", StateContext) : undefined;
let firstError: Error | undefined;
for (let i = 0; i < EndingVariantsLength; i++) { const suffix = AllEndingVariants[i]; const testUrl = path + suffix;
// Skip URLs we've already tried and failed if (failedSet.has(testUrl)) continue;
try { // Use 'force' cacheMode to prevent background refresh during extension probing. // Background refresh with extensionless URLs can 404 if the CDN doesn't consistently // redirect/resolve extensionless paths. if (headersOnly) { const { url, contentType } = await fetchPkgHeaders(testUrl, { cacheMode: 'reload', signal: abort?.signal }); return { url, contentType }; } else { const { url, contentType, content } = await fetchPkg(testUrl, { cacheMode: 'normal', signal: abort?.signal, scope }); return { url, contentType, content }; } } catch (e) { failedSet.add(testUrl); // Keep the first error as it's usually most accurate if (i === 0) firstError = e as Error;
// If we've exhausted all variants, throw if (i >= EndingVariantsLength - 1) { const error = firstError ?? e; dispatchEvent(LOGGER_ERROR, error as Error); throw error; } } }
// TypeScript: unreachable, but needed for type safety throw new Error(`[determineExtension] Failed to resolve ${path}`);}
// ============================================================================// esbuild Resolution// ============================================================================
/** * Resolution algorithm for the esbuild HTTP plugin * * Handles three cases: * 1. HTTP/HTTPS URLs - direct load via HTTP_NAMESPACE * 2. Bare imports - delegate to CdnResolution * 3. Relative/absolute imports - resolve against parent URL * * **Important**: Uses `pluginData.url` (the final URL after redirects) as the * base for resolving relative imports. This ensures correct resolution when * CDN aliases like `@latest` redirect to specific versions. */export function HttpResolution<T>(StateContext: Context<HttpResolutionState<T>>) { const host = fromContext("host", StateContext)!; const build = fromContext("build", StateContext)!;
return async function (args: ESBUILD.OnResolveArgs): Promise<ESBUILD.OnResolveResult | undefined> { const argPath = args.path;
// Non-relative imports if (!argPath.startsWith(".") && !isAbsolute(argPath)) { // Direct HTTP URL — route to HTTP namespace. // sideEffects enrichment is handled by PackagePlugin when // package context exists in pluginData. if (/^https?:\/\//.test(argPath)) { return { path: argPath, namespace: HTTP_NAMESPACE, pluginData: args.pluginData, }; }
// Determine origin for CDN-follows-parent behavior: // when a file is loaded from esm.sh, its bare imports should // also resolve through esm.sh (not the configured CDN). const pathOrigin = new URL( urlJoin(args.pluginData?.url ?? host, "../", argPath) ).origin;
const NPM_CDN = getCDNStyle(pathOrigin) === "npm";
// Registry mode takes priority: when the user configures cdn: "npm.registry" // or cdn: "npm", ALL bare imports should resolve through the registry as // tarballs, regardless of which CDN the parent file was loaded from. // Without this, a file loaded from e.g. esm.sh would resolve its deps // through esm.sh (CDN-follows-parent) instead of the configured registry. const REGISTRY_HOST = getCDNStyle(host) === "registry"; const origin = REGISTRY_HOST ? host : (NPM_CDN ? pathOrigin : host);
// Bare import (e.g., "lodash") → delegate through build.resolve() // so CdnPlugin handles resolution with the correct CDN origin. // Also handle private imports (#internal) and JSR spec imports. // // The cdnOrigin is passed in pluginData so CdnPlugin can use // the parent file's CDN rather than the configured default. // This preserves the CDN-follows-parent behavior that was // previously achieved by calling CdnResolution() directly // with a context override. if (/^#/.test(argPath) || isBareImport(argPath) || looksLikeJSRSpec(argPath)) { return await build.resolve(argPath, { kind: args.kind, resolveDir: args.resolveDir, pluginData: Object.assign({}, args.pluginData, { cdnOrigin: origin, }), }); }
// Absolute import (e.g., "/lib/foo") → resolve against CDN origin. // sideEffects enrichment is handled by PackagePlugin. return { path: getCDNUrl(argPath, origin).url.toString(), namespace: HTTP_NAMESPACE, pluginData: args.pluginData, }; }
// Relative imports - resolve against parent's final URL let resolvedPath: string; if (isAbsolute(argPath)) { const parentUrl = new URL(args.pluginData?.url); parentUrl.pathname = argPath; resolvedPath = parentUrl.toString(); } else { // Relative: "./foo" resolved against parent URL resolvedPath = urlJoin(args.pluginData?.url, "../", argPath); }
// ======================================================================== // Manifest field remapping + sideEffects computation are now handled by // PackagePlugin (registered before HttpPlugin). It intercepts relative // imports that have package context (pluginData.packageBaseUrl + manifest) // and returns an enriched result with remapped paths and accurate // sideEffects hints. // // This handler only fires for relative imports that: // a) Have no package context (direct URL fetches, non-package origins) // b) Fall outside the package base URL (escaping the package tree) // // In both cases, no remapping or sideEffects enrichment is applicable. // ========================================================================
return { path: resolvedPath, namespace: HTTP_NAMESPACE, pluginData: args.pluginData, }; };}
// ============================================================================// esbuild Plugin// ============================================================================
/** * esbuild HTTP plugin for loading modules from URLs * * @param StateContext Context with config, assets, and filesystem */export function HttpPlugin<T>(StateContext: Context<LocalState<T>>): ESBUILD.Plugin { // Resolve CDN host 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);
return { name: HTTP_NAMESPACE, setup(build) { const ctx = withContext({ build: Context.opaque(build) }, StateContext);
// Route HTTP/HTTPS URLs to this plugin. // sideEffects enrichment is handled by PackagePlugin when // package context exists — this handler is a pure router. build.onResolve({ filter: /^https?:\/\// }, args => ({ path: args.path, namespace: HTTP_NAMESPACE, pluginData: args.pluginData, }));
// Route all imports within HTTP namespace through HttpResolution build.onResolve({ filter: /.*/, namespace: HTTP_NAMESPACE }, HttpResolution(ctx));
// ──────────────────────────────────────────────────────────── // NOTE: No onLoad handler here. // // All HTTP content loading (fetching, extension probing, // asset discovery, Flow type stripping, loader inference) // is handled by PackagePlugin, which is registered before // this plugin and owns onLoad for the http-url namespace. // ──────────────────────────────────────────────────────────── }, };}