Skip to content

Commit 209eae4

Browse files
authored
Merge pull request #121 from boazpoolman/fix/type-declarations-for-custom-exports
fix: generate type declarations for all exports with types field
2 parents c8bbba9 + b8d289b commit 209eae4

6 files changed

Lines changed: 70 additions & 4 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@strapi/sdk-plugin': patch
3+
---
4+
5+
fix type declarations not being generated for custom exports
6+
7+
Previously, only `./strapi-admin` and `./strapi-server` exports would produce `.d.ts` files when a `types` field was defined in `package.json`. Custom exports silently skipped type generation because no per-bundle tsconfig existed for them.
8+
9+
The build now falls back to the project root `tsconfig.build.json` or `tsconfig.json` when no per-bundle tsconfig is found, so any export with a `types` field will correctly emit type declarations.

src/__tests__/e2e/build.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('build command', () => {
5656

5757
afterEach(() => {
5858
// Clean up built files after each test
59-
const filesToClean = ['index.mjs', 'index.js'];
59+
const filesToClean = ['index.mjs', 'index.js', 'index.d.ts'];
6060
for (const file of filesToClean) {
6161
const typesFilePath = path.join(distTypesDir, file);
6262
if (fs.existsSync(typesFilePath)) {
@@ -96,5 +96,18 @@ describe('build command', () => {
9696
// CJS should NOT exist (not specified in exports for ./types)
9797
expect(fs.existsSync(path.join(distTypesDir, 'index.js'))).toBe(false);
9898
});
99+
100+
it('should generate type declarations for custom exports with types field', async () => {
101+
const logger = createLogger({ silent: true, debug: false, timestamp: false });
102+
103+
await build({
104+
cwd: fixturePath,
105+
logger,
106+
silent: true,
107+
});
108+
109+
// Type declarations should be generated for the ./types custom export
110+
expect(fs.existsSync(path.join(distTypesDir, 'index.d.ts'))).toBe(true);
111+
});
99112
});
100113
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"include": ["./src"],
4+
"exclude": ["**/*.test.ts"],
5+
"compilerOptions": {
6+
"rootDir": "../",
7+
"outDir": "./dist"
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist"
5+
}
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"module": "ESNext",
5+
"moduleResolution": "bundler",
6+
"declaration": true,
7+
"strict": true,
8+
"skipLibCheck": true
9+
}
10+
}

src/cli/commands/utils/build/vite-config.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,28 @@ export async function createViteConfig(options: ViteConfigOptions): Promise<Inli
103103
}
104104

105105
// Add TypeScript declarations plugin if types are expected
106-
if (bundle.output.types && bundle.tsconfig) {
107-
const tsconfigPath = path.resolve(cwd, bundle.tsconfig);
108-
if (fs.existsSync(tsconfigPath)) {
106+
if (bundle.output.types) {
107+
// Find an appropriate tsconfig: try per-bundle first, then fall back to root-level configs
108+
let tsconfigPath: string | undefined;
109+
110+
if (bundle.tsconfig) {
111+
const candidate = path.resolve(cwd, bundle.tsconfig);
112+
if (fs.existsSync(candidate)) {
113+
tsconfigPath = candidate;
114+
}
115+
}
116+
117+
if (!tsconfigPath) {
118+
for (const fallback of ['tsconfig.build.json', 'tsconfig.json']) {
119+
const candidate = path.resolve(cwd, fallback);
120+
if (fs.existsSync(candidate)) {
121+
tsconfigPath = candidate;
122+
break;
123+
}
124+
}
125+
}
126+
127+
if (tsconfigPath) {
109128
const { default: dts } = await import('vite-plugin-dts');
110129

111130
// Output types to the correct location

0 commit comments

Comments
 (0)