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


<p dir="auto"><strong>What is the current behavior?</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const context = require.context(&quot;./src/&quot;, true, /\.spec\.js$/);

context.keys().forEach(function(key) {
	if (require.resolveWeak(key.replace(&quot;.spec.&quot;, &quot;.&quot;))) {
		context(key);
	}
});"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">context</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">.</span><span class="pl-en">context</span><span class="pl-kos">(</span><span class="pl-s">"./src/"</span><span class="pl-kos">,</span> <span class="pl-c1">true</span><span class="pl-kos">,</span> <span class="pl-pds"><span class="pl-c1">/</span><span class="pl-cce">\.</span>spec<span class="pl-cce">\.</span>js<span class="pl-cce">$</span><span class="pl-c1">/</span></span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-s1">context</span><span class="pl-kos">.</span><span class="pl-en">keys</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">forEach</span><span class="pl-kos">(</span><span class="pl-k">function</span><span class="pl-kos">(</span><span class="pl-s1">key</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
	<span class="pl-k">if</span> <span class="pl-kos">(</span><span class="pl-en">require</span><span class="pl-kos">.</span><span class="pl-en">resolveWeak</span><span class="pl-kos">(</span><span class="pl-s1">key</span><span class="pl-kos">.</span><span class="pl-en">replace</span><span class="pl-kos">(</span><span class="pl-s">".spec."</span><span class="pl-kos">,</span> <span class="pl-s">"."</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>
		<span class="pl-s1">context</span><span class="pl-kos">(</span><span class="pl-s1">key</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
	<span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Result: <code class="notranslate">ERROR: Cannot find module "."</code></p>
<p dir="auto"><strong>If the current behavior is a bug, please provide the steps to reproduce.</strong></p>
<ol dir="auto">
<li>Put e.g. <code class="notranslate">some-component.js</code> file with <code class="notranslate">some-component.spec.js</code> to <code class="notranslate">src</code> dir.</li>
<li>Use the code above in the main module.</li>
<li>Compile</li>
</ol>



<p dir="auto"><strong>What is the expected behavior?</strong><br>
<code class="notranslate">require.resolveWeak</code> should return a falsy value in case of not existed module.</p>
<p dir="auto"><strong>If this is a feature request, what is motivation or use case for changing the behavior?</strong></p>
<p dir="auto"><strong>Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.</strong><br>
Webpack 2.4.1, Mac OS 10.12.4, Node 6.10.1</p>
<p dir="auto"><strong>Do you want to request a <em>feature</em> or report a <em>bug</em>?</strong></p>
<p dir="auto">Request a feature.</p>


<p dir="auto"><strong>What is the current behavior?</strong></p>
<p dir="auto"><strong>If the current behavior is a bug, please provide the steps to reproduce.</strong></p>



<p dir="auto"><strong>What is the expected behavior?</strong></p>
<p dir="auto"><strong>If this is a feature request, what is motivation or use case for changing the behavior?</strong></p>
<p dir="auto">The current <code class="notranslate">include</code> and <code class="notranslate">exclude</code> loader options allow developers to include/exclude particular files, but in many cases its far more useful to include/exclude entire branches in the dependency tree.</p>
<p dir="auto">This will become more and more important (specifically in the case of babel-loader) as more and more browsers begin to natively support ES2015+ features. Right now it's common for module authors to publish the compiled versions of their code, but I don't think that should be considered a best practice going forward.</p>
<p dir="auto">Consider this scenario: if one or more of my module dependencies publish their source files as ES2015, it's much more complicated than it should be to include them in the transpilation step while <em>also</em> excluding all other modules because it requires intimate knowledge of that module's dependency tree.</p>
<p dir="auto">You can whitelist (via <code class="notranslate">include</code>) certain modules, but this won't always work because you need to know the full module path, which is problematic since npm sometimes installs a module directly under <code class="notranslate">node_modules</code> and other times it installs a module in a nested <code class="notranslate">node_modules</code> folder.</p>
<p dir="auto">To solve this issue, I think it'd be nice to add something like an <code class="notranslate">excludeTree</code> option. This option would accept an array of npm module names and exclude any files in that module's source as well as any files in the source of any of its dependencies (via <code class="notranslate">package.json</code>).</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  excludeTree: ['react', 'core-js'],
}"><pre class="notranslate"><span class="pl-kos">{</span>
  <span class="pl-c1">excludeTree</span>: <span class="pl-kos">[</span><span class="pl-s">'react'</span><span class="pl-kos">,</span> <span class="pl-s">'core-js'</span><span class="pl-kos">]</span><span class="pl-kos">,</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto"><code class="notranslate">excludeTree</code> is useful for cases where <em>most</em> of your dependencies need to be transpiled. Today, however, the norm is that most of your dependencies are published as ES5, so in these cases what you generally want to do is something like the following (which assumes a <code class="notranslate">matchesExcludeTree()</code> function exists):</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  exclude: (absPath) =&gt; {
    // Exclude all node modules *unless* they're in the following list
    // of packages (and their dependencies).
    return absPath.includes('node_modules') &amp;&amp;
        !matchesExcludeTree(absPath, ['autotrack']);
  }
}"><pre class="notranslate"><span class="pl-kos">{</span>
  <span class="pl-en">exclude</span>: <span class="pl-kos">(</span><span class="pl-s1">absPath</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-c">// Exclude all node modules *unless* they're in the following list</span>
    <span class="pl-c">// of packages (and their dependencies).</span>
    <span class="pl-k">return</span> <span class="pl-s1">absPath</span><span class="pl-kos">.</span><span class="pl-en">includes</span><span class="pl-kos">(</span><span class="pl-s">'node_modules'</span><span class="pl-kos">)</span> <span class="pl-c1">&amp;&amp;</span>
        <span class="pl-c1">!</span><span class="pl-en">matchesExcludeTree</span><span class="pl-kos">(</span><span class="pl-s1">absPath</span><span class="pl-kos">,</span> <span class="pl-kos">[</span><span class="pl-s">'autotrack'</span><span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Perhaps in this case it makes sense to have <code class="notranslate">exclude</code>/<code class="notranslate">excludeTree</code> work in conjunction with <code class="notranslate">include</code>/<code class="notranslate">includeTree</code>. That way the above imperative logic could be rewritten declaratively like this:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
  exclude: /node_modules/,
  includeTree: 'autotrack'
}"><pre class="notranslate"><span class="pl-kos">{</span>
  <span class="pl-c1">exclude</span>: <span class="pl-pds"><span class="pl-c1">/</span>node_modules<span class="pl-c1">/</span></span><span class="pl-kos">,</span>
  <span class="pl-c1">includeTree</span>: <span class="pl-s">'autotrack'</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">For some additional context as to why this is a problem, see this issue:<br>
<a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="208245680" data-permission-text="Title is private" data-url="https://github.com/googleanalytics/autotrack/issues/137" data-hovercard-type="issue" data-hovercard-url="/googleanalytics/autotrack/issues/137/hovercard" href="https://github.com/googleanalytics/autotrack/issues/137">googleanalytics/autotrack#137</a></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>