Skip to content

Commit

Permalink
feat: add skipNodeModulesBundle option
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Nov 21, 2024
1 parent 175197e commit 4827a81
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
17 changes: 14 additions & 3 deletions src/features/external.ts
Original file line number Diff line number Diff line change
@@ -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
}
},
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export async function buildSingle(resolved: ResolvedOptions): Promise<void> {
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,
Expand Down
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ export interface Options {
format: ModuleFormat,
) => MaybePromise<OutputOptions | void | null>)
onSuccess?: () => void | Promise<void>

/**
* Skip bundling node_modules.
*/
skipNodeModulesBundle?: boolean
}

/**
Expand Down Expand Up @@ -108,6 +113,7 @@ export async function resolveOptions(
dts = false,
unused = false,
watch = false,
skipNodeModulesBundle = false,
} = subOptions

entry = await resolveEntry(entry)
Expand All @@ -128,6 +134,7 @@ export async function resolveOptions(
dts,
unused,
watch,
skipNodeModulesBundle,
}
}),
)
Expand Down
1 change: 1 addition & 0 deletions tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default defineConfig({
format: 'esm',
clean: true,
platform: 'node',
skipNodeModulesBundle: true,
dts: true,
unused: { level: 'error' },
onSuccess() {
Expand Down

0 comments on commit 4827a81

Please sign in to comment.