question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`require.context` not parsed in ESM packages

See original GitHub issue

Bug 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.

  1. Clone https://github.com/tavoyne/webpack-2ece8c.
  2. Run yarn webpack --mode=production.
  3. Inspect the dist/main.js file and notice that the require.context() expression is still present.
  4. (Remove type: "module" from the package.json file.)
  5. (Inspect the dist/main.js file again and notice how the require.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:closed
  • Created 2 years ago
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
sokracommented, Feb 15, 2022

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 or exclude or mode

1reaction
vankopcommented, Feb 11, 2022

@sokra should we add import.meta.context ?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found