Correctly reset mod._compile after first use.#27
Conversation
Also try to reset the loader, this alows in test to completely unregister the loader. Also fixed a typo and renamed fakeCompile to compileHook.
| compile = mod._compile; | ||
| mod._compile = function _compile(code /*, filename*/) { | ||
| var newCode; | ||
| mod._compile = compile; |
There was a problem hiding this comment.
I'm pretty sure this isn't what we want to do, because then the hook will only work once. (It will remove itself.)
There was a problem hiding this comment.
Dang, I thought I answered.
Okay let me try to remember why I did this :P
The actual loader will only be called once per file, so will the compile function, as afterwards the result is cached by node. When the loader is reverted and then the same hook added again (in tests for example), the second loader will see mod._compile being the compile function of the first loader and call that one, which is useless as the first loader is reverted. By immediately resetting it will first of all save memory because node can clean the compile function for this file up, as it is not used anymore and this loader chains will be prevented. Should someone clear the node module cache and load the same file again, then a new compile function would be created anyway (and again this chain would happen otherwise). It took me a long time of debugging as far as I remember to understand that compile function will never be called multiple times, even requiring a file twice with clearing cache will lead to a new compile function being created.
I also added a comment in the code.
There was a problem hiding this comment.
And after I finished the PR I remember I also found a comment somewhere from @jamestalmage where it was done this way but too long ago to remember exactly.
|
Hi @danez! Thanks for the PR! Once small thing I'm not sure about, otherwise looks great! |
|
LGTM! |
Also try to reset the loader, this allows in test to completely unregister the loader.
Also fixed a typo and renamed fakeCompile to _compile to be inline with the real function.