<p dir="auto"><strong>I'm submitting a feature request</strong></p>
<p dir="auto"><strong>Webpack version:</strong><br>
2.1</p>
<p dir="auto"><strong>Expected/desired behavior:</strong><br>
Currently it's possible to either load modules by their ID's (e.g. <code class="notranslate">__webpack_require__(123)</code> or create a custom context (<code class="notranslate">require.context(...)</code>) which maps paths to module IDs.</p>
<p dir="auto">It would be great to have a way to create arbitrary "contexts" of modules which were <strong>previously</strong> in the bundle. What I mean by that is creating a simple mapping to resolve arbitrary names (set by the plugin) to actual module IDs, without adding any extra ones.</p>
<p dir="auto">I've managed to hack together something based on the ContextReplacementPlugin, but it's not a good solution, because the modules need to be added manually, plus it duplicates the modules instead of referencing the ones which are already there. I want to add new modules, just be able to access them using an "alternative module ID", stored in the context map.</p>
<p dir="auto">I'd be happy to implement this myself, as a plugin, but I'm not sure what's the best way - the <code class="notranslate">ContextModuleFactory</code> is quite complex. It seems to me, I'd need to duplicate that and just make it much simpler -- unless there's a better way?</p>
<ul dir="auto">
<li><strong>What is the motivation / use case for changing the behavior?</strong><br>
Some applications need to use arbitrary, dynamically generated names for importing modules. This would enable such use cases, given that the names are predictable (e.g. file-paths).</li>
</ul>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const ctxRequire = require.customContext('my-context-name');
const someModule = ctxRequire('my-custom-reference');
const someOtherModule = ctxRequire(dynamicallyGeneratedName);"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">ctxRequire</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">.</span><span class="pl-en">customContext</span><span class="pl-kos">(</span><span class="pl-s">'my-context-name'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">someModule</span> <span class="pl-c1">=</span> <span class="pl-s1">ctxRequire</span><span class="pl-kos">(</span><span class="pl-s">'my-custom-reference'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">someOtherModule</span> <span class="pl-c1">=</span> <span class="pl-s1">ctxRequire</span><span class="pl-kos">(</span><span class="pl-s1">dynamicallyGeneratedName</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">In other words it's like alternative module IDs. I could just hook <code class="notranslate">'before-module-ids'</code> and change the <code class="notranslate">module.id</code> of each module and then use <code class="notranslate">__webpack_require__</code> directly, but that way I'm limiting ourselves to a single set of custom references, while it might make sense to keep multiple aliases for the same module, e.g. with and without the extension, similarly to how the <code class="notranslate">ContextReplacementPlugin</code> works:</p>
<p dir="auto">Given the following Context Map:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="{
    &quot;my-custom-reference&quot;: 123,
    &quot;my-custom-reference.js&quot;: 123
}"><pre class="notranslate"><span class="pl-kos">{</span>
    <span class="pl-s">"my-custom-reference"</span>: <span class="pl-c1">123</span><span class="pl-kos">,</span>
    <span class="pl-s">"my-custom-reference.js"</span>: <span class="pl-c1">123</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Usage:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="const ctxRequire = require.customContext('my-context-name');
const someModule = ctxRequire('my-custom-reference');    // without extension
const sameModule = ctxRequire('my-custom-reference.js'); // with extension"><pre class="notranslate"><span class="pl-k">const</span> <span class="pl-s1">ctxRequire</span> <span class="pl-c1">=</span> <span class="pl-en">require</span><span class="pl-kos">.</span><span class="pl-en">customContext</span><span class="pl-kos">(</span><span class="pl-s">'my-context-name'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-k">const</span> <span class="pl-s1">someModule</span> <span class="pl-c1">=</span> <span class="pl-s1">ctxRequire</span><span class="pl-kos">(</span><span class="pl-s">'my-custom-reference'</span><span class="pl-kos">)</span><span class="pl-kos">;</span>    <span class="pl-c">// without extension</span>
<span class="pl-k">const</span> <span class="pl-s1">sameModule</span> <span class="pl-c1">=</span> <span class="pl-s1">ctxRequire</span><span class="pl-kos">(</span><span class="pl-s">'my-custom-reference.js'</span><span class="pl-kos">)</span><span class="pl-kos">;</span> <span class="pl-c">// with extension</span></pre></div>
<p dir="auto">For adding names I'd imagine the consumer of such plugin could either:</p>
<ul dir="auto">
<li>create a simple loader that doesn't modify the source, but adds the array of aliases for a given context</li>
<li>use a <code class="notranslate">Compilation</code> plugin hook like <code class="notranslate">'normal-module-loader'</code> to create and add the array of aliases given each module</li>
</ul>
<h1 dir="auto">Bug report</h1>




<p dir="auto"><strong>What is the current behavior?</strong><br>
when i use <a href="mailto:quill@1.x">quill@1.x</a>, it work. but 2.0 is not work. it report error is _<em>webpack_require</em> is not defined</p>
<p dir="auto"><strong>If the current behavior is a bug, please provide the steps to reproduce.</strong><br>
compare the version of quill. webpack5 seems not support module:"es6"<br>
the <a href="mailto:quill@1.x">quill@1.x</a> config is<br>
<code class="notranslate">{ test: /\.ts$/, use: [{ loader: 'ts-loader', options: { compilerOptions: { declaration: false, target: 'es5', module: 'commonjs' }, transpileOnly: true } }] }</code><br>
the quill@2 config<br>
<code class="notranslate">test: /\.ts$/, use: [ { loader: 'ts-loader', options: { compilerOptions: { declaration: false, module: 'es6', sourceMap: true, target: 'es6', }, transpileOnly: true, }, }, ],</code></p>






<p dir="auto"><strong>What is the expected behavior?</strong><br>
it can work well</p>


<p dir="auto"><strong>Other relevant information:</strong><br>
webpack version:<br>
Node.js version:<br>
Operating System:<br>
Additional tools:</p>