Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose notExternal option, introduce externalNodeModules option #44

Merged
merged 2 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 - `false` when `filepath` is in node_modules
*/
externalNodeModules?: boolean

/** A custom tsconfig path to read `paths` option */
tsconfig?: string

Expand Down Expand Up @@ -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",
Expand All @@ -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
Expand Down Expand Up @@ -274,7 +285,12 @@ export function bundleRequire<T = any>(
...(options.esbuildOptions?.plugins || []),
externalPlugin({
external: options.external,
notExternal: resolvePaths,
notExternal: [
...(options.notExternal || []),
...resolvePaths
],
// When `filepath` is in node_modules, this is default to false
externalNodeModules: options.externalNodeModules ?? !options.filepath.match(PATH_NODE_MODULES_RE),
}),
injectFileScopePlugin(),
],
Expand Down
Loading