Module dependencies set in buildModule hook are getting reset during the build
See original GitHub issueBug report
What is the current behavior?
I’m trying to modify the source of the module based on this comment
So my code looks like this:
compilation.hooks.buildModule.tap('MyPlugin', (module) => {
module.addDependency(new MyDependency())
})
But it’s doesn’t work because in the build method webpack resets dependencies of module https://github.com/webpack/webpack/blob/2151f56ae73b35722c56432c9b34820626d72e53/lib/NormalModule.js#L879
If the current behavior is a bug, please provide the steps to reproduce.
Clone https://github.com/7rulnik/webpack-repro-13361 and run yarn && yarn webpack
What is the expected behavior?
I don’t quite follow the logic of clearDependenciesAndBlocks
, but I thing that it should contain check for dependencies.length
Other relevant information: webpack version: 5.36.2 Node.js version: v14.15.4 Operating System: MacOS 11.3 M1
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Compilation Hooks | webpack
The Compilation module is used by the Compiler to create new compilations (or builds). ... SyncBailHook. Fired at the beginning of dependency optimization....
Read more >Webpack plugin: how can I modify and re-parse a module ...
How I got it working: hook into compilation 's 'seal' plugin, iterate through the compilation's module s and find the one you want ......
Read more >Workflows — Spack 0.11 documentation
Some module systems put a prefix on the names of modules created by Spack. ... reset tic tput $ spack view --verbose --dependencies...
Read more >Dependency Management in Go - GitLab Docs
Go packages must be stored in a version control repository on a VCS server. Dependencies are fetched directly from their VCS server or...
Read more >https://unpkg.com/webpack/types.d.ts
This can be set to 'false' to omit emitting e. g. for SSR. ... Module, Module>; /** * Modules in value are building...
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
@alexander-akait the only reason why I decided to create an issue, not a question in discussions, because that implementation based on Tobias’s own comment. Also, it works in webpack 4 and I didn’t find anything related to this change in changelog. So this is why I raised this issue.
So basically I want to modify a source of modules. I added repro into main post https://github.com/7rulnik/webpack-repro-13361
I also understand that this can be worked around by adding a dependency to
parser.hooks
, for example,ConstDependency
.For anyone who ran into similar problem and wondering how
parser.hooks
can be use to modify module. The trick is accessing module usingparser.state.module
inside aparser.hooks.program.tap(...)