【问题标题】:webpack4 lodash size too bigwebpack4 lodash 尺寸太大
【发布时间】:2018-08-17 07:14:28
【问题描述】:

我正在使用 React、Redux 和 Webpack 构建一个小应用程序。当我切换到 Webpack v.4 时,我开始收到有关包大小的警告。我读到了使它变小的方法。我还安装了捆绑分析器,发现节点模块中有很大的 lodash。通常我每个文件只使用 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"   },

【问题讨论】:

  • 你在使用 webpack 4 吗?
  • 还可以使用 bundle-analyzer 来查看最新情况。它不可能是 lodash
  • @PlayMa256 我正在使用捆绑分析器,我将结果添加到我的帖子中。
  • 您可以尝试将您的 json 包更改为:“lodash.**”:“^4.5.0”、“lodash.**”:“^4.3.0”、“lodash.**” *": "^4.4.0", 而不是 "lodash": "^4.17.10",

标签: javascript reactjs webpack lodash


【解决方案1】:

我建议只导入您需要的部分/功能:

import isEmpty from 'lodash.isempty';

在你的 package.json 中

"dependencies": {
   ...
   "lodash.foreach": "^4.5.0",
   "lodash.includes": "^4.3.0",
   "lodash.isempty": "^4.4.0",

【讨论】:

  • 这不是必需的,因为 Webpack 4 现在可以正确地进行树摇动,并且 lodash 为其提供了支持。
  • 如果你确实 import _ from 'loadash' Webpack 假设你需要使用所有包并且只导入你需要的 deps 使你的包更加必要
  • 他没有这样做,他的 package.json 中有完整的导入
  • 这没关系,只要你导入特定的部分,比如lodash/get。摇树可以休息。
【解决方案2】:

是的,默认情况下生产模式会缩小,但您似乎没有在配置中定义模式。

要使用生产模式,您必须定义模式,如:

module.exports = {
  ...
  mode: 'production'
  ...
};

或

您需要在 cli 中传递参数,例如:

webpack --mode=production 

您在--mode 和production 之间使用了缺失的=。如果未提供默认模式,则再次生产。

除了使用插件进行分析、压缩和其他实用程序之外,还有内置支持而不是使用插件stats 可用于分析。

更多详情请参考:

https://webpack.js.org/configuration/optimization/

https://webpack.js.org/configuration/stats/

【讨论】:

  • 谢谢,但即使更改为 webpack --mode=production,我的 bundle.js 大小也完全相同。
  • 在缩小或被 webpack 发射后,lodash 的大小到底是多少?
  • 超过 800kB
【解决方案3】:

Lodash 包很容易变得比您预期的要大得多,即使导入单个函数也是如此。例如,仅从 lodash 导入 get() 将为您的生产、缩小的捆绑包贡献 5K。我创建了一个库,该库做了一些简化假设,以提供许多功能的小得多的实现:micro-dash。 get() 的实现只贡献了 65 个字节。看看它是否适合你。

【讨论】:

    猜你喜欢
    • 2014-11-24
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多