rolldown-require
Version:
bundle and require a file using rolldown!
81 lines (77 loc) • 2.31 kB
text/typescript
import { InputOptions, OutputOptions, ExternalOption } from 'rolldown';
type RequireFunction = (outfile: string, ctx: {
format: 'cjs' | 'esm';
}) => any;
type GetOutputFile = (filepath: string, format: 'esm' | 'cjs') => string;
interface Options {
cwd?: string;
/**
* The filepath to bundle and require
*/
filepath: string;
/**
* The `require` function that is used to load the output file
* Default to the global `require` function
* This function can be asynchronous, i.e. returns a Promise
*/
require?: RequireFunction;
/**
* esbuild options
*
*/
rolldownOptions?: {
input?: InputOptions;
output?: OutputOptions;
};
/**
* Get the path to the output file
* By default we simply replace the extension with `.bundled_{randomId}.js`
*/
getOutputFile?: GetOutputFile;
/**
* Enable watching and call the callback after each rebuild
*/
/** External packages */
external?: ExternalOption;
/** Not external packages */
/**
* Automatically mark node_modules as external
* @default true - `false` when `filepath` is in node_modules
*/
/**
* A custom tsconfig path to read `paths` option
*
* Set to `false` to disable tsconfig
*/
tsconfig?: string | false;
/**
* Preserve compiled temporary file for debugging
* Default to `process.env.BUNDLE_REQUIRE_PRESERVE`
*/
preserveTemporaryFile?: boolean;
/**
* Provide bundle format explicitly
* to skip the default format inference
*/
format?: 'cjs' | 'esm';
}
interface InternalOptions extends Omit<Options, 'cwd' | 'filepath'> {
isESM: boolean;
format: 'cjs' | 'esm';
tsconfig?: string;
}
declare const configDefaults: Readonly<{
resolve: {
extensions: string[];
};
}>;
declare function bundleFile(fileName: string, options: InternalOptions): Promise<{
code: string;
dependencies: string[];
}>;
declare function loadFromBundledFile(fileName: string, bundledCode: string, options: InternalOptions): Promise<any>;
declare function bundleRequire<T = any>(options: Options): Promise<{
mod: T;
dependencies: string[];
}>;
export { bundleFile, bundleRequire, configDefaults, loadFromBundledFile };