Webpack软件包的大小在每个连续的构建中都会增加

时间:2019-04-04 11:29:25

标签: javascript reactjs webpack sass

我正在使用webpack打包项目,以便将其用作库。这是一个组件库,因此我为每个位于src/ui下自己目录中的组件生成小捆绑包。这是一个示例组件:

src/
 |- ui/
    |-- anchor/
        |- dist/
        |- index.js
        |- _anchor.scss

我假装在dist/index.js中生成捆绑的组件。

在我当前的Webpack配置下,例如,如果我连续运行NODE_ENV=production webpack --display-modules(连续三次不更改任何代码),则Webpack的输出为:

                   Asset      Size  Chunks             Chunk Names
    anchor/dist/index.js  4.69 KiB       0  [emitted]  anchor
anchor/dist/index.js.map  15.8 KiB       0  [emitted]  anchor
Entrypoint anchor = anchor/dist/index.js anchor/dist/index.js.map
[0] external "react" 42 bytes {0} [built]
[1] external "classnames" 42 bytes {0} [built]
[2] ./src/ui/anchors/_anchor.scss 1.94 KiB {0} [built]
[3] ./node_modules/css-loader/dist/runtime/api.js 2.35 KiB {0} [built]
[4] ./src/ui/anchor/index.js + 1 modules 1.69 KiB {0} [built]
    | ./src/ui/anchor/index.js 1.41 KiB [built]
    | ./node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/esm/defineProperty.js 269 bytes [built]
                   Asset      Size  Chunks             Chunk Names
    anchor/dist/index.js   5.7 KiB       0  [emitted]  anchor
anchor/dist/index.js.map  14.3 KiB       0  [emitted]  anchor
Entrypoint anchor = anchor/dist/index.js anchor/dist/index.js.map
[0] ./src/ui/anchor/dist/index.js 4.69 KiB {0} [built]
[1] external "react" 42 bytes {0} [built]
[2] external "classnames" 42 bytes {0} [built]
                   Asset      Size  Chunks             Chunk Names
    anchor/dist/index.js  6.71 KiB       0  [emitted]  anchor
anchor/dist/index.js.map  16.6 KiB       0  [emitted]  anchor
Entrypoint anchor = anchor/dist/index.js anchor/dist/index.js.map
[0] ./src/ui/anchor/dist/index.js 5.7 KiB {0} [built]
[1] external "react" 42 bytes {0} [built]
[2] external "classnames" 42 bytes {0} [built]

如您所见,捆绑包的大小在每次执行时都会增加1 KiB。

我当前的webpack配置为:

const path = require('path')

module.exports = {
  mode: 'production',
  entry: {
    anchor: './src/ui/anchor'
  },
  output: {
    path: path.resolve(__dirname, 'src', 'ui'),
    filename: '[name]/dist/index.js',
    library: ['components', '[name]'],
    libraryTarget: 'umd'
  },
  externals: {
    react: 'umd react',
    'react-dom': 'umd react-dom',
    'prop-types': 'umd prop-types',
    classnames: 'umd classnames'
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /(node_modules|dist)/,
        loader: 'babel-loader',
        options: {
          presets: [require.resolve('babel-preset-react-app')],
        },
        enforce: 'pre',
      }, {
        test: /\.scss$/,
        use: [
          "css-loader",
          "sass-loader"
        ]
      }
    ]
  },
  devtool: 'source-map'
}

我该如何配置它以使捆绑包“重置”或保持不变?

1 个答案:

答案 0 :(得分:-1)

library.filename中,您配置了[name]选项,为您配置的每个条目指定一个特殊名称。通常,仅在使用多个入口点时才使用此参数,我在这里看不到。

重新编译Webpack时,您不必“重置”任何内容。我猜这个问题之后文件大小并没有变得无限大,对吧?

检查一下: https://github.com/webpack/webpack/tree/master/examples/multi-part-library