diff --git a/src/features/external.ts b/src/features/external.ts index 2faef01..86a3285 100644 --- a/src/features/external.ts +++ b/src/features/external.ts @@ -1,15 +1,26 @@ +import path from 'node:path' import type { PackageJson } from 'pkg-types' import type { InputOptions, Plugin } from 'rolldown' export type External = InputOptions['external'] -export function ExternalPlugin(pkg: PackageJson): Plugin { +export function ExternalPlugin( + pkg: PackageJson, + skipNodeModulesBundle?: boolean, +): Plugin { const deps = Array.from(getProductionDeps(pkg)) return { name: 'tsdown:external', - resolveId(id) { + resolveId(id, importer, { isEntry }) { + if (isEntry) return + + const EXTERNAL = { id, external: true } + + if (skipNodeModulesBundle && !path.isAbsolute(id) && id[0] !== '.') { + return EXTERNAL + } if (deps.some((dep) => id === dep || id.startsWith(`${dep}/`))) { - return { id, external: true } + return EXTERNAL } }, } diff --git a/src/index.ts b/src/index.ts index 40e604a..81e7e96 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,9 +63,9 @@ export async function buildSingle(resolved: ResolvedOptions): Promise { treeshake, platform, plugins: [ - pkg && ExternalPlugin(pkg), - dts && IsolatedDecl.rolldown(dts === true ? {} : dts), + pkg && ExternalPlugin(pkg, resolved.skipNodeModulesBundle), unused && Unused.rolldown(unused === true ? {} : unused), + dts && IsolatedDecl.rolldown(dts === true ? {} : dts), ...plugins, ].filter((plugin) => !!plugin), ...resolved.inputOptions, diff --git a/src/options.ts b/src/options.ts index e2343db..2ea0f84 100644 --- a/src/options.ts +++ b/src/options.ts @@ -58,6 +58,11 @@ export interface Options { format: ModuleFormat, ) => MaybePromise) onSuccess?: () => void | Promise + + /** + * Skip bundling node_modules. + */ + skipNodeModulesBundle?: boolean } /** @@ -108,6 +113,7 @@ export async function resolveOptions( dts = false, unused = false, watch = false, + skipNodeModulesBundle = false, } = subOptions entry = await resolveEntry(entry) @@ -128,6 +134,7 @@ export async function resolveOptions( dts, unused, watch, + skipNodeModulesBundle, } }), ) diff --git a/tsdown.config.ts b/tsdown.config.ts index df0344e..f8cb6af 100644 --- a/tsdown.config.ts +++ b/tsdown.config.ts @@ -5,6 +5,7 @@ export default defineConfig({ format: 'esm', clean: true, platform: 'node', + skipNodeModulesBundle: true, dts: true, unused: { level: 'error' }, onSuccess() {