`require.context` not parsed in ESM packages
See original GitHub issueBug report
What is the current behavior?
In packages with type: "module"
, webpack fails to parse and replace require.context()
expressions.
If the current behavior is a bug, please provide the steps to reproduce.
- Clone https://github.com/tavoyne/webpack-2ece8c.
- Run
yarn webpack --mode=production
. - Inspect the
dist/main.js
file and notice that therequire.context()
expression is still present. - (Remove
type: "module"
from thepackage.json
file.) - (Inspect the
dist/main.js
file again and notice how therequire.context()
expression was successfully replaced.)
What is the expected behavior?
The replacement to be successful at step (3).
As require
is not available in ESM environments, it could be called import.context
in order to avoid confusion.
Other relevant information:
webpack version: 5.68.0
Node.js version: 17.4.0
Operating System: MacOS
Additional tools: N/A
Issue Analytics
- State:
- Created 2 years ago
- Comments:16 (8 by maintainers)
Top Results From Across the Web
require.context() alternative when using ES modules
I have a project where I want to use ES modules and use import instead of require . So I have added in...
Read more >Dependency Management - webpack
A context is created if your request contains expressions, so the exact module is not ... Webpack parses the require() call and extracts...
Read more >require-context - npm
Require with an expression.. Latest version: 1.1.0, last published: 5 years ago. Start using require-context in your project by running `npm ...
Read more >API - ESBuild
Tsconfig; Working directory. #Simple options. #Alias. Supported by: Build. This feature lets you substitute one package for another when bundling ...
Read more >rollup.js
UMD format requires a bundle name rollup main.js --file bundle.js --format umd ... file (see example) --no-esModule Do not add __esModule property --exports ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
import.meta.webpackContext
would be the name to choose here.But we should use the chance to improve the API a bit and let it take an object instead of many arguments. This would also allow to add the other options a context takes, like
include
orexclude
ormode
@sokra should we add
import.meta.context
?