Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does webpack bundle equal peer dependencies multiple times?

Tags:

webpack

Let's say I create an npm package called react-web-component that uses and imports react-dom, like so:

import ReactDOM from 'react-dom';

export default {
  create: function (app, tagName, options) {
    // Some code
    ReactDOM.render(app, mountPoint);
  }
};

I would publish it on npm as react-web-component;

Now I create a second project that uses webpack and react and all the other good stuff and I would use my own npm package like so:

package.json

{
  "devDependencies": {
    "react-web-component": "^1.0.0",
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
}

index.js

import React from 'react';
import ReactDOM from 'react-dom';
import ReactWebComponent from 'react-web-component';
import App from './App';

ReactWebComponent.create(<App />, 'my-react-web-component');

Woud webpack, when it bundles the application bundle ReactDom twice or once? And of the answer is twice, is there any chance to get my project to bundle ReactDom only once?

like image 422
Lukas Avatar asked Aug 10 '17 14:08

Lukas


People also ask

What's the difference between dependencies DevDependencies and Peerdependencies?

A dependency is a library that a project needs to function effectively. DevDependencies are the packages a developer needs during development. A peer dependency specifies that our package is compatible with a particular version of an npm package.

Are peer dependencies optional?

As of npm 7, peer dependencies are automatically installed without requiring the parent package to depend on them, including for optional peer dependencies. This looks weird to me. Common use cases for optional peer dependencies are: providing additional integration with a package if the project uses that package.

What does peer Dependency mean?

Having a peer dependency means that your package needs a dependency that is the same exact dependency as the person installing your package. This is useful for packages like react that need to have a single copy of react-dom that is also used by the person installing it.

Why use peer dependencies?

Peer Dependencies are used to specify that our package is compatible with a specific version of an npm package. Good examples are Angular and React.


1 Answers

Assuming you're using a relatively recent version (webpack 2 or later) of webpack, it seems it will automatically detect and remove the duplicate (i.e. bundle it only once), and for older versions it can be done manually with --optimize-dedupe or new webpack.optimize.DedupePlugin().

Sources: https://github.com/webpack/docs/wiki/optimization#deduplication

Webpack creating duplicate entries for dependencies

Also, it appears Zillow has created a tool to detect duplicate dependencies with different versions, which can sometimes be optimized to use the same version, that tool is here: https://github.com/zillow/webpack-stats-duplicates

Source:
https://www.zillow.com/engineering/webpack-optimize-dependencies/

like image 169
Patrick L Avatar answered Oct 06 '22 20:10

Patrick L