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 40d96d5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Update `package.json`:
}
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.cts",
"types": "./dist/index.d.ts",
"files": [
"dist"
]
Expand Down Expand Up @@ -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).

Expand All @@ -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',
})
```

Expand Down
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;
/**
* * `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)[];
Expand Down

0 comments on commit 40d96d5

Please sign in to comment.