UNPKG

3.54 kBTypeScriptView Raw
1import { BuildFailure, BuildResult, BuildOptions, TsconfigRaw, Plugin } from 'esbuild';
2export { loadTsConfig } from 'load-tsconfig';
3
4/**
5 * Dynamically import files.
6 *
7 * As a temporary workaround for Jest's lack of stable ESM support, we fallback to require
8 * if we're in a Jest environment.
9 * See https://github.com/vitejs/vite/pull/5197#issuecomment-938054077
10 *
11 * @param file File path to import.
12 */
13declare const dynamicImport: RequireFunction;
14
15declare const JS_EXT_RE: RegExp;
16
17type RequireFunction = (outfile: string, ctx: {
18 format: "cjs" | "esm";
19}) => any;
20type GetOutputFile = (filepath: string, format: "esm" | "cjs") => string;
21type RebuildCallback = (error: Pick<BuildFailure, "errors" | "warnings"> | null, result: BuildResult | null) => void;
22type ReadFile = (filepath: string) => string;
23interface Options {
24 cwd?: string;
25 /**
26 * The filepath to bundle and require
27 */
28 filepath: string;
29 /**
30 * The `require` function that is used to load the output file
31 * Default to the global `require` function
32 * This function can be asynchronous, i.e. returns a Promise
33 */
34 require?: RequireFunction;
35 /**
36 * esbuild options
37 *
38 */
39 esbuildOptions?: BuildOptions & {
40 /**
41 * @deprecated `esbuildOptions.watch` is deprecated, use `onRebuild` instead
42 */
43 watch?: boolean | {
44 onRebuild?: RebuildCallback;
45 };
46 };
47 /**
48 * Get the path to the output file
49 * By default we simply replace the extension with `.bundled_{randomId}.js`
50 */
51 getOutputFile?: GetOutputFile;
52 /**
53 * Enable watching and call the callback after each rebuild
54 */
55 onRebuild?: (ctx: {
56 err?: Pick<BuildFailure, "errors" | "warnings">;
57 mod?: any;
58 dependencies?: string[];
59 }) => void;
60 /** External packages */
61 external?: (string | RegExp)[];
62 /** Not external packages */
63 notExternal?: (string | RegExp)[];
64 /**
65 * Automatically mark node_modules as external
66 * @default true - `false` when `filepath` is in node_modules
67 */
68 externalNodeModules?: boolean;
69 /**
70 * A custom tsconfig path to read `paths` option
71 *
72 * Set to `false` to disable tsconfig
73 * Or provide a `TsconfigRaw` object
74 */
75 tsconfig?: string | TsconfigRaw | false;
76 /**
77 * Preserve compiled temporary file for debugging
78 * Default to `process.env.BUNDLE_REQUIRE_PRESERVE`
79 */
80 preserveTemporaryFile?: boolean;
81 /**
82 * Provide bundle format explicitly
83 * to skip the default format inference
84 */
85 format?: "cjs" | "esm";
86 readFile?: ReadFile;
87}
88
89declare const tsconfigPathsToRegExp: (paths: Record<string, any>) => RegExp[];
90declare const match: (id: string, patterns?: (string | RegExp)[]) => boolean;
91/**
92 * An esbuild plugin to mark node_modules as external
93 */
94declare const externalPlugin: ({ external, notExternal, externalNodeModules, }?: {
95 external?: (string | RegExp)[] | undefined;
96 notExternal?: (string | RegExp)[] | undefined;
97 externalNodeModules?: boolean | undefined;
98}) => Plugin;
99declare const injectFileScopePlugin: ({ readFile, }?: {
100 readFile?: ReadFile | undefined;
101}) => Plugin;
102declare function bundleRequire<T = any>(options: Options): Promise<{
103 mod: T;
104 dependencies: string[];
105}>;
106
107export { GetOutputFile, JS_EXT_RE, Options, ReadFile, RebuildCallback, RequireFunction, bundleRequire, dynamicImport, externalPlugin, injectFileScopePlugin, match, tsconfigPathsToRegExp };