Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does webpack handle multiple files importing the same module React

Tags:

I have a React app which has many components importing the same modules. Does webpack import each module once for each file requesting it, resulting in duplicate code(in this case each import twice for just the two components)? I'm considering re-writing the components so that child components do not need to require the React modules, but maybe I'm solving a problem that doesn't exist. I'd like to avoid many imports of the same react module if it results in duplicate code.

Component 1

import React from "react"; import { Link } from "react-router"; import ReactLogo from "elements/ReactLogo";  export default class MainMenu extends React.Component {     render() {         return <div>             <ReactLogo type="svg" /> <ReactLogo type="png" /> <ReactLogo type="jpg" />             <h2>MainMenu:</h2>             <ul>                 <li>The <Link to="home">home</Link> page.</li>                 <li>Do something on the <Link to="todo">todo page</Link>.</li>                 <li>Switch to <Link to="some-page">some page</Link>.</li>                 <li>Open the chat demo: <Link to="chat" params={{room: "home"}}>Chat</Link>.</li>                 <li>Open the page that shows <Link to="readme">README.md</Link>.</li>             </ul>         </div>;     } } 

Component 2 importing the same 3 modules.

import React from "react"; import { Link } from "react-router"; import ReactLogo from "elements/ReactLogo";  export default class MainMenu extends React.Component {     render() {         return <div>             <ReactLogo type="svg" /> <ReactLogo type="png" /> <ReactLogo type="jpg" />             <h2>MainMenu:</h2>             <ul>                 <li>The <Link to="home">home</Link> page.</li>                 <li>Do something on the <Link to="todo">todo page</Link>.</li>                 <li>Switch to <Link to="some-page">some page</Link>.</li>                 <li>Open the chat demo: <Link to="chat" params={{room: "home"}}>Chat</Link>.</li>                 <li>Open the page that shows <Link to="readme">README.md</Link>.</li>             </ul>         </div>;     } } 
like image 606
byrdr Avatar asked Oct 24 '15 01:10

byrdr


People also ask

How does webpack handle import?

Webpack builds a dependency graph used internally Next step for webpack is to check for everything that is imported from those three files. It does this recursively until it has covered all imported files in the app.

How does webpack dynamic import work?

Whenever you import a file in your code, Webpack will look for it in the project directory and copy it to the build folder with a new name, then Webpack replaces the import code in your bundled JavaScript file with the path to the newly copied file.

Do I need to import React in every file?

You no longer need to import React from "react" . Starting from the release 17 of React, JSX is automatically transformed without using React. createElement . However, other exports like hooks must be imported.


1 Answers

No, webpack (similar to browserify and other module bundlers) will only bundle it once.

Every react component will get its own scope, and when it requires/imports another module, webpack will check if the required file was already included or not in the bundle.

So no, it will not result in duplicate code. However if you import some external packaged libraries, you may have some duplicate code. In that case, you can use Webpack's Deduplication plugin to find these files and deduplicate them. More info here for that: https://github.com/webpack/docs/wiki/optimization#deduplication

like image 57
trekforever Avatar answered Oct 14 '22 00:10

trekforever