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.

[Breaking change] NormalModuleReplacementPlugin resolves modules using old resource's context in 5.49.0

See original GitHub issue

Bug report

What is the current behavior? Using the NormalModuleReplacementPlugin, the context in which imports were resolved inside the replacing resource (newResource) was changed in version 5.49.0.

Previously with 5.48.0 and lower, imports were resolved in context specific to the replacing resource (newResource). In 5.49.0 the resolution context remains the same as the original resource.

perhaps issue is caused by this line https://github.com/webpack/webpack/compare/v5.48.0...v5.49.0#diff-a5db25852bfa94348bb69d94ab95dbef8474d789240b4345cf518e5dd64547ccR249 lib/NormalModule.js at line 249:

super(type, context || getContext(resource), layer);

If the current behavior is a bug, please provide the steps to reproduce. https://codesandbox.io/s/setup-basic-node-js-server-forked-r1d0c?file=/src/index.js

What is the expected behavior? If this is intended/necessary change, then it should be explicitly stated in the documentation for NormalModuleReplacementPlugin and released as a breaking change. Otherwise, it should resolve imports in replacing resource (newResource) using newResource context.

Other relevant information: webpack version: 5.49.0/5.48.0 Node.js version: v14.16.1 Operating System: MacOS BigSur 11.5.1 Additional tools:

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vlazhcommented, Sep 6, 2021

As a workaround it’s needs to set a context of new resource. In my case NormalModuleReplacementPlugin resolves regex in afterResolve phase, so I use createData:

new webpack.NormalModuleReplacementPlugin(regex, (resource) => {
  if (resource.createData) {
    resource.createData.resource = newResource;
    resource.createData.context = path.dirname(newResource);
  }
})

It’s worked for me.

0reactions
webpack-botcommented, Mar 23, 2022

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NormalModuleReplacementPlugin - webpack
The NormalModuleReplacementPlugin allows you to replace resources that ... If newResource is relative, it is resolved relative to the previous resource.
Read more >
Issues with Node Polyfills and webpack.config.js
Module not found: Error: Can't resolve 'crypto' in '.\node_modules\mysql\lib' BREAKING CHANGE: webpack < 5 used to include polyfills for node.js ...
Read more >
How to use the webpack.NormalModuleReplacementPlugin ...
config, 'stubs/lru-cache.js') ), // If you require a missing module and then `npm install` it, you still have // to restart ...
Read more >
webpack: Versions | Openbase
Full version history for webpack including change logs. ... when using lazy-once Context modules; improve handling of context modules with multiple contexts ......
Read more >
NormalModuleReplacementPlugin - webpack 3 documentation
The NormalModuleReplacementPlugin allows you to replace resources that match resourceRegExp with newResource . If newResource is relative, it is resolved ...
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