Skip to content

Commit 4943c08

Browse files
authored
fix(deps): apply resolveDepSubpath when neverBundle is true (#1053)
1 parent 64715fe commit 4943c08

6 files changed

Lines changed: 33 additions & 5 deletions

File tree

‎docs/options/dependencies.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export default defineConfig({
140140

141141
When enabled, every import that follows npm package naming conventions (e.g. `lodash`, `@scope/pkg/utils`) is marked as external **as written, without being resolved**. This is fast and even works when dependencies are not installed. Note the following behaviors:
142142

143-
- Package specifiers are preserved exactly as written; subpaths like `my-dep/utils` are not rewritten, and `resolveDepSubpath` has no effect.
143+
- Package specifiers are preserved exactly as written; subpaths like `my-dep/utils` are only rewritten when `resolveDepSubpath` is enabled.
144144
- Other non-relative imports — [subpath imports](https://nodejs.org/api/packages.html#subpath-imports) starting with `#` and path aliases like `~/utils` — are still resolved: if they resolve into `node_modules`, they are kept external with the original specifier; otherwise the resolved local file is bundled.
145145

146146
`neverBundle: true` can be combined with `alwaysBundle` to bundle a few selected dependencies while externalizing everything else:

‎docs/zh-CN/options/dependencies.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export default defineConfig({
140140

141141
åŊį”Ļ后所有įŽĶ合 npm åŒ…åč§„čŒƒįš„åŊžå…Ĩäū‹åĶ‚ `lodash`、`@scope/pkg/utils`ïž‰éƒ―äžš**按原样标čŪ°äļšåĪ–éƒĻäūčĩ–äļäžščŋ›čĄŒč§Ģ析**。čŋ™äļ€čŋ‡įĻ‹éžåļļåŋŦ速åģä―ŋäūčĩ–æēĄæœ‰åŪ‰čĢ…äđŸčƒ―æ­Ģåļļå·Ĩä―œã€‚čŊ·æģĻæ„äŧĨäļ‹čĄŒäļšïžš
142142

143-
- 包čŊŽįŽĶ䞚按原样äŋį•™ïž›`my-dep/utils` čŋ™įąŧ子č·Ŋåū„äļäžščĒŦæ”đ写`resolveDepSubpath` 选éĄđäļį”Ÿæ•ˆã€‚
143+
- 包čŊŽįŽĶ䞚按原样äŋį•™ïž›`my-dep/utils` čŋ™įąŧ子č·Ŋåū„äŧ…åœĻåŊį”Ļ `resolveDepSubpath` æ—ķ才䞚čĒŦæ”đ写。
144144
- å…ķäŧ–非į›ļåŊđåŊžå…Ĩ——äŧĨ `#` 垀åĪīįš„[子č·Ŋåū„åŊžå…Ĩ](https://nodejs.org/api/packages.html#subpath-imports)和 `~/utils` čŋ™įąŧč·Ŋåū„åˆŦ名——äŧäžščĒŦč§Ģ析åĶ‚æžœč§Ģ析įŧ“æžœä―äšŽ `node_modules` äļ­ïžŒåˆ™äŋį•™åŽŸå§‹čŊŽįŽĶåđķåĪ–éƒĻ化åĶ则打包č§Ģæžåˆ°įš„æœŽåœ°æ–‡äŧķ。
145145

146146
`neverBundle: true` åŊäŧĨäļŽ `alwaysBundle` įŧ„合ä―ŋį”ĻåœĻåĪ–éƒĻ化å…ķäŧ–所有äūčĩ–įš„åŒæ—ķ打包少数指åۚäūčĩ–ïžš

‎skills/tsdown/references/option-dependencies.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export default defineConfig({
6868
})
6969
```
7070

71-
**Result:** Every import that follows npm package naming conventions is externalized as written, without being resolved. This is fast and even works when dependencies are not installed. Subpaths like `my-dep/utils` are preserved exactly (`resolveDepSubpath` has no effect). Other non-relative imports (`#` subpath imports, path aliases like `~/`) are resolved: they stay external if they resolve into node_modules, and are bundled if they map to local files. Combine with `alwaysBundle` to bundle selected dependencies.
71+
**Result:** Every import that follows npm package naming conventions is externalized as written, without being resolved. This is fast and even works when dependencies are not installed. Subpaths like `my-dep/utils` are preserved exactly unless `resolveDepSubpath` is enabled. Other non-relative imports (`#` subpath imports, path aliases like `~/`) are resolved: they stay external if they resolve into node_modules, and are bundled if they map to local files. Combine with `alwaysBundle` to bundle selected dependencies.
7272

7373
### `deps.alwaysBundle`
7474

‎src/features/deps.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ export function DepsPlugin(
364364
}
365365

366366
// `neverBundle: true`: imports that follow npm package naming
367-
// conventions are externalized as written, without resolving them.
367+
// conventions are externalized as written, without resolving them
368+
// unless `resolveDepSubpath` rewrites the subpath.
368369
// Anything else (`#` subpath imports, path aliases like `~/` or `@/`)
369370
// may map to local files, so it is resolved and kept external only if
370371
// it lands in `node_modules`.
@@ -376,9 +377,14 @@ export function DepsPlugin(
376377
if (id[0] === '\0' || id.startsWith('data:') || path.isAbsolute(id)) {
377378
return false
378379
}
379-
if (isBuiltin(id) || RE_PACKAGE_SPECIFIER.test(id)) {
380+
if (isBuiltin(id)) {
380381
return true
381382
}
383+
if (RE_PACKAGE_SPECIFIER.test(id)) {
384+
const resolvedDep =
385+
shouldResolveDepSubpath && (await resolveDepSubpath(id, resolve))
386+
return resolvedDep ? [true, resolvedDep] : true
387+
}
382388
const resolved = await resolve()
383389
return (
384390
!!resolved && (!!resolved.external || RE_NODE_MODULES.test(resolved.id))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## index.mjs
2+
3+
```mjs
4+
import { lt } from "my-dep/functions/lt.js";
5+
export { lt };
6+
7+
```

‎tests/e2e.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,21 @@ export { local } from './local'`,
15091509
expect(fileMap['index.mjs']).toContain('local = 1')
15101510
})
15111511

1512+
test('resolveDepSubpath resolves subpaths', async (context) => {
1513+
const { fileMap } = await testBuild({
1514+
context,
1515+
files: {
1516+
...node_modules,
1517+
'index.ts': `export { lt } from 'my-dep/functions/lt'`,
1518+
},
1519+
options: {
1520+
deps: { neverBundle: true, resolveDepSubpath: true },
1521+
},
1522+
})
1523+
1524+
expect(fileMap['index.mjs']).toContain('my-dep/functions/lt.js')
1525+
})
1526+
15121527
test('# subpath imports are resolved', async (context) => {
15131528
const { fileMap } = await testBuild({
15141529
context,

0 commit comments

Comments
 (0)