Skip to content

Commit

Permalink
fix: respect noExternal option with Tsup node (#720)
Browse files Browse the repository at this point in the history
Co-authored-by: EGOIST <hi@egoist.dev>
  • Loading branch information
eric-burel and egoist committed Oct 17, 2022
1 parent 3ab4b88 commit bb2309a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
3 changes: 2 additions & 1 deletion docs/README.md
Expand Up @@ -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.**

Expand Down
27 changes: 18 additions & 9 deletions src/esbuild/external.ts
Expand Up @@ -28,23 +28,32 @@ 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,
external: true,
}
}
})
} 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 }
}
})
},
}
}
1 change: 1 addition & 0 deletions src/options.ts
Expand Up @@ -116,6 +116,7 @@ export type Options = {
silent?: boolean
/**
* Skip node_modules bundling
* Will still bundle modules matching the `noExternal` option
*/
skipNodeModulesBundle?: boolean
/**
Expand Down
24 changes: 24 additions & 0 deletions test/index.test.ts
Expand Up @@ -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(),
Expand Down

0 comments on commit bb2309a

Please sign in to comment.