webpackwebpack-5tree-shaking

Can't get webpack usedExport / treeshaking to work


Been trying for hours to get webpack to treeshake my code. Here's the repro: https://github.com/dmt0/treeshake-repro

The problem is that even though I'm importing only a few icons from @mdi/js, the whole bundle of 2.6MB gets imported.

Reread the webpack docs multiple times and saw multiple SO questions and GH issues. I do have the following in place:

   "sideEffects": [
    "*.scss",
    "*.css"
  ],

What am I missing?


Solution

  • The problem was in some completely unrelated setting - devtool. No idea why it breaks treeshaking. It was never changed in this config file since some ancient version of webpack. Had to change from this:

      devtool: env.production ? 'hidden-source-map' : 'eval-source-map',
    
    

    to this:

      devtool: argv.mode === 'production' ? 'hidden-source-map' : 'eval-source-map',
    

    Now treeshaking works correctly and my bundle size went down significantly.