1.0.0 • Published 5 years ago

webpack-why-plugin v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

webpack-why-plugin

Print out why a particular module is included in webpack bundle. 'webpack-bundle-analyzer' allows you to look at the bundle in detail, but it doesn't tell you why a module is included in the bundle.

Example output:

    warning is bundled because:
      warning <= formik
        : src/my-page.tsx
      warning <= @material-ui/core <= @material-ui/icons
      warning <= jss-nested <= @material-ui/core <= @material-ui/icons
      warning <= jss <= @material-ui/core <= @material-ui/icons
      warning <= jss <= jss-global <= @material-ui/core <= @material-ui/icons
        : src/my-page-2.tsx

Install

First add the module:

# yarn add -D webpack-why-plugin

# npm i --dev webpack-why-plugin

In "webpack.config.js", add these lines:

    import { WebpackWhyPlugin } from "webpack-why-plugin";

    module.exports = async function main(env, arg) {
      let config = {
        entry:{
          // ...
        },
        plugins: [
        ]
        // ...
      }
      if (arg.why) {
        config.plugins.push(new WebpackWhyPlugin({names: arg.why}));
      }
      return config;
    }

Usage examples

From command line, run

# npx webpack --why m3
m3 is bundled because:
  m3 <= m1 <= m2
    : src/page1.tsx
    : src/page2.tsx

The output says the src/page1.tsx and src/page2.tsx imported module "m2", "m2" imported "m1", "m1" imported "m3", thus "m3" is in the bundle.

To explain multiple modules, delaminate by comma:

# npx webpack --why m3,m4

To match module names by regex, surround with //

# npx webpack --why /material/