From 908c0c090dee8adeb8490a49e200166239c12350 Mon Sep 17 00:00:00 2001 From: kricsleo Date: Tue, 17 Dec 2024 16:15:02 +0800 Subject: [PATCH 1/2] fix: fix the `.d.cts` default export type --- src/builders/rollup/build.ts | 4 +++- src/builders/rollup/plugins/cjs.ts | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/builders/rollup/build.ts b/src/builders/rollup/build.ts index 8b3d6f40..3af01e6d 100644 --- a/src/builders/rollup/build.ts +++ b/src/builders/rollup/build.ts @@ -9,6 +9,7 @@ import { getRollupOptions } from "./config"; import { getChunkFilename } from "./utils"; import { rollupStub } from "./stub"; import { rollupWatch } from "./watch"; +import { fixCJSExportTypePlugin } from "./plugins/cjs"; export async function rollupBuild(ctx: BuildContext): Promise { // Stub mode @@ -81,7 +82,8 @@ export async function rollupBuild(ctx: BuildContext): Promise { ...rollupOptions.plugins, dts(ctx.options.rollup.dts), removeShebangPlugin(), - ]; + ctx.options.rollup.emitCJS && fixCJSExportTypePlugin() + ].filter(Boolean); await ctx.hooks.callHook("rollup:dts:options", ctx, rollupOptions); const typesBuild = await rollup(rollupOptions); diff --git a/src/builders/rollup/plugins/cjs.ts b/src/builders/rollup/plugins/cjs.ts index 2bba6d0e..b487c478 100644 --- a/src/builders/rollup/plugins/cjs.ts +++ b/src/builders/rollup/plugins/cjs.ts @@ -14,6 +14,29 @@ export function cjsPlugin(_opts?: any): Plugin { } as Plugin; } +// Ported from https://github.com/egoist/tsup/blob/cd03e1e00ec2bd6676ae1837cbc7e618ab6a2362/src/rollup.ts#L92-L109 +export function fixCJSExportTypePlugin(): Plugin { + return { + name: "unbuild-fix-cjs-export-type", + renderChunk(code, info, opts) { + if ( + info.type !== "chunk" || + !info.fileName.endsWith('.d.cts') || + !info.isEntry || + info.exports?.length !== 1 || + info.exports[0] !== "default" + ) { + return; + } + + return code.replace( + /(?<=(?<=[;}]|^)\s*export\s*){\s*([\w$]+)\s*as\s+default\s*}/, + `= $1`, + ); + }, + } as Plugin; +} + const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/; const CJSShim = ` From c8e8445c7d499d4b58e37d9b326dc83dd153b468 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:12:43 +0000 Subject: [PATCH 2/2] chore: apply automated updates --- src/builders/rollup/build.ts | 2 +- src/builders/rollup/plugins/cjs.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/builders/rollup/build.ts b/src/builders/rollup/build.ts index 3af01e6d..f7c49889 100644 --- a/src/builders/rollup/build.ts +++ b/src/builders/rollup/build.ts @@ -82,7 +82,7 @@ export async function rollupBuild(ctx: BuildContext): Promise { ...rollupOptions.plugins, dts(ctx.options.rollup.dts), removeShebangPlugin(), - ctx.options.rollup.emitCJS && fixCJSExportTypePlugin() + ctx.options.rollup.emitCJS && fixCJSExportTypePlugin(), ].filter(Boolean); await ctx.hooks.callHook("rollup:dts:options", ctx, rollupOptions); diff --git a/src/builders/rollup/plugins/cjs.ts b/src/builders/rollup/plugins/cjs.ts index b487c478..5edfa5bd 100644 --- a/src/builders/rollup/plugins/cjs.ts +++ b/src/builders/rollup/plugins/cjs.ts @@ -21,7 +21,7 @@ export function fixCJSExportTypePlugin(): Plugin { renderChunk(code, info, opts) { if ( info.type !== "chunk" || - !info.fileName.endsWith('.d.cts') || + !info.fileName.endsWith(".d.cts") || !info.isEntry || info.exports?.length !== 1 || info.exports[0] !== "default"