Skip to content

Correctly reset mod._compile after first use.#27

Merged
ariporad merged 3 commits intomasterfrom
correct-revert
Sep 18, 2016
Merged

Correctly reset mod._compile after first use.#27
ariporad merged 3 commits intomasterfrom
correct-revert

Conversation

@danez
Copy link
Owner

@danez danez commented Aug 22, 2016

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.

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this isn't what we want to do, because then the hook will only work once. (It will remove itself.)

Copy link
Owner Author

@danez danez Sep 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Owner Author

@danez danez Sep 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ariporad
Copy link
Collaborator

Hi @danez! Thanks for the PR! Once small thing I'm not sure about, otherwise looks great!

@ariporad
Copy link
Collaborator

LGTM!

@ariporad ariporad merged commit d479008 into danez:master Sep 18, 2016
@danez danez deleted the correct-revert branch March 8, 2017 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants