From 40d96d561f765e0755acd3ca0ff147bb837a3dce Mon Sep 17 00:00:00 2001 From: ZHAO JinXiang Date: Tue, 4 Jul 2023 18:21:19 +0800 Subject: [PATCH] feat: add case "compatible" and "node16" to option declaration --- README.md | 14 ++++++++++---- package.json | 2 +- src/builder/mkdist.ts | 2 +- src/builder/rollup.ts | 17 +++++++++++------ src/types.ts | 11 +++++++++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 960ffefa..0c8cc382 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Update `package.json`: } }, "main": "./dist/index.cjs", - "types": "./dist/index.d.cts", + "types": "./dist/index.d.ts", "files": [ "dist" ] @@ -84,7 +84,7 @@ export default { } ``` -You can either use `unbuild` key in `package.json` or `build.config.{js,ts,json}` to specify configuration. +You can either use `unbuild` key in `package.json` or `build.config.{js,cjs,mjs,ts,mts,cts,json}` to specify configuration. See options [here](./src/types.ts). @@ -109,8 +109,14 @@ export default defineBuildConfig({ // Change outDir, default is 'dist' outDir: 'build', - // Generates .d.ts declaration file - declaration: true, + /** + * * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts". + * * `node16` means "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', }) ``` 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..e1456f20 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; + /** + * * `compatible` means "src/index.ts" will generate "dist/index.d.mts", "dist/index.d.cts" and "dist/index.d.ts". + * * `node16` means "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)[];