Revert "feat: Optionally hook internal paths like require-in-the-middle (#194)"
#236
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.