From 428eb17effa6c448444a0186e1109f96be8d02dd Mon Sep 17 00:00:00 2001 From: eric-burel Date: Mon, 19 Sep 2022 09:46:11 +0200 Subject: [PATCH 1/4] respect noExternal option with Tsup node --- docs/README.md | 3 ++- src/esbuild/external.ts | 3 +++ src/options.ts | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/README.md b/docs/README.md index dfdd9bd7..ed8e8580 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..a416db84 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -28,6 +28,9 @@ export const externalPlugin = ({ if (match(args.path, resolvePatterns)) { return } + if (match(args.path, noExternal)) { + return + } if (NON_NODE_MODULE_RE.test(args.path)) { return { path: args.path, diff --git a/src/options.ts b/src/options.ts index 4fd71a02..e8475dff 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 /** From cdf726f8365058ad96389ebba5427ea695eb1277 Mon Sep 17 00:00:00 2001 From: eric-burel Date: Mon, 19 Sep 2022 13:41:33 +0200 Subject: [PATCH 2/4] only register one onResolve cb for external management --- src/esbuild/external.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/esbuild/external.ts b/src/esbuild/external.ts index a416db84..12188f34 100644 --- a/src/esbuild/external.ts +++ b/src/esbuild/external.ts @@ -28,9 +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, @@ -38,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 } - } - }) }, } } From b4b73e3505ed63309cf27bc3bce40843581a9de7 Mon Sep 17 00:00:00 2001 From: eric-burel Date: Mon, 19 Sep 2022 13:56:58 +0200 Subject: [PATCH 3/4] add a regression test for skipNodeModulesBundle + noExternal --- test/__snapshots__/index.test.ts.snap | 44 +++++++++++++++++++++++++++ test/index.test.ts | 22 ++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/test/__snapshots__/index.test.ts.snap b/test/__snapshots__/index.test.ts.snap index 080f7e6e..56e52e5e 100644 --- a/test/__snapshots__/index.test.ts.snap +++ b/test/__snapshots__/index.test.ts.snap @@ -135,6 +135,50 @@ var answer = 42; " `; +exports[`noExternal are respected when skipNodeModulesBundle is true 1`] = ` +"\\"use strict\\"; +var __defProp = Object.defineProperty; +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; +var __getOwnPropNames = Object.getOwnPropertyNames; +var __hasOwnProp = Object.prototype.hasOwnProperty; +var __export = (target, all) => { + for (var name in all) + __defProp(target, name, { get: all[name], enumerable: true }); +}; +var __copyProps = (to, from, except, desc) => { + if (from && typeof from === \\"object\\" || typeof from === \\"function\\") { + for (let key of __getOwnPropNames(from)) + if (!__hasOwnProp.call(to, key) && key !== except) + __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); + } + return to; +}; +var __toCommonJS = (mod) => __copyProps(__defProp({}, \\"__esModule\\", { value: true }), mod); + +// input.ts +var input_exports = {}; +__export(input_exports, { + bar: () => import_bar.bar, + baz: () => import_baz.baz, + foo: () => foo +}); +module.exports = __toCommonJS(input_exports); + +// node_modules/foo/index.ts +var foo = \\"foo\\"; + +// input.ts +var import_bar = require(\\"bar\\"); +var import_baz = require(\\"baz\\"); +// Annotate the CommonJS export names for ESM import in node: +0 && (module.exports = { + bar, + baz, + foo +}); +" +`; + exports[`node protocol 1`] = ` "\\"use strict\\"; var __create = Object.create; diff --git a/test/index.test.ts b/test/index.test.ts index 7eaec1b1..3ac23a5b 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -360,6 +360,28 @@ 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).toMatchSnapshot() +}) + test('disable code splitting to get proper module.exports =', async () => { const { output } = await run( getTestName(), From 10761697e3f9312347389e1f80e256d056f76171 Mon Sep 17 00:00:00 2001 From: EGOIST Date: Thu, 6 Oct 2022 23:58:34 +0800 Subject: [PATCH 4/4] use assertion instead of snapshot --- test/__snapshots__/index.test.ts.snap | 44 --------------------------- test/index.test.ts | 4 ++- 2 files changed, 3 insertions(+), 45 deletions(-) diff --git a/test/__snapshots__/index.test.ts.snap b/test/__snapshots__/index.test.ts.snap index 56e52e5e..080f7e6e 100644 --- a/test/__snapshots__/index.test.ts.snap +++ b/test/__snapshots__/index.test.ts.snap @@ -135,50 +135,6 @@ var answer = 42; " `; -exports[`noExternal are respected when skipNodeModulesBundle is true 1`] = ` -"\\"use strict\\"; -var __defProp = Object.defineProperty; -var __getOwnPropDesc = Object.getOwnPropertyDescriptor; -var __getOwnPropNames = Object.getOwnPropertyNames; -var __hasOwnProp = Object.prototype.hasOwnProperty; -var __export = (target, all) => { - for (var name in all) - __defProp(target, name, { get: all[name], enumerable: true }); -}; -var __copyProps = (to, from, except, desc) => { - if (from && typeof from === \\"object\\" || typeof from === \\"function\\") { - for (let key of __getOwnPropNames(from)) - if (!__hasOwnProp.call(to, key) && key !== except) - __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); - } - return to; -}; -var __toCommonJS = (mod) => __copyProps(__defProp({}, \\"__esModule\\", { value: true }), mod); - -// input.ts -var input_exports = {}; -__export(input_exports, { - bar: () => import_bar.bar, - baz: () => import_baz.baz, - foo: () => foo -}); -module.exports = __toCommonJS(input_exports); - -// node_modules/foo/index.ts -var foo = \\"foo\\"; - -// input.ts -var import_bar = require(\\"bar\\"); -var import_baz = require(\\"baz\\"); -// Annotate the CommonJS export names for ESM import in node: -0 && (module.exports = { - bar, - baz, - foo -}); -" -`; - exports[`node protocol 1`] = ` "\\"use strict\\"; var __create = Object.create; diff --git a/test/index.test.ts b/test/index.test.ts index 3ac23a5b..64fe459e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -379,7 +379,9 @@ test('noExternal are respected when skipNodeModulesBundle is true', async () => } `, }) - expect(output).toMatchSnapshot() + 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 () => {