Revert "feat: Optionally hook internal paths like require-in-the-middle (#194)"#236
Conversation
…dle` (nodejs#194)" This reverts commit 976d032.
BridgeAR
left a comment
There was a problem hiding this comment.
LGTM, I just wonder if we should remove it as a major version bump.
It was an experimental option and I can't find any sign of anyone using it in public code. If we can confirm this I think it's safe to remove in v2. |
I was leaning on it explicitly being experimental with the note in the README that it could be removed in a minor. That said, I don't have a strong preference. #230 keeps support for Node.js 18 that OTel currently requires, so all good. (There might be some small pain for my work's non-OTel-based agent which currently still has support back to Node.js 14, but that doesn't need to block anything.) |
This reverts commit 976d032.
Summary
This removes the
experimentalPatchInternalsoption that was added in #194. I believe it doesn't solve the issue.Details
If you want to play along, commit 3ed5244 on a separate feature branch (https://github.com/trentm/import-in-the-middle/tree/trentm-clearer-match-block) includes example code demonstrating the issue with
experimentalPatchInternals, that I'll walk through below.Background
The following works. A typical case of hooking top-level import of a module.
Running that:
The original issue
If a user imports the module "main" file by referring to its internal implementation file, then IITM doesn't hook it. Change the above example to
import('c8/index.js')and IITM doesn't hook it.Note: This is the issue that #194 was trying to solve, which I'm inferring from the added test case.
The original motivating issue #185 was a misunderstanding on
internals: trueusage, which was slightly different.The workaround:
experimentalPatchInternalsThis looks like it works:
However,
c8is a commonjs module, so we don't see the problem.The problem with
experimentalPatchInternalsIf the package being hooked: (a) is type=module, (b) has multiple files, and (c) does not use
"exports"(so we can import the "main" implementation file path). Then every file loaded in the package is matched by IITM, and all of them withname === "<the module name>".Given this package:
The run this:
The result is every internal file is matched, and the returned
nameis useless:Using
internals: trueis betterUsing
internals: trueis better:
But still not exactly what RITM does
The equivalent with RITM results in:
RITM does extra work (using
require.resolve(moduleName)) to realize that the loaded ".../a-cjs-module-not-using-exports-field/index.js" file is the package "main" file, and then returns the module name rather than the internal file path.I don't know how IITM could do this.
I'll open a separate issue to discuss this behaviour difference with RITM.