<h1 dir="auto">Bug report</h1>




<p dir="auto"><strong>What is the current behavior?</strong></p>
<p dir="auto">When loading 2 entry points on the same page that share a common module, the module is instantiated twice, even when it's bundled into the commons bundle.</p>
<p dir="auto">We ran into this while upgrading <a href="https://github.com/zeit/next.js">Next.js</a> to webpack 4 and it's currently blocking the upgrade until we know if this is expected behavior.</p>
<p dir="auto"><strong>If the current behavior is a bug, please provide the steps to reproduce.</strong></p>
<p dir="auto">I've created a reproduction for both webpack 3 and webpack 4, also added an explanation about how Next.js handles things and research into the issue:</p>
<p dir="auto"><a href="https://github.com/timneutkens/webpack-instantiation-issue">https://github.com/timneutkens/webpack-instantiation-issue</a></p>






<p dir="auto"><strong>What is the expected behavior?</strong></p>
<p dir="auto">Following webpack 3 there should be only one instance of a shared module. Which is not the case in webpack 4 when having multiple entries on the same page. Though this might be expected.</p>
<p dir="auto">My main question being: is this expected? Should we bind the "singleton" instance to <code class="notranslate">window</code> so that it will be the same across multiple module instances, or is this a regression/bug?</p>


<p dir="auto"><strong>Other relevant information:</strong><br>
webpack version: 4.12.0<br>
Node.js version: any<br>
Operating System: any<br>
Additional tools:</p>
<p dir="auto">I'm working on this larg(ish) project where we currently use AMD. It works all right, we don't build the project in development and load each module individually. It takes about ~2.5 seconds to load the page with ~420 modules/templates.</p>
<p dir="auto">I'm exploring switching to webpack (which so far looks amazing) for all the benefits that would add. I have our entire existing project building with webpack with no modifications to the project, which is awesome.</p>
<p dir="auto">It takes 14s to build the project, but if I use <code class="notranslate">webpack-dev-server</code>, the rebuilds are impressively fast ~1s.</p>
<p dir="auto">However, if I turn on <code class="notranslate">devtool: "source-map"</code>, the rebuilds then take ~4s which is slower than loading all modules individually with Require.js where source maps are not needed.<br>
I've tried using <code class="notranslate">devtool: "eval"</code> and that's fast, but it's not .. a perfect sourcemap (e.g. requires are all numbers now and <code class="notranslate">pathInfo: true</code> doesn't seem to work for me).</p>
<p dir="auto">So, my question really is, why does sourcemap slowdown rebuilds so much and how could I try to go around it. E.g. Should I try to make more smaller chunks? Should I try to move some of the shared dependencies from the chunks into the entry bundle? Am I doing something wrong with having so many megabytes of JavaScript?</p>
<p dir="auto">Here's my current output just to give you the idea of the size of the project.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Version: webpack 1.0.0-rc3
Time: 14180ms
      Asset     Size  Chunks             Chunk Names
  bundle.js   985492       0  [emitted]  main       
1.bundle.js   437111       1  [emitted]             
2.bundle.js   552099       2  [emitted]             
3.bundle.js  1848882       3  [emitted]             
4.bundle.js  2413145       4  [emitted] "><pre class="notranslate"><code class="notranslate">Version: webpack 1.0.0-rc3
Time: 14180ms
      Asset     Size  Chunks             Chunk Names
  bundle.js   985492       0  [emitted]  main       
1.bundle.js   437111       1  [emitted]             
2.bundle.js   552099       2  [emitted]             
3.bundle.js  1848882       3  [emitted]             
4.bundle.js  2413145       4  [emitted] 
</code></pre></div>
<p dir="auto">That's 5.9MB in total at the moment, even though the unuglified r.js build is 3.8MB. That's because there's a lot of code duplication in chunks 1, 2, 3, 4. I'd like to build all modules that are shared at least once into the parent bundle, but haven't figured how to do that yet.</p>