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
72 stars 27 forks source link

Fix loader cyclical dependency bug #36

Closed khanayan123 closed 11 months ago

khanayan123 commented 1 year ago

fixes loader cyclical dependency bug by forgoing module wrapping of file and embedding file contents within the patch instead.

To ensure the patch still works when embedding file content an AST is generated to make modifications to the file contents. The general flow is file content -> AST -> update const named exports to let & add a name to any non named default export -> regenerate file contents using updated AST -> embed the file contents in the patch.

Recast (https://github.com/benjamn/recast) is used as the AST transformer & code generator of choice since it seems to be great at maintaining original file content structure & thus subsequently maintaining original file stack trace. Although the use of source maps might be better.

This PR aims to solve this issue: https://github.com/DataDog/dd-trace-js/issues/3595

khanayan123 commented 1 year ago

Going to work on getting the CI to pass & fix other linting/nitpick issues, but in the meanwhile wanted to open a PR to get opinions on the general approach

khanayan123 commented 1 year ago

If needed we can also use https://swc.rs/ instead which is written in RUST and aims to be faster then recast/acorn.

We can draw inspiration from: https://github.com/DataDog/dd-native-iast-rewriter-js/