Skip to content

Commit

Permalink
fix: default exports not working on CJS + treeshake: true (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanZellmer committed Feb 12, 2025
1 parent 17068d9 commit f927d10
Show file tree
Hide file tree
Showing 4 changed files with 571 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/plugins/tree-shaking.ts
Expand Up @@ -44,6 +44,7 @@ export const treeShakingPlugin = ({
})

const result = await bundle.generate({
interop: 'auto',
format: this.format,
file: 'out.js',
sourcemap: !!this.options.sourcemap,
Expand Down
49 changes: 49 additions & 0 deletions test/index.test.ts
Expand Up @@ -998,6 +998,55 @@ test('use rollup for treeshaking', async () => {
)
})

test('use rollup for treeshaking --format cjs', async () => {
const { getFileContent } = await run(
getTestName(),
{
'package.json': `{
"dependencies": {
"react-select": "5.7.0",
"react": "17.0.2",
"react-dom": "17.0.2"
}
}`,
'input.tsx': `
import ReactSelect from 'react-select'
export const Component = (props: {}) => {
return <ReactSelect {...props} />
};
`,
'tsconfig.json': `{
"compilerOptions": {
"baseUrl": ".",
"esModuleInterop": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"noEmit": true,
"rootDir": ".",
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es6",
"importHelpers": true,
"outDir": "dist"
}
}`,
},
{
entry: ['input.tsx'],
flags: ['--treeshake', '--target', 'es2022', '--format', 'cjs'],
}
)

expect(await getFileContent('dist/input.js')).toContain(
`jsxRuntime.jsx(ReactSelect__default.default`
)
})

test('custom output extension', async () => {
const { outFiles } = await run(
getTestName(),
Expand Down
5 changes: 5 additions & 0 deletions test/package.json
Expand Up @@ -2,8 +2,13 @@
"private": true,
"devDependencies": {
"@egoist/path-parser": "1.0.4",
"@types/react": "17.0.50",
"@types/react-dom": "17.0.17",
"graphql": "^15.3.0",
"graphql-tools": "^5.0.0",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-select": "5.7.0",
"vue": "3.2.36",
"vue-router": "4.0.15"
},
Expand Down

0 comments on commit f927d10

Please sign in to comment.