我正在使用React,Redux和Webpack构建一个小应用程序。当我切换到Webpack v.4时,我开始收到有关包大小的警告。我读过关于如何让它变小的文章。我还安装了捆绑包分析器,发现我在node-modules中有很大的寄存器。像往常一样,我只使用一个或两个方法,对每个我更改了所有导入内容的文件使用lodash
import _ from 'loadash'
至
import get from 'lodash/get'
但这并不能帮助我降低捆绑包大小。下面我附上了我的webpack.config.js。我不知道为什么它不能工作。更令人惊讶的是,我的构建命令是webpack --mode production,根据我在webpack 4文档中读到的内容,它应该有一些启用了优化的out of the box。
webpack.config.js
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const webpack = require('webpack');
const path = require('path');
module.exports = {
entry: {
app: ['babel-polyfill', './src/index.js']
},
output: {
path: path.resolve(__dirname, './dist'),
filename: 'bundle.js',
},
devServer: {
contentBase: ".",
headers: {
"Access-Control-Allow-Origin": "*"
},
inline: true,
historyApiFallback: true,
port: 8880
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['es2015', 'react'],
plugins: ["transform-class-properties"]
}
},
{
loader: 'eslint-loader'
}
]
},
{
test: /\.(png|jpg|gif|jpeg)$/,
use: [
{
loader: 'file-loader',
options: {}
}
]
},
{
test: /^.*\.(css|scss)$/,
use: [
'style-loader',
'css-loader?importLoaders=1&modules&localIdentName=[path]___[name]__[local]___[hash:base64:5]',
{
loader: 'postcss-loader',
options: {
plugins: () => [
require('autoprefixer')({
browsers: ['last 1 version']
})
]
}
},
'sass-loader'
],
}
]
},
plugins: [
new LodashModuleReplacementPlugin(),
new BundleAnalyzerPlugin()
]
};依赖项和开发依赖项。
"dependencies": {
"babel-runtime": "^6.26.0",
"classnames": "^2.2.5",
"clean-webpack-plugin": "^0.1.17",
"css-loader": "^1.0.0",
"express": "^4.16.2",
"file-loader": "^1.1.5",
"html-webpack-plugin": "^3.2.0",
"lodash": "^4.17.10",
"node-sass": "^4.6.0",
"normalize.css": "^8.0.0",
"postcss-loader": "^2.0.6",
"prop-types": "^15.5.10",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-icons": "^2.2.5",
"react-redux": "^5.0.6",
"react-router": "^4.1.2",
"react-router-dom": "^4.2.2",
"react-select": "^1.0.0-rc.10",
"react-test-renderer": "^16.4.1",
"react-textfit": "^1.0.1",
"react-transition-group": "^2.2.1",
"redux": "^4.0.0",
"redux-devtools-extension": "^2.13.2",
"redux-form": "^7.1.0",
"redux-saga": "^0.16.0",
"reselect": "^3.0.1",
"sass-loader": "^7.0.3",
"style-loader": "^0.21.0",
"webpack-bundle-analyzer": "^2.13.1" }, "devDependencies": {
"autoprefixer": "^9.0.1",
"axios": "^0.18.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.6",
"babel-jest": "^23.4.0",
"babel-loader": "^7.1.5",
"babel-plugin-lodash": "^3.3.4",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"enzyme": "^3.2.0",
"enzyme-adapter-react-15": "^1.0.5",
"enzyme-to-json": "^3.2.2",
"eslint": "^5.3.0",
"eslint-loader": "^2.1.0",
"eslint-plugin-react": "^7.11.1",
"extract-text-webpack-plugin": "^3.0.0",
"hoist-non-react-statics": "^3.0.1",
"identity-obj-proxy": "^3.0.0",
"isomorphic-fetch": "^2.2.1",
"jest": "^23.4.1",
"jquery": "^3.2.1",
"lodash-webpack-plugin": "^0.11.5",
"material-design-icons": "^3.0.1",
"path": "^0.12.7",
"prettier": "^1.14.2",
"transform-runtime": "^0.0.0",
"uglifyjs-webpack-plugin": "^1.3.0",
"webpack": "^4.16.5",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5",
"webpack-merge": "^4.1.1" },

发布于 2018-08-17 22:27:52
我建议只导入您需要的部件/函数:
import isEmpty from 'lodash.isempty';在你的package.json中
"dependencies": {
...
"lodash.foreach": "^4.5.0",
"lodash.includes": "^4.3.0",
"lodash.isempty": "^4.4.0",发布于 2018-08-17 16:23:59
是的,默认情况下,生产模式会进行缩小,但似乎您没有在配置中定义模式。
要使用生产模式,您必须定义如下模式:
module.exports = {
...
mode: 'production'
...
};或
您需要在cli中传递参数,如下所示:
webpack --mode=production 您正在--mode和production之间使用缺少的=。另外,如果未提供,默认模式也是生产模式。
此外,可以使用stats进行分析,而不是使用插件进行分析,还有内置的支持,而不是使用插件。
有关详细信息,请参阅:
发布于 2019-03-13 08:49:03
即使导入一个函数,Lodash包也很容易变得比您预期的要大得多。例如,仅从lodash导入get()将为您的生产贡献5K,精简包。我创建了一个库,它做了一些简化的假设,以提供许多函数的更小的实现:micro-dash。它的get()实现只贡献了65个字节。检查它,看看它是否适合您。
https://stackoverflow.com/questions/51890166
复制相似问题