import-in-the-middle 2.0.0 → 2.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.0.2](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v2.0.1...import-in-the-middle-v2.0.2) (2026-01-11)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * grammar issue in README.md ([#216](https://github.com/nodejs/import-in-the-middle/issues/216)) ([46e4a2a](https://github.com/nodejs/import-in-the-middle/commit/46e4a2a9ad250c06fb52c9b782370071a6d1f3cc))
9
+ * properly handle internals when specifier matches ([#220](https://github.com/nodejs/import-in-the-middle/issues/220)) ([05e4216](https://github.com/nodejs/import-in-the-middle/commit/05e4216e10d11c7eb996d4124f36e476f3a6d42f))
10
+
11
+ ## [2.0.1](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v2.0.0...import-in-the-middle-v2.0.1) (2025-12-18)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * properly hook submodule package exports ([#215](https://github.com/nodejs/import-in-the-middle/issues/215)) ([a20f47a](https://github.com/nodejs/import-in-the-middle/commit/a20f47a3013105a235f2ba48bc17319f7a57636c))
17
+
3
18
  ## [2.0.0](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.15.0...import-in-the-middle-v2.0.0) (2025-10-14)
4
19
 
5
20
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # import-in-the-middle
2
2
 
3
- **`import-in-the-middle`** is an module loading interceptor inspired by
3
+ **`import-in-the-middle`** is a module loading interceptor inspired by
4
4
  [`require-in-the-middle`](https://npm.im/require-in-the-middle), but
5
5
  specifically for ESM modules. In fact, it can even modify modules after loading
6
6
  time.
package/create-hook.mjs CHANGED
@@ -5,7 +5,10 @@
5
5
  import { URL, fileURLToPath } from 'url'
6
6
  import { inspect } from 'util'
7
7
  import { builtinModules } from 'module'
8
- import { getExports as getExportsImpl } from './lib/get-exports.mjs'
8
+ import {
9
+ getExports as getExportsImpl,
10
+ hasModuleExportsCJSDefault
11
+ } from './lib/get-exports.mjs'
9
12
 
10
13
  const specifiers = new Map()
11
14
  const isWin = process.platform === 'win32'
@@ -231,7 +234,16 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
231
234
  }
232
235
 
233
236
  for (const n of exportNames) {
234
- if (n === 'default' && excludeDefault) continue
237
+ if (excludeDefault) {
238
+ const isDefault = n === 'default' ||
239
+ (
240
+ n === 'module.exports' &&
241
+ context.format === 'commonjs' &&
242
+ hasModuleExportsCJSDefault
243
+ )
244
+
245
+ if (isDefault) continue
246
+ }
235
247
 
236
248
  if (isStarExportLine(n) === true) {
237
249
  const [, modFile] = n.split('* from ')
package/index.js CHANGED
@@ -16,7 +16,7 @@ const {
16
16
 
17
17
  function addHook (hook) {
18
18
  importHooks.push(hook)
19
- toHook.forEach(([name, namespace]) => hook(name, namespace))
19
+ toHook.forEach(([name, namespace, specifier]) => hook(name, namespace, specifier))
20
20
  }
21
21
 
22
22
  function removeHook (hook) {
@@ -118,7 +118,7 @@ function Hook (modules, options, hookFn) {
118
118
  sendModulesToLoader(modules)
119
119
  }
120
120
 
121
- this._iitmHook = (name, namespace) => {
121
+ this._iitmHook = (name, namespace, specifier) => {
122
122
  const filename = name
123
123
  const isBuiltin = name.startsWith('node:')
124
124
  let baseDir
@@ -140,12 +140,16 @@ function Hook (modules, options, hookFn) {
140
140
 
141
141
  if (modules) {
142
142
  for (const moduleName of modules) {
143
- if (moduleName === name) {
143
+ const nameMatch = moduleName === name
144
+ const specMatch = moduleName === specifier
145
+ if (nameMatch || specMatch) {
144
146
  if (baseDir) {
145
147
  if (internals) {
146
148
  name = name + path.sep + path.relative(baseDir, fileURLToPath(filename))
147
149
  } else {
148
- if (!getExperimentalPatchInternals() && !baseDir.endsWith(specifiers.get(filename))) continue
150
+ if (!getExperimentalPatchInternals() && !specMatch && !baseDir.endsWith(specifiers.get(filename))) {
151
+ continue
152
+ }
149
153
  }
150
154
  }
151
155
  callHookFn(hookFn, namespace, name, baseDir)
@@ -7,6 +7,9 @@ import { builtinModules, createRequire } from 'module'
7
7
  import { fileURLToPath, pathToFileURL } from 'url'
8
8
  import { dirname, join } from 'path'
9
9
 
10
+ const nodeMajor = Number(process.versions.node.split('.')[0])
11
+ export const hasModuleExportsCJSDefault = nodeMajor >= 23
12
+
10
13
  let parserInitialized = false
11
14
 
12
15
  function addDefault (arr) {
@@ -138,6 +141,13 @@ async function getCjsExports (url, context, parentLoad, source) {
138
141
  }
139
142
  }))
140
143
 
144
+ // added in node 23 as alias for default in cjs modules
145
+ if (full.has('default') && hasModuleExportsCJSDefault) {
146
+ full.add('module.exports')
147
+ }
148
+
149
+ // we know that it's commonjs at this point, because ESM failed
150
+ context.format = 'commonjs'
141
151
  return full
142
152
  } finally {
143
153
  urlsBeingProcessed.delete(url)
package/lib/register.js CHANGED
@@ -39,8 +39,8 @@ function register (name, namespace, set, get, specifier) {
39
39
  setters.set(namespace, set)
40
40
  getters.set(namespace, get)
41
41
  const proxy = new Proxy(namespace, proxyHandler)
42
- importHooks.forEach(hook => hook(name, proxy))
43
- toHook.push([name, proxy])
42
+ importHooks.forEach(hook => hook(name, proxy, specifier))
43
+ toHook.push([name, proxy, specifier])
44
44
  }
45
45
 
46
46
  let experimentalPatchInternals = false
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "import-in-the-middle",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {