refactor(Webpack): Centralize and refactor Webpack patching
Centralizes Webpack patching around a single thing: Patching the module factories object of the WebpackRequire (wreq.m). This wraps the modules object with a proxy to intercept the addition of new modules, and allow us to decide what to do with them.
A new internal setting was added, eagerPatches, and it decides what to do with the newly added modules. With it enabled, we patch the factories as soon as they are set, and with it disabled we only patch them once they are accessed for the first time.
For correctly handling patching, because of multiple WebpackInstances, and to allow for patching factories only when they are accessed, our proxy defines getters and setters for each module factory added, instead of doing a simple Reflect.set on the module factories object.
Obtaining the main WebpackRequire instance is now also handled by the same way we patch wreq.m, as inside of the wreq.O setter we define logic to check if this wreq instance is the main one, and initialize our references if it is.
Additionally, this PR does a lot of clean up related to the main Webpack patching things. This includes changes to the reporter code (which was also modified to fit the removal of the beforeListeners API), variable names, stringifying of modules and QOL changes for better DevTools analyzing.
I also took the opportunity to correctly document all of the WebpackRequire instance, including full typings of it and docs of its internal methods (all of the ones present in the main Discord WebpackRequire). Our code utilizes the newer and correct typings now.
The patchedFactory wrapper is more future proof now and has a fallback logic if we fail to acquire the main WebpackRequire instance, for whatever the reason is. The first time it is called, it will attempt to use the value of the require argument of the factory as our internal reference to the WebpackRequire.
~~and yes, this completely removes the refactor I did 2 months ago~~
@CodiumAI-Agent /review
@CodiumAI-Agent /improve
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Possible bug |
Add null checks for
| 10 |
Add a check to ensure the target value is not undefined before getting its property descriptorThe condition in
Suggestion importance[1-10]: 9Why: Adding a check to ensure that | 9 | |
Add error handling for cases where
| 9 | |
| Enhancement |
Use a functional approach to combine arrays for better clarity and immutabilityInstead of directly modifying the
Suggestion importance[1-10]: 7Why: Using a functional approach with | 7 |
Improve readability and adopt functional programming practices by using array methodsReplace the src/webpack/patchWebpack.ts [37-40]
Suggestion importance[1-10]: 6Why: The suggestion improves readability and adopts functional programming practices, but the performance impact of this change is minimal, making it a minor enhancement. | 6 | |
| Maintainability |
Improve type safety by replacing
| 7 |
| Best practice |
Use an enum for unconfigurable properties to enhance type safety and maintainabilityConsider using a TypeScript enum for
Suggestion importance[1-10]: 6Why: Using a TypeScript enum for | 6 |
@CodiumAI-Agent /improve
PR Code Suggestions ✨
| Category | Suggestion | Score |
| Possible bug |
Add error handling for the promise to prevent unhandled promise rejectionsConsider adding error handling for the promise returned by scripts/generateReport.ts [358]
Suggestion importance[1-10]: 9Why: This suggestion addresses a potential bug that could cause unhandled promise rejections, which are deprecated and can terminate the Node.js process. Adding error handling improves the robustness of the code. | 9 |
Add error handling around dynamic module loading to prevent runtime crashesThe scripts/generateReport.ts [444]
Suggestion importance[1-10]: 9Why: Adding error handling around the dynamic module loading is crucial to prevent runtime crashes. This suggestion enhances the stability and reliability of the code. | 9 | |
Add a check to ensure
| 9 | |
Handle potential null or undefined values returned by
| 8 | |
| Enhancement |
Remove the redundant
| 7 |
| Maintainability |
Improve type safety by using a more specific interface instead of a generic
| 7 |
Improve variable naming for better code readabilityUse a more descriptive variable name than
Suggestion importance[1-10]: 6Why: This suggestion enhances code readability by using a more descriptive variable name. While it improves maintainability, it is a minor change and does not address any critical issues. | 6 |
I updated the PR description to explain what changed
Superseded by https://github.com/Vendicated/Vencord/pull/2409