I'm working on a webpack plugin and can't figure out how to modify a module during the build. What I'm trying to do:
- Collect data via a custom loader (fine)
- After all modules have been loaded, collect data gathered by my loader (fine)
- Insert code I generate into an existing module in the build (doing this as described below, not sure if it's the best way)
- 'update' that module so that the code I added gets parsed and has its 'require's turned into webpack require calls (can't figure out how to do this correctly)
Currently I'm hooking into 'this-compilation' on the compiler, then 'additional-chunk-assets' on the compilation. Grabbing the first chunk (the only one, currently, as I'm still in development), iterating through the modules in that chunk to find the one I want to modify. Then:
- Appending my generated source to the module's _cachedSource.source._source._value (I also tried appending to the module's ._source._value)
- setting the ._cachedSource.hash to an empty string (as this seems to be necessary for the next step to work)
- I pass the module to .rebuildModule()
It looks like rebuildModule should re-parse the source, re-establish dependencies, etc. etc., but it's not parsing my require statements and changing them to webpack requires. The built file includes my modified source but the require('...') statements are unmodified.
How can I make the module I modified 'update' so that webpack will treat my added source the same as the originally parsed source? Is there something I need to do in addition to rebuildModule()? Am I doing this work too late in the build process? Or am I going about it the wrong way?