Skip to content

Commit 0be7c6d

Browse files
kalvenschrautsxzz
authored andcommitted
fix: external type only packages @types/*
Author: Kalven Schraut <kalvens@rtvision.com> Ref #755 closes #755
1 parent 02f43b4 commit 0be7c6d

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/features/dep.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isBuiltin } from 'node:module'
22
import { blue, underline, yellow } from 'ansis'
33
import { createDebug } from 'obug'
4-
import { RE_NODE_MODULES } from 'rolldown-plugin-dts/filename'
4+
import { RE_DTS, RE_NODE_MODULES } from 'rolldown-plugin-dts/filename'
55
import { and, id, importerId, include } from 'rolldown/filter'
66
import { matchPattern, slash, typeAssert } from '../utils/general.ts'
77
import { shimFile } from './shims.ts'
@@ -165,11 +165,17 @@ export function DepPlugin({
165165
}
166166
}
167167

168-
if (
169-
deps &&
170-
(deps.includes(id) || deps.some((dep) => id.startsWith(`${dep}/`)))
171-
) {
172-
return true
168+
if (deps) {
169+
if (deps.includes(id) || deps.some((dep) => id.startsWith(`${dep}/`))) {
170+
return true
171+
}
172+
173+
if (importer && RE_DTS.test(importer) && !id.startsWith('@types/')) {
174+
const typesName = `@types/${id.replace(/^@/, '').replaceAll('/', '__')}`
175+
if (deps.includes(typesName)) {
176+
return true
177+
}
178+
}
173179
}
174180

175181
return false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## index.d.mts
2+
3+
```mts
4+
import { version } from "foo";
5+
export { version };
6+
```
7+
8+
## index.mjs
9+
10+
```mjs
11+
//#region node_modules/foo/index.js
12+
const version = "1.0.0";
13+
14+
//#endregion
15+
export { version };
16+
```

tests/e2e.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,3 +748,40 @@ describe('import.meta.glob', () => {
748748
expect(outputFiles.length).toBe(1)
749749
})
750750
})
751+
752+
test('externalize @types/foo', async (context) => {
753+
const node_modules = {
754+
'node_modules/foo/index.js': `export const version = "1.0.0"`,
755+
'node_modules/foo/package.json': JSON.stringify({
756+
name: 'foo',
757+
version: '1.0.0',
758+
main: 'index.js',
759+
}),
760+
761+
'node_modules/@types/foo/index.d.ts': `export const version: string`,
762+
'node_modules/@types/foo/package.json': JSON.stringify({
763+
name: '@types/foo',
764+
version: '1.0.0',
765+
types: 'index.d.ts',
766+
}),
767+
}
768+
769+
const { fileMap } = await testBuild({
770+
context,
771+
files: {
772+
...node_modules,
773+
'index.ts': `export { version } from 'foo'`,
774+
'package.json': JSON.stringify({
775+
name: 'test-pkg',
776+
version: '1.0.0',
777+
dependencies: {
778+
'@types/foo': '^1.0.0',
779+
},
780+
}),
781+
},
782+
options: { dts: true },
783+
})
784+
785+
expect(fileMap['index.mjs']).toContain('1.0.0')
786+
expect(fileMap['index.d.mts']).toContain('from "foo"')
787+
})

0 commit comments

Comments
 (0)