Skip to content

Commit

Permalink
fix: declaration option
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Sep 14, 2023
1 parent 511cd75 commit 3811611
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ export const autoPreset = definePreset(() => {
if (res.cjs) {
ctx.options.rollup.emitCJS = true;
}
if (res.dts) {
ctx.options.declaration = res.dts;
if (ctx.options.declaration === undefined) {
// Enable auto detect based on "package.json"
// If "package.json" has "types" field, it will be "compatible", otherwise false.
ctx.options.declaration = res.dts ? "compatible" : false;
}
consola.info(
"Automatically detected entries:",
Expand Down
2 changes: 1 addition & 1 deletion src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ async function _build(
rootDir,
entries: [],
clean: true,
declaration: false,
declaration: undefined,
outDir: "dist",
stub,
stubOptions: {
Expand Down
19 changes: 14 additions & 5 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,26 @@ export async function rollupBuild(ctx: BuildContext) {
);

// DTS Stub
await writeFile(
output + ".d.ts",
[
if (ctx.options.declaration) {
const dtsContent = [
`export * from ${JSON.stringify(resolvedEntryWithoutExt)};`,
hasDefaultExport
? `export { default } from ${JSON.stringify(
resolvedEntryWithoutExt,
)};`
: "",
].join("\n"),
);
].join("\n");

await writeFile(output + ".d.cts", dtsContent);
await writeFile(output + ".d.mts", dtsContent);
// .d.ts for node10 compatibility (TypeScript version < 4.7)
if (
ctx.options.declaration === "compatible" ||
ctx.options.declaration === true
) {
await writeFile(output + ".d.ts", dtsContent);
}
}

if (shebang) {
await makeExecutable(output + ".cjs");
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default defineBuildConfig([
entries: ["src/index"],
outDir: "dist/min",
sourcemap: true,
declaration: true,
declaration: "compatible",
rollup: {
esbuild: {
minify: true,
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/build.preset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { definePreset } from "../../src";

export default definePreset({
declaration: true,
declaration: "compatible",
rollup: {
cjsBridge: true,
},
Expand Down

0 comments on commit 3811611

Please sign in to comment.