nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
52 stars 20 forks source link

Are sequential patched supported? #98

Open TheAsda opened 3 weeks ago

TheAsda commented 3 weeks ago

As far as i understand and according to some testing being done if there are multiple multiple hooks attached to the same package the last one will be eventually used in application. Am i right? If so are there any methods to apply sequential patches to the package. The case is that i'm using library that's patched the package i want to patch.

Library version: 1.8.0 Node version: 20.11.1

TheAsda commented 3 weeks ago

So i did some tesing with import and require. Branch with import-in-the-middle: https://github.com/TheAsda/import-in-the-middle-issue/tree/master Branch with require-in-the-middle: https://github.com/TheAsda/import-in-the-middle-issue/tree/require There are two scripts that apply some patching to fastify package. With require everything works as expected: image But the same example with import applies only the latest patch: image I also tried different methods such as using register from the module package, applying patches in single file. The result is the same

TheAsda commented 3 weeks ago

Looks like the issue is with proxy created in https://github.com/DataDog/import-in-the-middle/blob/main/lib/register.js. hook is called with the scoped namespace that is the same every time the hook is called

TheAsda commented 3 weeks ago

So the issue is that in register only setter is used so every time hook is called with the original namespace

TheAsda commented 3 weeks ago

I added getter similar to setter to be able to get the latest namespace after modification image

timfish commented 5 days ago

I added getter similar to setter to be able to get the latest namespace after modification

Could you submit a PR to fix this or do you have a patch I could try?

TheAsda commented 5 days ago

I managed to fix the issue with default import patching but when i tested patching named exports tests failed :( Right now i don't have a solution to this problem