diff --git a/docs/README.md b/docs/README.md index accdabdc..9f0a5e24 100644 --- a/docs/README.md +++ b/docs/README.md @@ -64,7 +64,8 @@ tsup automatically excludes packages specified in the `dependencies` and `peerDe tsup-node src/index.ts ``` -All other CLI flags still apply to this command. +All other CLI flags still apply to this command. You can still use the `noExternal` option to reinclude packages in the bundle, +for example packages that belong to a local monorepository. **If the regular `tsup` command doesn't work for you, please submit an issue with a link to your repo so we can make the default command better.** diff --git a/src/esbuild/external.ts b/src/esbuild/external.ts index 4c574795..12188f34 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -28,6 +28,14 @@ export const externalPlugin = ({ if (match(args.path, resolvePatterns)) { return } + // Respect explicit external/noExternal conditions + if (match(args.path, noExternal)) { + return + } + if (match(args.path, external)) { + return { external: true } + } + // Exclude any other import that looks like a Node module if (NON_NODE_MODULE_RE.test(args.path)) { return { path: args.path, @@ -35,16 +43,17 @@ export const externalPlugin = ({ } } }) + } else { + build.onResolve({ filter: /.*/ }, (args) => { + // Respect explicit external/noExternal conditions + if (match(args.path, noExternal)) { + return + } + if (match(args.path, external)) { + return { external: true } + } + }) } - - build.onResolve({ filter: /.*/ }, (args) => { - if (match(args.path, noExternal)) { - return - } - if (match(args.path, external)) { - return { external: true } - } - }) }, } } diff --git a/src/options.ts b/src/options.ts index 127a7307..e17333d8 100644 --- a/src/options.ts +++ b/src/options.ts @@ -116,6 +116,7 @@ export type Options = { silent?: boolean /** * Skip node_modules bundling + * Will still bundle modules matching the `noExternal` option */ skipNodeModulesBundle?: boolean /** diff --git a/test/index.test.ts b/test/index.test.ts index a7faba3b..038ea569 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -364,6 +364,30 @@ test('external', async () => { expect(output).toMatchSnapshot() }) +test('noExternal are respected when skipNodeModulesBundle is true', async () => { + const { output } = await run(getTestName(), { + 'input.ts': `export {foo} from 'foo' + export {bar} from 'bar' + export {baz} from 'baz' + `, + 'node_modules/foo/index.ts': `export const foo = 'foo'`, + 'node_modules/foo/package.json': `{"name":"foo","version":"0.0.0"}`, + 'node_modules/bar/index.ts': `export const bar = 'bar'`, + 'node_modules/bar/package.json': `{"name":"bar","version":"0.0.0"}`, + 'node_modules/baz/index.ts': `export const baz = 'baz'`, + 'node_modules/baz/package.json': `{"name":"baz","version":"0.0.0"}`, + 'tsup.config.ts': ` + export default { + skipNodeModulesBundle: true, + noExternal: [/foo/] + } + `, + }) + expect(output).toContain(`var foo = "foo"`) + expect(output).not.toContain(`var bar = "bar"`) + expect(output).not.toContain(`var baz = "baz"`) +}) + test('disable code splitting to get proper module.exports =', async () => { const { output } = await run( getTestName(),