egoist / tsup

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

default exports not working on CJS + treeshake: true #814

Closed csantos1113 closed 1 year ago

csantos1113 commented 1 year ago
    I'm having an issue after upgrading to `6.4.0` bundling to CJS.

Here is a simplified version of my react code:

// Component.tsx

import ReactSelect from 'react-select';

const Comp = (props) => {
  return <ReactSelect {...props} />
}
// tsup.config.ts

export default {
  bundle: true,
  clean: true,
  dts: true,
  format: ['cjs', 'esm'],
  minify: false,
  sourcemap: true,
  splitting: true,
  target: 'es2022',
  treeshake: 'smallest'

The generated CJS file with tsup v6.3.0 was working ✅

var ReactSelect = require('react-select');
var ReactSelect__default = /*#__PURE__*/_interopDefaultLegacy(ReactSelect);

var Comp = (props) => {
  return /* @__PURE__ */ jsxRuntime.jsx(ReactSelect__default["default"], { ...props });
}

The generated CJS file with tsup v6.4.0 isn't working ⚠️

var ReactSelect = require('react-select');
var Comp = (props) => {
  return /* @__PURE__ */ jsxRuntime.jsx(ReactSelect, { ...props });
}

Do you know if there is additional rollup configuration I need to pass to have rollup resolving default exports properly?

Originally posted by @csantos1113 in https://github.com/egoist/tsup/issues/749#issuecomment-1375931081

Upvote & Fund

Fund with Polar

csantos1113 commented 1 year ago

Reading this piece of Rollup documentation: https://rollupjs.org/guide/en/#outputinterop it says:

Note that the default mode of "default" mimics NodeJS behavior and is different from TypeScript esModuleInterop. To get TypeScript's behavior, explicitly set the value to "auto". In the examples, we will be using the CommonJS format, but the choice of interop similarly applies to AMD, IIFE and UMD targets as well.

So I wonder if we should add interop: 'auto' here 👇 https://github.com/egoist/tsup/blob/31b2e7261fa84c41b0673c36bf7c7133dcbbda11/src/plugins/tree-shaking.ts#L46-L51

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 6.6.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: