/** * Options for configuring a bundle run. */ interface BundlerOptions { /** Output file extension: ".js" | ".cjs" | ".mjs" */ ext: string; /** Directory containing the compiled output files to process */ outDir: string; /** Source root directory used to resolve tsconfig paths. Defaults to process.cwd() */ rootDir?: string; /** Explicit path to tsconfig.json or jsconfig.json. Auto-detected from rootDir if omitted */ configPath?: string; } /** * Post-compilation import rewriter and alias resolver. * * Recursively processes all `.js`, `.cjs`, and `.mjs` files in the output * directory, rewriting: * - Relative imports to include the correct file extension * - Directory imports to use `/index.ext` * - Path aliases defined in tsconfig/jsconfig `paths` to relative paths * * Bare node_modules specifiers (e.g. `"express"`, `"@prisma/client"`) are * left untouched. * * @example * ```ts * const bundler = new Bundler(); * * bundler.bundle({ * ext: ".js", * outDir: "./dist", * rootDir: "./", * configPath: "./tsconfig.json", * }); * ``` */ export declare class Bundler { /** * Regexes for matching all import/export/require statement forms. * Defined as static to avoid recreation per file processed. */ private static readonly IMPORT_REGEXES; /** * Pre-compiles path alias patterns into a lookup-friendly structure * so alias matching during processing is O(n) over aliases rather than * re-parsing patterns per import. */ private compileAliases; /** * Loads and parses the tsconfig/jsconfig file, resolving any `extends` chain. * Falls back to `{ baseUrl: rootDir, paths: {} }` if no config is found. * * @param rootDir - Absolute root directory * @param configPath - Explicit config path, or undefined to auto-detect */ private loadConfig; /** * Searches for a tsconfig.json or jsconfig.json in the given directory. * Prefers tsconfig.json over jsconfig.json. * * @param dir - Directory to search in * @returns Absolute path to the config file, or null if not found */ private findConfig; /** * Recursively parses a tsconfig/jsconfig file, following `extends` chains * and merging `paths` with child config taking precedence over parent. * * @param configPath - Absolute path to the config file to parse * @param rootDir - Absolute root directory used as fallback baseUrl */ private parseConfig; /** * Resolves a tsconfig `extends` value that points to a node_modules package, * e.g. `"@tsconfig/node18/tsconfig.json"`. * * @param extendsValue - The raw extends string from tsconfig * @param rootDir - Root directory to resolve from * @returns Absolute path to the resolved config file, or null if not found */ private resolveNodeModulesConfig; /** * Reads a JSON file that may contain comments and trailing commas * (as tsconfig/jsconfig files allow) and returns the parsed object. * * @param filePath - Absolute path to the JSON file * @returns Parsed object, or empty object if parsing fails */ readJsonWithComments(filePath: string): any; /** * Resolves a single import path to its final rewritten form. * * Resolution order: * 1. Relative imports (`./`, `../`) — extension is added via `addExtension` * 2. Aliased imports — matched against compiled aliases and converted to relative paths * 3. Bare specifiers (`express`, `@prisma/client`) — returned untouched * * @param importPath - The raw import string from source code * @param fileDir - Absolute directory of the file containing the import * @param ext - The output file extension * @param resolvedPaths - Resolved baseUrl and paths from tsconfig * @param compiledAliases - Pre-compiled alias entries */ private resolveImport; private getFileDirInSource; /** * Appends the configured extension to an import path if not already present. * If the path resolves to a directory containing an `index` file, appends * `/index.ext` instead. * * @param importPath - The relative import path to fix * @param fileDir - Absolute directory of the file containing the import * @param ext - The output file extension */ private addExtension; /** * Converts an absolute path to a relative path from a given directory, * ensuring the result always starts with `./` or `../`. * * @param fromDir - The directory to compute the relative path from * @param toPath - The absolute target path * @returns A POSIX-style relative path */ private toRelative; /** * Rewrites all import/export/require statements in a string of JS source code. * Each regex is reset before use since they are stateful with the `/g` flag. * * @param content - Raw file content to process * @param fileDir - Absolute directory of the file being processed * @param ext - The output file extension * @param resolvedPaths - Resolved baseUrl and paths from tsconfig * @param compiledAliases - Pre-compiled alias entries */ private rewriteImports; /** * Recursively processes all `.js`, `.cjs`, and `.mjs` files in the output * directory, rewriting imports in-place. Files whose content is unchanged * are not written back to disk. * * @param options - Configuration options for this bundle run */ bundle(options: BundlerOptions): void; /** * Internal recursive directory walker, separated so that `bundle()` only * resolves config once at the top level rather than on every recursion. */ private _bundleDir; } export declare const bundler: Bundler; export {};