From 0df8cb2836c24d43c0c11e4ec623060a42ace9d3 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 2 May 2024 10:37:47 +0200 Subject: [PATCH 1/2] feat: expose `notExternal` option, introduce `externalNodeModules` option --- src/index.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 21162d1..336c5c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -81,6 +81,15 @@ export interface Options { /** External packages */ external?: (string | RegExp)[] + /** Not external packages */ + notExternal?: (string | RegExp)[] + + /** + * Automatically mark node_modules as external + * @default true + */ + externalNodeModules?: boolean + /** A custom tsconfig path to read `paths` option */ tsconfig?: string @@ -128,9 +137,11 @@ export const match = (id: string, patterns?: (string | RegExp)[]) => { export const externalPlugin = ({ external, notExternal, + externalNodeModules = true, }: { external?: (string | RegExp)[] notExternal?: (string | RegExp)[] + externalNodeModules?: boolean } = {}): EsbuildPlugin => { return { name: "bundle-require:external", @@ -147,7 +158,7 @@ export const externalPlugin = ({ return } - if (args.path.match(PATH_NODE_MODULES_RE)) { + if (externalNodeModules && args.path.match(PATH_NODE_MODULES_RE)) { const resolved = args.path[0] === "." ? path.resolve(args.resolveDir, args.path) : args.path @@ -274,7 +285,11 @@ export function bundleRequire( ...(options.esbuildOptions?.plugins || []), externalPlugin({ external: options.external, - notExternal: resolvePaths, + notExternal: [ + ...(options.notExternal || []), + ...resolvePaths + ], + externalNodeModules: options.externalNodeModules, }), injectFileScopePlugin(), ], From 1f11d8ce8c1ca2603d50d257783971f20d35733a Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 2 May 2024 10:59:04 +0200 Subject: [PATCH 2/2] feat: update --- src/index.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 336c5c1..bca9a87 100644 --- a/src/index.ts +++ b/src/index.ts @@ -86,7 +86,7 @@ export interface Options { /** * Automatically mark node_modules as external - * @default true + * @default true - `false` when `filepath` is in node_modules */ externalNodeModules?: boolean @@ -289,7 +289,8 @@ export function bundleRequire( ...(options.notExternal || []), ...resolvePaths ], - externalNodeModules: options.externalNodeModules, + // When `filepath` is in node_modules, this is default to false + externalNodeModules: options.externalNodeModules ?? !options.filepath.match(PATH_NODE_MODULES_RE), }), injectFileScopePlugin(), ],