使用npm run build
Package.json文件命令
"build": "webpack --mode production --config webpack.config.prod.js",
构建后,我的捆绑大小为1010 KiB,这太大了。试图弄清楚,但是没有成功,所以最终放在这里
webpack.config.prod.js
var path = require('path');
var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
mode: 'production',
devtool: 'none',
entry: {
index: './src/index.js',
},
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/'
},
optimization: {
minimize: true,
},
plugins: [
new webpack.LoaderOptionsPlugin({ options: {} }),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new CopyWebpackPlugin([
{
from: 'style.css',
},
{
from: 'bootstrap.min.css',
},
{
from: 'index.html',
}
]),
new BundleAnalyzerPlugin(),
],
module: {
noParse:[ /node_modules\/localforage\/dist\/localforage.js/],
rules: [
{
test: /\.js$/,
enforce: "pre",
loaders: ['eslint-loader'],
include: path.join(__dirname, 'src')
},
{
test: /\.js$/,
loaders: ['babel-loader'],
include: [path.join(__dirname, 'src'), path.join(__dirname, 'translations')]
},
{
// do not exclude node_modules, since map.json is in node_modules
test: /\.json$/,
loader: 'json'
},
{
test: /\.css$/,
loaders: [ 'style-loader', 'css-loader' ]
},
]
}
};

我的.babelrc如下所示
{
"presets": ["react", "es2015", "stage-2"],
"env": {
"development": {
"plugins": [
["react-transform", {
"transforms": [{
"transform": "react-transform-hmr",
"imports": ["react"],
"locals": ["module"],
"preventFullImport": true
}, {
"transform": "react-transform-catch-errors",
"imports": ["react", "redbox-react"],
"preventFullImport": true
}]
}]
]
}
},
"plugins": [
["transform-object-rest-spread"],
["transform-react-display-name"],
["module-resolver", {
"root": ["./src"]
}]
]
}

我知道我在这里遗漏了一些东西。
应该接近或超过webpack建议,但1010 KB太多了
FROM BUILD LOGS
入口点大小限制中的警告:以下入口点组合资产大小超过建议限制(244 KiB)。这可能会影响Web性能。 入口点: 指数(1010 KiB) bundle.js
其他相关信息:
答案 0 :(得分:1)
您可能需要考虑拆分您的块。就像您可以为自己的代码和node_modules
分别使用单独的块。
您可能希望在生产模式下缩小代码以减小捆绑包大小。 您可以使用webpack配置文件执行以下操作:
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');
optimization: splitChunks: {
cacheGroups: {
vendor: {
chunks: "initial",
test: path.resolve(process.cwd(), "node_modules"),
name: "vendor",
enforce: true
}
}
},
minimizer: [
new UglifyJSPlugin({
uglifyOptions: {
sourceMap: true,
compress: {
drop_console: true,
conditionals: true,
unused: true,
comparisons: true,
dead_code: true,
if_return: true,
join_vars: true,
warnings: false
},
output: {
comments: false
}
}
})
]
这将为您的node_module
创建单独的捆绑包,您可以将其包含在主文件中。