diff --git a/package.json b/package.json index 1ef9fb46..59a28470 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "import": "./dist/index.mjs" } }, - "types": "./dist/index.d.mts", + "types": "./dist/index.d.ts", "bin": { "unbuild": "./dist/cli.mjs" }, diff --git a/src/builder/mkdist.ts b/src/builder/mkdist.ts index 8a2e0dc3..c1ccc12a 100644 --- a/src/builder/mkdist.ts +++ b/src/builder/mkdist.ts @@ -20,7 +20,7 @@ export async function mkdistBuild(ctx: BuildContext) { distDir, format: entry.format, cleanDist: false, - declaration: entry.declaration, + declaration: !!entry.declaration, pattern: entry.pattern, // @ts-ignore ext: entry.ext, diff --git a/src/builder/rollup.ts b/src/builder/rollup.ts index 5de56f31..fd4185d9 100644 --- a/src/builder/rollup.ts +++ b/src/builder/rollup.ts @@ -195,12 +195,17 @@ export async function rollupBuild(ctx: BuildContext) { chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.mts"), }); // #endregion - // #region .d.ts for node10 compatibility - await typesBuild.write({ - dir: resolve(ctx.options.rootDir, ctx.options.outDir), - entryFileNames: "[name].d.ts", - chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.ts"), - }); + // #region .d.ts for node10 compatibility (TypeScript version < 4.7) + if ( + ctx.options.declaration === true || + ctx.options.declaration === "compatible" + ) { + await typesBuild.write({ + dir: resolve(ctx.options.rootDir, ctx.options.outDir), + entryFileNames: "[name].d.ts", + chunkFileNames: (chunk) => getChunkFilename(ctx, chunk, "d.ts"), + }); + } // #endregion } diff --git a/src/types.ts b/src/types.ts index 677030b8..db9cc5be 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,7 +20,7 @@ export interface BaseBuildEntry { input: string; name?: string; outDir?: string; - declaration?: boolean; + declaration?: "compatible" | "node16" | boolean; } export interface UntypedBuildEntry extends BaseBuildEntry { @@ -64,7 +64,14 @@ export interface BuildOptions { rootDir: string; entries: BuildEntry[]; clean: boolean; - declaration?: boolean; + /** + * * if `compatible`, "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts". + * * if `node16`, "src/index.ts" will generate "dist/index.d.mts" and "dist/index.d.cts". + * * `true` is equivalent to `compatible`. + * * `false` will disable declaration generation. + * * `undefined` will auto detect based on "package.json". If "package.json" has "types" field, it will be `"compatible"`, otherwise `false`. + */ + declaration?: "compatible" | "node16" | boolean; outDir: string; stub: boolean; externals: (string | RegExp)[];