Invalid webpack4 tree-shaking

webpack4 tree-shaking, I configure mode according to the official documentation, setting mode to production, but not optimizing my code. My app.js only introduces the a () method of util.js, but when packaging, other methods of util are also packaged, asking for advice,

Mar.13,2021

change modules to false in the js configuration parameter of the configuration file, options: {preset: [['babel-preset-env'], {modules:false}]}


Hello, have you solved this problem? Encountered the same problem

officially configure ides/tree-shaking/" rel=" nofollow noreferrer "> https://www.webpackjs.com/gui.

as follows

I have the same problem. Can you explain in more detail upstairs? I can't find a place.


you may have used OptimizeCSSAssetsPlugin, then add TerserPlugin. The code is as follows:

const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const TerserPlugin = require('terser-webpack-plugin');
...

optimization: {
    minimizer: [
      new OptimizeCSSAssetsPlugin(),
      new TerserPlugin()
    ]
  }

then tree shaking will take effect. Of course, it is also effective to empty, tree shaking of all plug-ins in minimizer.

Menu