Skip to content

Commit

Permalink
feat: use fix-dts-default-cjs-exports to transform CJS types (egois…
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin authored Feb 27, 2025
1 parent 773d27e commit c654e5f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 20 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"consola": "^3.4.0",
"debug": "^4.4.0",
"esbuild": "^0.25.0",
"fix-dts-default-cjs-exports": "^1.0.0",
"joycon": "^3.1.1",
"picocolors": "^1.1.1",
"postcss-load-config": "^6.0.1",
Expand Down
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 2 additions & 20 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getProductionDeps, loadPkg } from './load'
import { reportSize } from './lib/report-size'
import type { NormalizedOptions } from './'
import type { InputOptions, OutputOptions, Plugin } from 'rollup'
import { FixDtsDefaultCjsExportsPlugin } from 'fix-dts-default-cjs-exports/rollup'

const logger = createLogger()

Expand Down Expand Up @@ -89,25 +90,6 @@ const getRollupConfig = async (
},
}

const fixCjsExport: Plugin = {
name: 'tsup:fix-cjs-export',
renderChunk(code, info) {
if (
info.type !== 'chunk' ||
!/\.(ts|cts)$/.test(info.fileName) ||
!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`,
)
},
}

return {
inputConfig: {
input: dtsOptions.entry,
Expand Down Expand Up @@ -167,7 +149,7 @@ const getRollupConfig = async (
entryFileNames: `[name]${outputExtension}`,
chunkFileNames: `[name]-[hash]${outputExtension}`,
plugins: [
format === 'cjs' && options.cjsInterop && fixCjsExport,
format === 'cjs' && options.cjsInterop && FixDtsDefaultCjsExportsPlugin(),
].filter(Boolean),
}
}),
Expand Down

0 comments on commit c654e5f

Please sign in to comment.