title: How to restrict Webpack `require.context` neatly?
body: <p>I am developing a little <a href="https://antwarjs.github.io/" rel="noreferrer">static site generator</a> on top of Webpack and React. Currently I'm making it more dynamic. One part of this is making it more configurable.</p>
  
  <p>Given a site structure like this</p>
  
  <pre><code>.
  ├── _book
  ├── assets
  ├── build
  ├── drafts
  ├── manuscript
  ├── node_modules
  ├── pages
  ├── project_source
  └── styles
  </code></pre>
  
  <p>I would want to require files only from certain directory or directories. In this case it would be enough to require Markdown files from <code>manuscript</code>. Naively <code>var req = require.context('manuscript', true, /^\.\/.*\.md$/)</code> would work.</p>
  
  <p>The problem is that this needs to become dynamic as I pass the directory through site generator configuration. As <code>require.context</code> relies on fixed values I think I need to change context to site root using something like <code>var req = require.context('.', true, /^.*\.md$/)</code> and then check against <code>req.keys()</code> to match against my configuration.</p>
  
  <p>In practice this is extremely slow as it will traverse the whole tree! Especially <code>node_modules</code> can contain a lot of files and this is something that should be avoided at all costs.</p>
  
  <p>Is there a neat way to exclude <code>node_modules</code> out of <code>require.context</code>? I suppose some form of Regex might work although I am open for other ideas.</p>