<h1 dir="auto">Bug report</h1>
<p dir="auto">The issue happens in the <a href="https://github.com/eclipse-theia/theia">Theia repository</a> where Webpack emits a bogus bundle in production mode.</p>
<p dir="auto">I tracked this issue down to the update to <code class="notranslate">webpack-sources</code>: <a class="commit-link" data-hovercard-type="commit" data-hovercard-url="https://github.com/webpack/webpack/commit/c3a0145b49e62fbda7fe1372b87b0cef517a6c7f/hovercard" href="https://github.com/webpack/webpack/commit/c3a0145b49e62fbda7fe1372b87b0cef517a6c7f"><tt>c3a0145</tt></a></p>
<p dir="auto">Any version <code class="notranslate">webpack-sources@3</code> fails our build, previous versions seem to work fine.</p>
<p dir="auto"><strong>What is the current behavior?</strong></p>
<p dir="auto">Bogus bundle code being emitted, such as:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="ffunction then(..."><pre class="notranslate"><span class="pl-s1">ffunction</span> <span class="pl-s1">then</span><span class="pl-kos">(</span>...</pre></div>
<p dir="auto">Note the duplicated <code class="notranslate">f</code> from <code class="notranslate">function</code>.</p>
<p dir="auto">The exact error is:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="ERROR in bundle.js from Terser
Unexpected token: name (then) [webpack://../node_modules/es6-promise/dist/lib/es6-promise/-internal.js:129,4][bundle.js:46850,10]
    at js_error (theia\node_modules\terser\dist\bundle.min.js:548:11)
    at croak (theia\node_modules\terser\dist\bundle.min.js:1272:9)
    at token_error (theia\node_modules\terser\dist\bundle.min.js:1280:9)
    at unexpected (theia\node_modules\terser\dist\bundle.min.js:1286:9)
    at semicolon (theia\node_modules\terser\dist\bundle.min.js:1324:56)
    at simple_statement (theia\node_modules\terser\dist\bundle.min.js:1581:73)
    at statement (theia\node_modules\terser\dist\bundle.min.js:1394:19)
    at _embed_tokens_wrapper (theia\node_modules\terser\dist\bundle.min.js:1337:26)
    at block_ (theia\node_modules\terser\dist\bundle.min.js:2170:20)
    at _function_body (theia\node_modules\terser\dist\bundle.min.js:2082:21)"><pre class="notranslate"><code class="notranslate">ERROR in bundle.js from Terser
Unexpected token: name (then) [webpack://../node_modules/es6-promise/dist/lib/es6-promise/-internal.js:129,4][bundle.js:46850,10]
    at js_error (theia\node_modules\terser\dist\bundle.min.js:548:11)
    at croak (theia\node_modules\terser\dist\bundle.min.js:1272:9)
    at token_error (theia\node_modules\terser\dist\bundle.min.js:1280:9)
    at unexpected (theia\node_modules\terser\dist\bundle.min.js:1286:9)
    at semicolon (theia\node_modules\terser\dist\bundle.min.js:1324:56)
    at simple_statement (theia\node_modules\terser\dist\bundle.min.js:1581:73)
    at statement (theia\node_modules\terser\dist\bundle.min.js:1394:19)
    at _embed_tokens_wrapper (theia\node_modules\terser\dist\bundle.min.js:1337:26)
    at block_ (theia\node_modules\terser\dist\bundle.min.js:2170:20)
    at _function_body (theia\node_modules\terser\dist\bundle.min.js:2082:21)
</code></pre></div>
<p dir="auto"><strong>If the current behavior is a bug, please provide the steps to reproduce.</strong></p>
<p dir="auto">Building a Theia application has a few <a href="https://github.com/eclipse-theia/theia/blob/master/doc/Developing.md#prerequisites">prerequisites</a>.</p>
<p dir="auto">Unless you know how to skip node-gyp building, you'll need to have the proper shared libraries installed on Linux.</p>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="git clone https://github.com/eclipse-theia/theia.git
cd theia
git checkout 3716c0716f4f7fe30ef5b36faff4f85e833b20be
yarn upgrade
yarn # you can CTRL+C once you see linting output...
cd examples/browser
# &lt;checkpoint&gt;
yarn theia build # this runs webpack in production mode among other things"><pre class="notranslate">git clone https://github.com/eclipse-theia/theia.git
<span class="pl-c1">cd</span> theia
git checkout 3716c0716f4f7fe30ef5b36faff4f85e833b20be
yarn upgrade
yarn <span class="pl-c"><span class="pl-c">#</span> you can CTRL+C once you see linting output...</span>
<span class="pl-c1">cd</span> examples/browser
<span class="pl-c"><span class="pl-c">#</span> &lt;checkpoint&gt;</span>
yarn theia build <span class="pl-c"><span class="pl-c">#</span> this runs webpack in production mode among other things</span></pre></div>
<p dir="auto">The above steps should lead to the error.</p>
<p dir="auto">You can start again from <code class="notranslate">&lt;checkpoint&gt;</code> and edit <code class="notranslate">webpack.config.js</code> to add:</p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="config.optimization = {
    minimize: false
};"><pre class="notranslate"><span class="pl-s1">config</span><span class="pl-kos">.</span><span class="pl-c1">optimization</span> <span class="pl-c1">=</span> <span class="pl-kos">{</span>
    <span class="pl-c1">minimize</span>: <span class="pl-c1">false</span>
<span class="pl-kos">}</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">This will allow you to take a look at the generated bundles. The bogus generation should be in <code class="notranslate">lib/bundle.js</code> line 46850.</p>
<p dir="auto"><strong>What is the expected behavior?</strong></p>
<p dir="auto">Emitted code to not be bogus, notably the duplicated <code class="notranslate">f</code> character in the previous examples.</p>
<p dir="auto"><strong>Other relevant information:</strong><br>
webpack version: 5.47.0<br>
Node.js version: 12.22.3<br>
Operating System: Windows 10<br>
Additional tools: n/a</p>
<h2 dir="auto">Feature request</h2>





<p dir="auto"><strong>What is the expected behavior?</strong></p>
<p dir="auto">Support a syntax like <code class="notranslate">require.context('./modules', true, /\.js$/, 'lazy', 'myModule')</code> to allow specifying a fixed chunkName.</p>
<p dir="auto"><strong>What is motivation or use case for adding/changing the behavior?</strong></p>
<p dir="auto">I want to split some code and pack them into a single file about operating dialogs from a enormous module.</p>
<ul dir="auto">
<li><code class="notranslate">require.context()</code> doesn't support specifying a fixed chunkName.</li>
<li><code class="notranslate">require.ensure()</code> and <code class="notranslate">import()</code> don't support passing expressions as module names.</li>
</ul>
<p dir="auto"><strong>How should this be implemented in your opinion?</strong></p>
<p dir="auto">I try to turn <code class="notranslate">webpack\lib\dependencies\RequireContextDependencyParserPlugin.js</code> into a new <a href="https://github.com/yyj08070631/async-import-plugin/blob/c73e2ba651bd861579821d8496838525c685b024/async-import-plugin.js#L13">async-import-plugin</a>. It supports the fifth param which can pass a fixed chunkName. The following is a replaying step.</p>
<ul dir="auto">
<li>Clone the repo <a href="https://github.com/yyj08070631/async-import-plugin/blob/c73e2ba651bd861579821d8496838525c685b024/async-import-plugin.js#L13">async-import-plugin</a>, run <code class="notranslate">npm i</code>.</li>
<li>Modify some code about managing chunkName in <code class="notranslate">webpack\lib\ContextModule.js</code>.<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/27988527/127635622-5d15a6d3-9592-4a02-82c6-ff45d2be467b.png"><img src="https://user-images.githubusercontent.com/27988527/127635622-5d15a6d3-9592-4a02-82c6-ff45d2be467b.png" alt="impicture_20210730_174940" style="max-width: 100%;"></a></li>
<li>Run <code class="notranslate">npm run build</code>, then <code class="notranslate">require.asyncImport('./modules', true, /\.js$/, 'lazy', '$myModule$')</code> in index.js can achieve the target.</li>
</ul>
<p dir="auto"><strong>Are you willing to work on this yourself?</strong><br>
yes</p>