Skip to content

Commit

Permalink
feat: add case "compatible" and "node16" to option declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Jul 4, 2023
1 parent 0191789 commit bb4e837
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"import": "./dist/index.mjs"
}
},
"types": "./dist/index.d.mts",
"types": "./dist/index.d.ts",
"bin": {
"unbuild": "./dist/cli.mjs"
},
Expand Down
2 changes: 1 addition & 1 deletion src/builder/mkdist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 11 additions & 6 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface BaseBuildEntry {
input: string;
name?: string;
outDir?: string;
declaration?: boolean;
declaration?: "compatible" | "node16" | boolean;
}

export interface UntypedBuildEntry extends BaseBuildEntry {
Expand Down Expand Up @@ -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)[];
Expand Down

0 comments on commit bb4e837

Please sign in to comment.