Skip to content

Commit 01ea624

Browse files
feat: add support for nested types in subpath exports (#112)
Co-authored-by: Hiroki Osame <[email protected]>
1 parent 97adefa commit 01ea624

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/utils/parse-package-json/get-export-entries.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ import { propertyNeedsQuotes } from '../property-needs-quotes.js';
55

66
const getFileType = (
77
filePath: string,
8-
): PackageType | undefined => {
8+
): PackageType | 'types' | undefined => {
99
if (filePath.endsWith('.mjs')) {
1010
return 'module';
1111
}
1212
if (filePath.endsWith('.cjs')) {
1313
return 'commonjs';
1414
}
15+
if (
16+
filePath.endsWith('.d.ts')
17+
|| filePath.endsWith('.d.cts')
18+
|| filePath.endsWith('.d.mts')
19+
) {
20+
return 'types';
21+
}
1522
};
1623

1724
const isPath = (filePath: string) => filePath.startsWith('.');

tests/specs/builds/package-exports.ts

+31
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,37 @@ export default testSuite(({ describe }, nodePath: string) => {
109109
expect(utilsCjs).toMatch('exports.sayHello =');
110110
});
111111

112+
test('conditions - types', async () => {
113+
await using fixture = await createFixture({
114+
...packageFixture({
115+
installTypeScript: true,
116+
}),
117+
'package.json': createPackageJson({
118+
type: 'module',
119+
exports: {
120+
types: {
121+
default: './dist/mts.d.ts',
122+
},
123+
import: './dist/mts.js',
124+
},
125+
}),
126+
});
127+
128+
const pkgrollProcess = await pkgroll([], {
129+
cwd: fixture.path,
130+
nodePath,
131+
});
132+
133+
expect(pkgrollProcess.exitCode).toBe(0);
134+
expect(pkgrollProcess.stderr).toBe('');
135+
136+
const indexMjs = await fixture.readFile('dist/mts.js', 'utf8');
137+
expect(indexMjs).toMatch('function sayGoodbye(name) {');
138+
139+
const indexDts = await fixture.readFile('dist/mts.d.ts', 'utf8');
140+
expect(indexDts).toMatch('declare function sayGoodbye(name: string): void;');
141+
});
142+
112143
test('conditions - import should allow cjs', async () => {
113144
await using fixture = await createFixture({
114145
...packageFixture(),

0 commit comments

Comments
 (0)