Webpack 输出一个非常大的包:1.5MB 最小化。
我根据文档导入单个组件,使用导入 'antd/lib/...'
这些是我的导入:
import React from "react";
import ReactDOM from "react-dom";
import TreeSelect from 'antd/lib/tree-select';
const TreeNode = TreeSelect.TreeNode;
import 'antd/lib/tree-select/style/css';
import moment from 'moment';
import LocaleProvider from 'antd/lib/locale-provider';
import DatePicker from 'antd/lib/date-picker';
import 'antd/lib/date-picker/style/css'
const { RangePicker } = DatePicker;
import Menu from 'antd/lib/menu';
import 'antd/lib/menu/style/css'
import Dropdown from 'antd/lib/dropdown';
import 'antd/lib/dropdown/style/css';
import Modal from 'antd/lib/modal';
import 'antd/lib/modal/style/css';
import './styles.css';
我只使用了 5 个组件。捆绑包大小这么大有意义吗?我自己的代码相当小——大约 15KB 没有缩小。
更新:使用IgnorePlugin()
片刻后,我的包大小变小了 300KB。1.5MB 还是很大的。
Bellow 是 webpack 配置文件。
webpack.config.js:
const config = {
entry: {
main: path.resolve(SRC_DIR, "index.js"),
},
mode: 'development',
devtool: 'cheap-eval-source-map',
output: {
path: DIST_DIR,
filename: "bundle.js",
publicPath: "/static/bundles/",
},
resolve: {
extensions: [".js", ".json", ".css"]
},
module: {
rules: [
{
test: /\.js?/,
include: SRC_DIR,
loader: "babel-loader",
options: {
babelrc: true
}
},
{
test: /\.css$/,
use: [
"style-loader", "css-loader"
]
}
]
},
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
//new webpack.LoaderOptionsPlugin({ debug: true}),
]
};
module.exports = config;
webpack.prod.js(用于制作捆绑包):
const common = require('./webpack.config.js');
module.exports = Object.assign(common, {
entry: {
main: path.resolve(SRC_DIR, "index.js"),
},
mode: 'production',
devtool: false,
output: {
publicPath: '/static/dist/',
path: DIST_DIR,
filename: 'bundle.js'
},
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new BundleAnalyzerPlugin()
]
});