【发布时间】:2018-10-31 15:25:54
【问题描述】:
我刚刚升级到 webpack 4 和 babel 7,并注意到我的包的大小大大增加了。
使用包分析器,我可以看到 antd,它的依赖项大约是我的 1.7mb 包的 1MB。
在开发模式下捆绑时,我可以看到所有的 antd 组件都被包含在内,即使我的应用当前只导入了一个按钮
import { Button } from 'antd';
....
我怎样才能只加载我需要的东西?这是我的相关配置
//webpack.config
{
devtool: false,
mode: 'production',
entry: [
'@babel/polyfill',
'antd/dist/antd.css',
'./js/router',
'./css/test.less',
],
output: {
path: path.resolve(__dirname, './plugins/js'),
filename: 'app.js',
},
plugins: [
new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /en/)
],
module: {
rules: [
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
....
还有:
//babelrc
{
"presets": [
['@babel/preset-env', {
modules: false,
useBuiltIns: 'entry',
targets: {
chrome:"58", ie: "11"
}
}],
'@babel/preset-react',
],
"plugins": [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-transform-modules-commonjs',
["import", { "libraryName": "antd", "style": "css" }]
]
}
【问题讨论】:
-
@A.com 我已经配置好了。它在我的 babelrc 中。我还尝试将其更改为该页面中的示例:
["import", { "libraryName": "antd", "libraryDirectory": "lib"}, "ant"],但没有更改。我也没有收到关于使用完整库的控制台消息警告。