Skip to content

Commit

Permalink
feat: add patchCjsDefaultExport feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 12, 2024
1 parent b29d8d9 commit bbb2f71
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export type Options = {
extraOutdir?: string
/** Automatically add `.js` extension to resolve in `Node16` + ESM mode. */
autoAddExts?: boolean
/** Patch `export default` in `.d.cts` to `export = ` */
patchCjsDefaultExport?: boolean
} & (
| {
/**
Expand Down Expand Up @@ -45,5 +47,6 @@ export function resolveOptions(options: Options): OptionsResolved {
ignoreErrors: options.ignoreErrors || false,
extraOutdir: options.extraOutdir,
autoAddExts: options.autoAddExts || false,
patchCjsDefaultExport: options.patchCjsDefaultExport || false,
}
}
26 changes: 20 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,17 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
entryFileNames = path.join(options.extraOutdir, entryFileNames)
}

for (const [filename, source] of Object.entries(outputFiles)) {
for (let [outname, source] of Object.entries(outputFiles)) {
const fileName = entryFileNames.replace(
'[name]',
path.relative(outBase, outname),
)
if (options.patchCjsDefaultExport && fileName.endsWith('.d.cts')) {
source = patchCjsDefaultExport(source)
}
this.emitFile({
type: 'asset',
fileName: entryFileNames.replace(
'[name]',
path.relative(outBase, filename),
),
fileName,
source,
})
}
Expand Down Expand Up @@ -246,13 +250,16 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
}

const textEncoder = new TextEncoder()
for (const [filename, source] of Object.entries(outputFiles)) {
for (let [filename, source] of Object.entries(outputFiles)) {
const outDir = build.initialOptions.outdir
let outFile = `${path.relative(outBase, filename)}.d.${outExt}`
if (options.extraOutdir) {
outFile = path.join(options.extraOutdir, outFile)
}
const filePath = outDir ? path.resolve(outDir, outFile) : outFile
if (options.patchCjsDefaultExport && filePath.endsWith('.d.cts')) {
source = patchCjsDefaultExport(source)
}
if (write) {
await mkdir(path.dirname(filePath), { recursive: true })
await writeFile(filePath, source)
Expand Down Expand Up @@ -292,6 +299,13 @@ function stripExt(filename: string) {
return filename.replace(/\.(.?)[jt]s$/, '')
}

function patchCjsDefaultExport(source: string) {
return source.replace(
/(?<=(?:[;}]|^)\s*export\s*)(?:\{\s*([\w$]+)\s*as\s+default\s*\}|default\s+([\w$]+))/,
(_, s1, s2) => `= ${s1 || s2}`,
)
}

export function lowestCommonAncestor(...filepaths: string[]): string {
if (filepaths.length === 0) return ''
if (filepaths.length === 1) return path.dirname(filepaths[0])
Expand Down
1 change: 1 addition & 0 deletions tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
plugins: [
IsolatedDecl({
autoAddExts: true,
patchCjsDefaultExport: true,
}),
],
})

0 comments on commit bbb2f71

Please sign in to comment.