简体   繁体   中英

Webpack bundle size increases on every consecutive build

I'm packing a project with webpack in order to use it as a library. It's a components library so I'm generating small bundles for every component that lives in its own directory under src/ui . This is a example component:

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

I pretend to generate the bundled component in dist/index.js .

With my current Webpack configuration, if I run NODE_ENV=production webpack --display-modules for example, three consecutive times wihtout changing any code, the Webpack outputs are:

                   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]

As you can see, bundle size increases around a 1 KiB on each execution.

My current webpack configuration is:

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'
}

How can I configure it so bundle 'resets' or stays the same?

In library.filename , you configured the [name] option specifying for every entry you have configured, it'll assign a special name. You would usually only use this parameter if you are using multiple entry points, which I don't see here.

You shouldn't have to "reset" anything when you recompile your Webpack. I'm guessing the file size didn't get infinitely larger after this question, right?

Check this out: https://github.com/webpack/webpack/tree/master/examples/multi-part-library

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM