<p dir="auto"><strong>Do you want to request a <em>feature</em> or report a <em>bug</em>?</strong></p>
<p dir="auto">Bug</p>


<p dir="auto"><strong>What is the current behavior?</strong></p>
<p dir="auto">Webpack 4.5 cannot be configured to create JavaScript files that both: a) bundle shared modules and b) runs them.</p>
<p dir="auto"><strong>If the current behavior is a bug, please provide the steps to reproduce.</strong></p>



<p dir="auto">The following markup shows the use case of a large app that has pages that assumes a babel-polyfilled environment and may depend on either React or jQuery or both. A page often has a corresponding Webpack entry that shouldn't redundantly bundle babel-polyfill, React, jQuery, or the Webpack runtime.</p>
<div class="highlight highlight-text-html-basic notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&lt;html&gt;
  &lt;body&gt;
    &lt;!-- Download and run babel-polyfill, fetch polyfill modules, and webpack runtime. --&gt;
    &lt;script src=&quot;environment.js&quot;&gt;&lt;/script&gt;
    &lt;!-- An ES6 environment should polyfilled --&gt;
    &lt;script&gt;console.assert('Map exists.', typeof Map === 'function');&lt;/script&gt;
    &lt;script&gt;console.assert('Set exists.', typeof Set === 'function');&lt;/script&gt;

    &lt;!-- Download react, react-dom --&gt;
    &lt;script src=&quot;vendor-react.js&quot;&gt;&lt;/script&gt;

    &lt;!-- Download and run jquery, and a module that sets it on window --&gt;
    &lt;script src=&quot;vendor-jquery.js&quot;&gt;&lt;/script&gt;
    &lt;script&gt;console.assert('jQuery exists.', typeof jQuery === 'function');&lt;/script&gt;

    &lt;!-- Download and run page-specific code that may import react or jquery
      without bundling react or jquery in page-a.js --&gt;
    &lt;script src=&quot;page-a.js&quot;&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;
"><pre class="notranslate"><span class="pl-kos">&lt;</span><span class="pl-ent">html</span><span class="pl-kos">&gt;</span>
  <span class="pl-kos">&lt;</span><span class="pl-ent">body</span><span class="pl-kos">&gt;</span>
    <span class="pl-c">&lt;!-- Download and run babel-polyfill, fetch polyfill modules, and webpack runtime. --&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span> <span class="pl-c1">src</span>="<span class="pl-s">environment.js</span>"<span class="pl-kos">&gt;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>
    <span class="pl-c">&lt;!-- An ES6 environment should polyfilled --&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span><span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">assert</span><span class="pl-kos">(</span><span class="pl-s">'Map exists.'</span><span class="pl-kos">,</span> <span class="pl-k">typeof</span> <span class="pl-v">Map</span> <span class="pl-c1">===</span> <span class="pl-s">'function'</span><span class="pl-kos">)</span><span class="pl-kos">;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span><span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">assert</span><span class="pl-kos">(</span><span class="pl-s">'Set exists.'</span><span class="pl-kos">,</span> <span class="pl-k">typeof</span> <span class="pl-v">Set</span> <span class="pl-c1">===</span> <span class="pl-s">'function'</span><span class="pl-kos">)</span><span class="pl-kos">;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>

    <span class="pl-c">&lt;!-- Download react, react-dom --&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span> <span class="pl-c1">src</span>="<span class="pl-s">vendor-react.js</span>"<span class="pl-kos">&gt;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>

    <span class="pl-c">&lt;!-- Download and run jquery, and a module that sets it on window --&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span> <span class="pl-c1">src</span>="<span class="pl-s">vendor-jquery.js</span>"<span class="pl-kos">&gt;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span><span class="pl-smi">console</span><span class="pl-kos">.</span><span class="pl-en">assert</span><span class="pl-kos">(</span><span class="pl-s">'jQuery exists.'</span><span class="pl-kos">,</span> <span class="pl-k">typeof</span> <span class="pl-s1">jQuery</span> <span class="pl-c1">===</span> <span class="pl-s">'function'</span><span class="pl-kos">)</span><span class="pl-kos">;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>

    <span class="pl-c">&lt;!-- Download and run page-specific code that may import react or jquery</span>
<span class="pl-c">      without bundling react or jquery in page-a.js --&gt;</span>
    <span class="pl-kos">&lt;</span><span class="pl-ent">script</span> <span class="pl-c1">src</span>="<span class="pl-s">page-a.js</span>"<span class="pl-kos">&gt;</span><span class="pl-kos">&lt;/</span><span class="pl-ent">script</span><span class="pl-kos">&gt;</span>
  <span class="pl-kos">&lt;/</span><span class="pl-ent">body</span><span class="pl-kos">&gt;</span>
<span class="pl-kos">&lt;/</span><span class="pl-ent">html</span><span class="pl-kos">&gt;</span></pre></div>
<p dir="auto"><strong>What is the expected behavior?</strong></p>
<p dir="auto">Webpack 3 and it's CommonsChunkPlugin supported this feature.</p>
<p dir="auto"><strong>Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.</strong></p>
<p dir="auto">Webpack 4.5, Node 8.2, OS X 10.12.6.</p>
<p dir="auto"><em>updated with the latest versions (29.06.2017)</em><br>
Webpack - 3.0.0<br>
Babel-preset-env - 1.5.2<br>
Node - 8.1.2<br>
OS X - 10.12.5</p>
<p dir="auto"><strong>Current</strong></p>
<p dir="auto">Tree shaking is not removing imports, not used in the app.</p>
<p dir="auto"><strong>Expected</strong></p>
<p dir="auto">Tree shaking, removing not used imports.</p>
<hr>
<p dir="auto">The test case is a simple app with React &amp; React Router v4. <code class="notranslate">Link</code> component is not used, but the whole router package is bundled.</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import React from 'react'
import ReactDOM from 'react-dom'
import { Link } from 'react-router-dom'

ReactDOM.render(
  &lt;p&gt;Hey&lt;/p&gt;,
  document.getElementById('app')
)"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-v">React</span> <span class="pl-k">from</span> <span class="pl-s">'react'</span>
<span class="pl-k">import</span> <span class="pl-v">ReactDOM</span> <span class="pl-k">from</span> <span class="pl-s">'react-dom'</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-v">Link</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'react-router-dom'</span>

<span class="pl-v">ReactDOM</span><span class="pl-kos">.</span><span class="pl-en">render</span><span class="pl-kos">(</span>
  <span class="pl-c1">&lt;</span><span class="pl-ent">p</span><span class="pl-c1">&gt;</span>Hey<span class="pl-c1">&lt;</span><span class="pl-c1">/</span><span class="pl-ent">p</span><span class="pl-c1">&gt;</span><span class="pl-kos">,</span>
  <span class="pl-smi">document</span><span class="pl-kos">.</span><span class="pl-en">getElementById</span><span class="pl-kos">(</span><span class="pl-s">'app'</span><span class="pl-kos">)</span>
<span class="pl-kos">)</span></pre></div>
<p dir="auto"><a href="https://github.com/VladShcherbin/react-rollup-webpack-test">Webpack &amp; Rollup test repo</a></p>