我有一个包含多个反应组件的库,我想让库可以摇树,这样当我导入一个组件时
import { Checkbox } from 'my-react-components'
我不导入整个捆绑包。
我的 index.js 看起来像这样
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'
我使用 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,
},
}
在我的 babel 配置中,我当然有
['@babel/preset-env', {
modules: false,
}]
使用此设置,当我只导入一个组件并包含整个包时(导入时我也在使用 webpack)。我该如何防止这种情况?