0

Will webpack dedupe packages that have already been bundled with webpack?

For example, | Webpack bundle 1 | |------------------| | react@15.5 | | jquery@3.0 |

| Webpack app bundle | |--------------------| | react@15.5 | | jquery@3.1 | | Webpack bundle 1 |

Will Webpack app bundle include 2 copies of react@15.5? Will Webpack app bundle include 2 copies of jquery?

Matt
  • 59
  • 6

1 Answers1

0

It's all due to the configuration that you give to webpack. If you have two entry points that don't communicate each other and both import jquery and react, so the answer is yes, you will have those libraries in both bundles. Although, you can easily create a vendor bundle using the CommonsChunkPlugin. Here's an example of how to put vendors modules inside a separated chunk that will be consumed by both entry points:

new webpack.optimize.CommonsChunkPlugin({
    name: "vendor",
    minChunks: function (module) {
    return module.context && module.context.indexOf("node_modules") !== -1;
  }
})

In this way, all the modules inside node_modules will be exported inside a bundle called vendor.

Ematipico
  • 1,181
  • 1
  • 9
  • 14
  • Thank you for your comment. Let's assume I have no control over bundle 1. My single entry point is the Webpack app bundle. – Matt May 16 '17 at 22:20
  • If you don't any control over the first bundle, so webpack doesn't know that some vendors are already inside the first bundle. What I can suggest is use the `expose-loader` ( [link](https://github.com/webpack-contrib/expose-loader) ) inside the **first bundle** and expose the vendors you need inside the window object. Inside the second bundle, you can use the `externals` ( [link](https://webpack.js.org/configuration/externals/#externals) ) options inside your **second bundle** – Ematipico May 17 '17 at 10:04
  • http://stackoverflow.com/questions/42248952/using-react-components-bundled-with-webpack-causes-duplication-of-submodules <- I think this post answers my question. If a module has already been bundled with webpack, I don't think there is a way to unbundle it to dedupe modules. – Matt May 18 '17 at 14:21