diff --git a/src/esbuild/external.ts b/src/esbuild/external.ts index 955ea97a..1120ca85 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -5,11 +5,13 @@ import { tsconfigPathsToRegExp, match } from 'bundle-require' const NON_NODE_MODULE_RE = /^[^.\/]|^\.[^.\/]|^\.\.[^\/]/ export const externalPlugin = ({ - patterns, + external, + noExternal, skipNodeModulesBundle, tsconfigResolvePaths, }: { - patterns?: (string | RegExp)[] + external?: (string | RegExp)[] + noExternal?: (string | RegExp)[] skipNodeModulesBundle?: boolean tsconfigResolvePaths?: Record }): Plugin => { @@ -35,18 +37,12 @@ export const externalPlugin = ({ }) } - if (!patterns || patterns.length === 0) return - build.onResolve({ filter: /.*/ }, (args) => { - const external = patterns.some((p) => { - if (p instanceof RegExp) { - return p.test(args.path) - } - return args.path === p - }) - - if (external) { - return { path: args.path, external } + if (match(args.path, noExternal)) { + return + } + if (match(args.path, external)) { + return { external: true } } }) }, diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index ce02082a..5f327e84 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -102,7 +102,7 @@ export async function runEsbuild( // So here we use a custom plugin to implement it format !== 'iife' && externalPlugin({ - patterns: external, + external, skipNodeModulesBundle: options.skipNodeModulesBundle, tsconfigResolvePaths: options.tsconfigResolvePaths, }), diff --git a/src/options.ts b/src/options.ts index 6df9a767..ab406053 100644 --- a/src/options.ts +++ b/src/options.ts @@ -50,7 +50,9 @@ export type Options = { } dts?: boolean | string | DtsConfig sourcemap?: BuildOptions['sourcemap'] - /** Don't bundle these packages */ + /** Always bundle modules matching given patterns */ + noExternal?: (string | RegExp)[] + /** Don't bundle these modules */ external?: (string | RegExp)[] /** Transform the result with `@babel/core` */ babel?: boolean