I have a library of multiple react components, and I want to make the library tree-shakable so that when I import a component like
import { Checkbox } from 'my-react-components'
I don't import the whole bundle.
My index.js looks like this
export { default as Button } from './components/Button'
export { default as Checkbox } from './components/Checkbox'
export { default as FlexView } from './components/FlexView'
export { default as Radio } from './components/Radio'
export { default as Select } from './components/Select'
export { default as TextInput } from './components/TextInput'
export { default as Toggle } from './components/Toggle'
And I bundle using webpack
module.exports = {
mode: 'production',
entry: './src/index.ts',
output: {
path: path.resolve('./lib'),
filename: 'react-components.js',
libraryTarget: 'commonjs2',
},
// loaders and stuff...
// terser-webpack-plugin...
externals: {
// don't include react in the bundle
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react',
},
},
optimization: {
splitChunks: false,
sideEffects: false,
},
}
And in my babel config of course I have
['@babel/preset-env', {
modules: false,
}]
With this setup when I import only one component, and the whole bundle gets included (I am using webpack also when I import it). How do I prevent this?