How to require JavaScript using webpack from a dynamic directory
<p>I have some old-school plain old ES5 JavaScript that I need to pull into my application that is using webpack as a bundler. The problem is that the location of these files is in a folder whose path is dynamic. It seems that webpack does not work well with expressions in <code>require</code> and <code>require.context</code> doesn't allow them at all.</p>

<p>For known path directories to require all the files in a directory and its subdirectories, this approach <strong>works fine</strong>:</p>

<p><code>let domainRequires = require.context('./app/domain', true, /\.js$/);
domainRequires.keys().forEach(domainRequires);</code></p>

<p>Since I can't use an expression in <code>require.context</code>, I tried just using plain <code>require</code> and it is <strong>NOT working</strong>:</p>

<p><code>require('../../path/to/code/my-library-' + versionNumber + '/domain/clazz.js');</code></p>

<p>using the full relative path.</p>

<p>I've also tried to use <code>require.context</code> but enhancing the regex portion to get it working and haven't had luck:</p>

<p><code>require.context('../../path/to/code', true, /^my-library-.*\/domain\/.*\.js$/);</code></p>

<p>Any thoughts, suggestions, or solutions would be welcome. If I need to use something 3rd party with webpack, that's fine too.</p>