Issue 1:
Bug report

The issue happens in the Theia repository where Webpack emits a bogus bundle in production mode.

I tracked this issue down to the update to webpack-sources: c3a0145

Any version webpack-sources@3 fails our build, previous versions seem to work fine.

What is the current behavior?

Bogus bundle code being emitted, such as:

ffunction then(...

Note the duplicated f from function.

The exact error is:

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)

If the current behavior is a bug, please provide the steps to reproduce.

Building a Theia application has a few prerequisites.

Unless you know how to skip node-gyp building, you'll need to have the proper shared libraries installed on Linux.

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
# <checkpoint>
yarn theia build # this runs webpack in production mode among other things

The above steps should lead to the error.

You can start again from <checkpoint> and edit webpack.config.js to add:

config.optimization = {
minimize: false
};

This will allow you to take a look at the generated bundles. The bogus generation should be in lib/bundle.js line 46850.

What is the expected behavior?

Emitted code to not be bogus, notably the duplicated f character in the previous examples.

Other relevant information:

webpack version: 5.47.0

Node.js version: 12.22.3

Operating System: Windows 10

Additional tools: n/a

Issue 2:
Feature request

What is the expected behavior?

Support a syntax like require.context('./modules', true, /\.js$/, 'lazy', 'myModule') to allow specifying a fixed chunkName.

What is motivation or use case for adding/changing the behavior?

I want to split some code and pack them into a single file about operating dialogs from a enormous module.

require.context() doesn't support specifying a fixed chunkName.

require.ensure() and import() don't support passing expressions as module names.

How should this be implemented in your opinion?

I try to turn webpack\lib\dependencies\RequireContextDependencyParserPlugin.js into a new async-import-plugin. It supports the fifth param which can pass a fixed chunkName. The following is a replaying step.

Clone the repo async-import-plugin, run npm i.

Modify some code about managing chunkName in webpack\lib\ContextModule.js.

Run npm run build, then require.asyncImport('./modules', true, /\.js$/, 'lazy', '$myModule$') in index.js can achieve the target.

Are you willing to work on this yourself?

yes