Skip to content

esm: refactor hooks – add loader instances

Previously we had one array for each kind of chain in the variable #chains; this is probably the most efficient, but it decouples the connection between the original register call and the hook functions themselves.

By adding #loaderInstances we can keep track of each time register is called. This allows, for example, trivially removing the loader later on, or associating shared context with a particular register call. The tradeoff here if we just did this would be that during hook traversal we may iterate over a slightly larger number of objects. Instead we keep #chains but it is now a derived property; every time #loaderInstances changes we simply rebuild #chains.

This sets the stage for doing other things, like adding deregister: https://github.com/nodejs/node/pull/49159 or adding the ability to attach local state to individual loader instances.

N.B. I should probably add back the documentation about the entries in #chains somehow.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

(d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

Merge request reports

Loading