egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.48k stars 209 forks source link

feat: add cjsInterop support without splitting flag #1056

Open tmkx opened 6 months ago

tmkx commented 6 months ago

esbuild does not provide an exports metadata when the format is cjs.

splitting is working because it's also bundled as esm https://github.com/egoist/tsup/blob/fb4c2b6e75e29c58956eaaa6afab12b130accb14/src/esbuild/index.ts#L164-L165 and then transformed to cjs: https://github.com/egoist/tsup/blob/7000c8b6f5e69b801754a1846844cfc966f84571/src/plugins/cjs-splitting.ts#L21-L31

codesandbox[bot] commented 6 months ago

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders
Open Preview

vercel[bot] commented 6 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tsup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 29, 2024 9:02am
kibertoad commented 2 months ago

@sxzz Aby updates on this?

sxzz commented 2 months ago

Hmmm, can we just enable spiltting automatically if cjsInterop is enabled because of the problem https://github.com/egoist/tsup/pull/1056#discussion_r1582460208? And add the notice to the docs

tmkx commented 2 months ago
  1. splitting with cjs has edge cases: https://github.com/egoist/tsup/issues/255
  2. it's also broken when a plugin attaches named exports
Input ```ts // src/index.ts const a = { hello: 'world', }; export default a; ``` ```ts // tsup.config.ts export default defineConfig({ entry: ['./src/index.ts'], format: 'cjs', cjsInterop: true, splitting: true, plugins: [ { name: 'add', renderChunk(code) { return { code: code + `\nconst newValue = 1;\nexport { newValue };`, }; }, }, ], }); ```
Output ```js "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/index.ts var a = { hello: "world" }; var src_default = a; exports.default = src_default; const newValue = 1; exports.newValue = newValue; module.exports = exports.default; ```