/** * Shared bundler utilities used by both browser and node bundlers. * * This module contains the VFS plugin, path resolution, and shared module * code generation logic that is common to both esbuild and esbuild-wasm. */ import type { ISharedModuleRegistry, BundleOptions, BundleResult, BundleWarning, BundleError, Filesystem } from "../types"; /** * Minimal esbuild types needed for the shared utilities. * These are compatible with both esbuild and esbuild-wasm. */ export interface EsbuildMessage { text: string; location?: { file: string; line: number; column: number; lineText: string; } | null; } export interface EsbuildPlugin { name: string; setup: (build: EsbuildPluginBuild) => void; } export interface EsbuildPluginBuild { onResolve: (options: { filter: RegExp; namespace?: string; }, callback: (args: EsbuildResolveArgs) => Promise | EsbuildResolveResult | null | undefined) => void; onLoad: (options: { filter: RegExp; namespace?: string; }, callback: (args: EsbuildLoadArgs) => Promise | EsbuildLoadResult | null | undefined) => void; } export interface EsbuildResolveArgs { path: string; kind: string; resolveDir: string; importer: string; namespace: string; } export interface EsbuildResolveResult { path?: string; namespace?: string; external?: boolean; errors?: Array<{ text: string; }>; } export interface EsbuildLoadArgs { path: string; } export interface EsbuildLoadResult { contents?: string; loader?: string; resolveDir?: string; errors?: Array<{ text: string; }>; } export type EsbuildLoader = "js" | "jsx" | "ts" | "tsx" | "json" | "css" | "text"; /** * Type guard for esbuild BuildFailure */ export declare function isEsbuildBuildFailure(err: unknown): err is { errors: EsbuildMessage[]; warnings: EsbuildMessage[]; }; /** * Convert esbuild Message to our BundleError/BundleWarning format */ export declare function convertEsbuildMessage(msg: EsbuildMessage): BundleError | BundleWarning; export interface VfsPluginOptions { fs: Filesystem; entryPoint: string; installedPackages: Record; sharedModules: Set; sharedModuleRegistry: ISharedModuleRegistry | null; cdnBaseUrl: string; includedFiles: Set; /** * If true, CDN imports (http/https URLs) will be bundled by esbuild * rather than marked as external. This is required for Node/Bun * since they cannot resolve HTTP imports at runtime. * * - Browser: false (external) - browser can fetch at runtime * - Node/Bun: true (bundle) - native esbuild fetches during build * * @default false */ bundleCdnImports?: boolean; /** * Path aliases from tsconfig.json paths. * Maps alias patterns to target paths. * * @example { "@/*": ["/src/*"] } */ pathAliases?: Record; /** * Options for esm.sh CDN URL generation. */ esmOptions?: EsmUrlOptions; } /** * Options for esm.sh CDN URL generation. * * These options are appended as query parameters to esm.sh URLs * to customize how packages are resolved and bundled. */ export interface EsmUrlOptions { /** * Module IDs to mark as external in esm.sh. * * When a package imports one of these modules, esm.sh will leave * the import as-is instead of bundling it. This is useful for * shared modules that are provided by the host environment. * * @example ["react", "react-dom"] * * Results in: https://esm.sh/swr@2.0.0?external=react,react-dom */ external?: string[]; /** * ECMAScript target for esm.sh to build for. * * By default, esm.sh uses the User-Agent header to determine the target. * Setting this explicitly ensures consistent output across environments. * * @example "es2020" * @example "es2022" * * Results in: https://esm.sh/lodash@4.17.21?target=es2020 */ target?: string; } /** * Create an esbuild plugin that reads from a virtual filesystem. */ export declare function createVfsPlugin(options: VfsPluginOptions): EsbuildPlugin; /** * Check if a path is a bare import (npm package, not relative/absolute) */ export declare function isBareImport(path: string): boolean; /** * Try to resolve an import path using path aliases. * Returns the resolved path if a match is found, null otherwise. * * Supports wildcard patterns like "@/*" -> "/src/*" */ export declare function resolvePathAlias(importPath: string, pathAliases: Record): string | null; /** * Check if an import matches a shared module. * Handles exact matches and subpath imports. */ export declare function matchSharedModule(importPath: string, sharedModules: Set): string | null; /** * Parse an import path into package name and subpath. */ export declare function parseImportPath(importPath: string): { packageName: string; subpath?: string; }; /** * Resolve a bare import to an esm.sh CDN URL. * * Supports esm.sh query parameters: * - `?external=mod1,mod2` - Don't bundle these modules, leave imports as-is * - `?target=es2020` - ECMAScript target for the build * * For subpath imports with query params, uses esm.sh's `&` format: * - `https://esm.sh/react-dom@18.3.1&external=react/client` * * Note: Query parameters are skipped when version is "latest" due to an esm.sh * bug that causes 500 errors for some packages when using @latest with query params. * For full esm.sh feature support, use pinned versions (e.g., "react@18.3.1"). * * @see https://esm.sh/ for full documentation */ export declare function resolveToEsmUrl(importPath: string, installedPackages: Record, cdnBaseUrl: string, options?: EsmUrlOptions): string | null; /** * Resolve a relative or absolute path in the VFS. * Tries extensions and index files as needed. */ export declare function resolveVfsPath(fs: Filesystem, resolveDir: string, importPath: string): string | null; /** * Simple path resolution (handles . and ..) */ export declare function resolvePath(from: string, to: string): string; /** * Normalize a path (remove . and ..) */ export declare function normalizePath(path: string): string; /** * Get the directory name of a path */ export declare function dirname(path: string): string; /** * Get the appropriate esbuild loader based on file extension */ export declare function getLoader(path: string): EsbuildLoader; /** * Generate JavaScript code that injects CSS into the document. * This transforms CSS imports into runtime style injection. */ export declare function generateCssInjectionCode(css: string): string; /** * Generate JavaScript code that accesses a shared module at runtime. */ export declare function generateSharedModuleCode(moduleId: string, registry: ISharedModuleRegistry | null): string; /** * Minimal esbuild interface needed for bundling. * Compatible with both esbuild and esbuild-wasm. * * Uses a loose `Record` for build options to avoid * type conflicts between the various esbuild module signatures. */ export interface EsbuildInstance { build(options: Record): Promise<{ outputFiles?: Array<{ text: string; }>; warnings: EsbuildMessage[]; }>; } /** * Options for the shared bundle execution helper. */ export interface ExecuteBundleOptions { /** The esbuild instance to use */ esbuild: EsbuildInstance; /** Bundle options from the IBundler interface */ bundleOptions: BundleOptions; /** Base URL for CDN imports */ cdnBaseUrl: string; /** * Whether to bundle CDN imports inline. * - Browser: false (external) - browser can fetch at runtime * - Node/Bun: true (bundle) - esbuild fetches during build */ bundleCdnImports: boolean; /** * Options for esm.sh CDN URL generation. * If not provided, defaults will be used (shared modules as external). */ esmOptions?: EsmUrlOptions; } /** * Execute a bundle using esbuild with the VFS plugin. * * This is the shared implementation used by both browser and node WASM bundlers. * It handles entry point normalization, VFS plugin creation, and error handling. * * @param options - Bundle execution options * @returns Bundle result with code or errors */ export declare function executeBundleWithEsbuild(options: ExecuteBundleOptions): Promise; //# sourceMappingURL=bundler-utils.d.ts.map