Skip to content

Commit bbb2f71

Browse files
committed
feat: add patchCjsDefaultExport feature
1 parent b29d8d9 commit bbb2f71

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/core/options.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ export type Options = {
1010
extraOutdir?: string
1111
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
1212
autoAddExts?: boolean
13+
/** Patch `export default` in `.d.cts` to `export = ` */
14+
patchCjsDefaultExport?: boolean
1315
} & (
1416
| {
1517
/**
@@ -45,5 +47,6 @@ export function resolveOptions(options: Options): OptionsResolved {
4547
ignoreErrors: options.ignoreErrors || false,
4648
extraOutdir: options.extraOutdir,
4749
autoAddExts: options.autoAddExts || false,
50+
patchCjsDefaultExport: options.patchCjsDefaultExport || false,
4851
}
4952
}

src/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,17 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
5555
entryFileNames = path.join(options.extraOutdir, entryFileNames)
5656
}
5757

58-
for (const [filename, source] of Object.entries(outputFiles)) {
58+
for (let [outname, source] of Object.entries(outputFiles)) {
59+
const fileName = entryFileNames.replace(
60+
'[name]',
61+
path.relative(outBase, outname),
62+
)
63+
if (options.patchCjsDefaultExport && fileName.endsWith('.d.cts')) {
64+
source = patchCjsDefaultExport(source)
65+
}
5966
this.emitFile({
6067
type: 'asset',
61-
fileName: entryFileNames.replace(
62-
'[name]',
63-
path.relative(outBase, filename),
64-
),
68+
fileName,
6569
source,
6670
})
6771
}
@@ -246,13 +250,16 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
246250
}
247251

248252
const textEncoder = new TextEncoder()
249-
for (const [filename, source] of Object.entries(outputFiles)) {
253+
for (let [filename, source] of Object.entries(outputFiles)) {
250254
const outDir = build.initialOptions.outdir
251255
let outFile = `${path.relative(outBase, filename)}.d.${outExt}`
252256
if (options.extraOutdir) {
253257
outFile = path.join(options.extraOutdir, outFile)
254258
}
255259
const filePath = outDir ? path.resolve(outDir, outFile) : outFile
260+
if (options.patchCjsDefaultExport && filePath.endsWith('.d.cts')) {
261+
source = patchCjsDefaultExport(source)
262+
}
256263
if (write) {
257264
await mkdir(path.dirname(filePath), { recursive: true })
258265
await writeFile(filePath, source)
@@ -292,6 +299,13 @@ function stripExt(filename: string) {
292299
return filename.replace(/\.(.?)[jt]s$/, '')
293300
}
294301

302+
function patchCjsDefaultExport(source: string) {
303+
return source.replace(
304+
/(?<=(?:[;}]|^)\s*export\s*)(?:\{\s*([\w$]+)\s*as\s+default\s*\}|default\s+([\w$]+))/,
305+
(_, s1, s2) => `= ${s1 || s2}`,
306+
)
307+
}
308+
295309
export function lowestCommonAncestor(...filepaths: string[]): string {
296310
if (filepaths.length === 0) return ''
297311
if (filepaths.length === 1) return path.dirname(filepaths[0])

tsdown.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineConfig({
88
plugins: [
99
IsolatedDecl({
1010
autoAddExts: true,
11+
patchCjsDefaultExport: true,
1112
}),
1213
],
1314
})

0 commit comments

Comments
 (0)