import-in-the-middle 1.15.0 → 2.0.1

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.
Files changed (155) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/{hook.js → create-hook.mjs} +23 -11
  3. package/hook.mjs +1 -1
  4. package/index.js +5 -3
  5. package/lib/{get-esm-exports.js → get-esm-exports.mjs} +3 -5
  6. package/lib/{get-exports.js → get-exports.mjs} +33 -9
  7. package/lib/register.js +2 -2
  8. package/package.json +1 -1
  9. package/.editorconfig +0 -14
  10. package/.eslintrc.yaml +0 -26
  11. package/.release-please-manifest.json +0 -3
  12. package/release-please-config.json +0 -10
  13. package/test/README.md +0 -17
  14. package/test/check-exports/test.mjs +0 -317
  15. package/test/fixtures/a.mjs +0 -7
  16. package/test/fixtures/b.mjs +0 -5
  17. package/test/fixtures/bundle.mjs +0 -4
  18. package/test/fixtures/circular-a.js +0 -18
  19. package/test/fixtures/circular-a.mjs +0 -5
  20. package/test/fixtures/circular-b.js +0 -15
  21. package/test/fixtures/circular-b.mjs +0 -3
  22. package/test/fixtures/cyclical-a.mjs +0 -7
  23. package/test/fixtures/cyclical-b.mjs +0 -6
  24. package/test/fixtures/cyclical-self.mjs +0 -8
  25. package/test/fixtures/duplicate-a.mjs +0 -4
  26. package/test/fixtures/duplicate-b.mjs +0 -1
  27. package/test/fixtures/duplicate-c.mjs +0 -1
  28. package/test/fixtures/duplicate-explicit.mjs +0 -5
  29. package/test/fixtures/duplicate.mjs +0 -5
  30. package/test/fixtures/env.mjs +0 -8
  31. package/test/fixtures/esm-exports.txt +0 -32
  32. package/test/fixtures/executable +0 -6
  33. package/test/fixtures/export-types/declarations.mjs +0 -19
  34. package/test/fixtures/export-types/default-call-expression-renamed.mjs +0 -2
  35. package/test/fixtures/export-types/default-call-expression.mjs +0 -1
  36. package/test/fixtures/export-types/default-class-anon.mjs +0 -1
  37. package/test/fixtures/export-types/default-class.mjs +0 -1
  38. package/test/fixtures/export-types/default-expression-array.mjs +0 -1
  39. package/test/fixtures/export-types/default-expression-num.mjs +0 -1
  40. package/test/fixtures/export-types/default-expression-string.mjs +0 -1
  41. package/test/fixtures/export-types/default-function-anon.mjs +0 -1
  42. package/test/fixtures/export-types/default-function.mjs +0 -1
  43. package/test/fixtures/export-types/default-generator-anon.mjs +0 -1
  44. package/test/fixtures/export-types/default-generator.mjs +0 -1
  45. package/test/fixtures/export-types/fn-default-export.mjs +0 -3
  46. package/test/fixtures/export-types/import-default-export.mjs +0 -2
  47. package/test/fixtures/export-types/list.mjs +0 -12
  48. package/test/fixtures/export-types/variable-default-export.mjs +0 -5
  49. package/test/fixtures/foo.mjs +0 -5
  50. package/test/fixtures/got-alike.mjs +0 -11
  51. package/test/fixtures/import-absolute-after.mjs +0 -10
  52. package/test/fixtures/import-absolute.mjs +0 -18
  53. package/test/fixtures/import-after.mjs +0 -16
  54. package/test/fixtures/import.mjs +0 -23
  55. package/test/fixtures/index.js +0 -1
  56. package/test/fixtures/invalid-identifier.js +0 -8
  57. package/test/fixtures/json-attributes.mjs +0 -5
  58. package/test/fixtures/json.mjs +0 -9
  59. package/test/fixtures/lib/baz.mjs +0 -4
  60. package/test/fixtures/module-exports-cjs-shim.mjs +0 -7
  61. package/test/fixtures/native-modules/aix-ppc64.js +0 -2
  62. package/test/fixtures/native-modules/darwin-arm64.js +0 -1
  63. package/test/fixtures/native-modules/darwin-x64.js +0 -1
  64. package/test/fixtures/native-modules/linux-arm64.js +0 -1
  65. package/test/fixtures/native-modules/linux-ppc64.js +0 -2
  66. package/test/fixtures/native-modules/linux-s390x.js +0 -2
  67. package/test/fixtures/native-modules/linux-x64.js +0 -1
  68. package/test/fixtures/native-modules/win32-arm64.js +0 -1
  69. package/test/fixtures/native-modules/win32-x64.js +0 -1
  70. package/test/fixtures/nested-folder/specifier.js +0 -1
  71. package/test/fixtures/package.json +0 -17
  72. package/test/fixtures/re-export-cjs-built-in.js +0 -1
  73. package/test/fixtures/re-export-cjs-json.js +0 -1
  74. package/test/fixtures/re-export-cjs.js +0 -1
  75. package/test/fixtures/re-export-star-external.mjs +0 -1
  76. package/test/fixtures/reexport.js +0 -15
  77. package/test/fixtures/require-root.js +0 -1
  78. package/test/fixtures/say-hi.mts +0 -3
  79. package/test/fixtures/something.js +0 -10
  80. package/test/fixtures/something.json +0 -3
  81. package/test/fixtures/something.mjs +0 -9
  82. package/test/fixtures/something.mts +0 -7
  83. package/test/fixtures/specifier-external.js +0 -1
  84. package/test/fixtures/specifier-string.js +0 -1
  85. package/test/fixtures/specifier.mjs +0 -1
  86. package/test/fixtures/sub-directory/re-export-star-external.mjs +0 -1
  87. package/test/generic-loader.mjs +0 -14
  88. package/test/get-esm-exports/v18.19-get-esm-exports.js +0 -3
  89. package/test/get-esm-exports/v20-get-esm-exports.js +0 -31
  90. package/test/hook/circular-imports.mjs +0 -11
  91. package/test/hook/cyclical-self.mjs +0 -4
  92. package/test/hook/default-export.mjs +0 -86
  93. package/test/hook/define-property.js +0 -20
  94. package/test/hook/duplicate-exports-explicit.mjs +0 -13
  95. package/test/hook/duplicate-exports.mjs +0 -19
  96. package/test/hook/dynamic-import-default.js +0 -29
  97. package/test/hook/dynamic-import-default.mjs +0 -23
  98. package/test/hook/dynamic-import.js +0 -27
  99. package/test/hook/dynamic-import.mjs +0 -27
  100. package/test/hook/import-reexport-cjs.mjs +0 -11
  101. package/test/hook/loader.mjs +0 -5
  102. package/test/hook/module-toStringTag.mjs +0 -25
  103. package/test/hook/re-export-cjs.mjs +0 -26
  104. package/test/hook/re-export-star-module.mjs +0 -17
  105. package/test/hook/remove.mjs +0 -23
  106. package/test/hook/require-root-cjs.mjs +0 -12
  107. package/test/hook/specifier-external-imports.mjs +0 -11
  108. package/test/hook/specifier-imports-mjs.mjs +0 -10
  109. package/test/hook/specifier-imports.mjs +0 -11
  110. package/test/hook/specifier-string-imports.mjs +0 -11
  111. package/test/hook/static-import-default.mjs +0 -26
  112. package/test/hook/static-import-disabled.mjs +0 -15
  113. package/test/hook/static-import-package-internals-enabled.mjs +0 -15
  114. package/test/hook/static-import-package-internals.mjs +0 -15
  115. package/test/hook/static-import-package.mjs +0 -15
  116. package/test/hook/static-import-star.mjs +0 -34
  117. package/test/hook/static-import.mjs +0 -24
  118. package/test/hook/v14-date-fns.mjs +0 -10
  119. package/test/hook/v14-declaration-exports.mjs +0 -84
  120. package/test/hook/v14-double-hook.mjs +0 -33
  121. package/test/hook/v14-native-modules.mjs +0 -10
  122. package/test/hook/v14-react-email-components.mjs +0 -16
  123. package/test/hook/v16-fake-cjs-export.mjs +0 -4
  124. package/test/hook/v16-got.mjs +0 -16
  125. package/test/hook/v16-invalid-identifier.mjs +0 -5
  126. package/test/hook/v18-v21-static-import-assert.mjs +0 -15
  127. package/test/hook/v18.19-openai.mjs +0 -16
  128. package/test/hook/v18.19-static-import-gotalike.mjs +0 -26
  129. package/test/hook/v20.10-static-import-attributes.mjs +0 -15
  130. package/test/hook/v24-typescript.mjs +0 -8
  131. package/test/hook/vue-server-renderer.mjs +0 -8
  132. package/test/low-level/dynamic-import-default.js +0 -23
  133. package/test/low-level/dynamic-import-default.mjs +0 -23
  134. package/test/low-level/dynamic-import.js +0 -27
  135. package/test/low-level/remove.mjs +0 -25
  136. package/test/low-level/sanitized-url.mjs +0 -11
  137. package/test/low-level/static-import-default.mjs +0 -20
  138. package/test/low-level/static-import-disabled.mjs +0 -15
  139. package/test/low-level/static-import.mjs +0 -24
  140. package/test/multiple-loaders/multiple-loaders.test.mjs +0 -6
  141. package/test/multiple-loaders/typescript-loader.mjs +0 -26
  142. package/test/other/v14-assert-cyclical-dependency-failure.mjs +0 -36
  143. package/test/other/v20.10-import-executable.mjs +0 -8
  144. package/test/register/v18.19-circular.mjs +0 -17
  145. package/test/register/v18.19-exclude-regex.mjs +0 -16
  146. package/test/register/v18.19-exclude.mjs +0 -15
  147. package/test/register/v18.19-experimental-patch-internals.mjs +0 -14
  148. package/test/register/v18.19-include-builtin.mjs +0 -20
  149. package/test/register/v18.19-include-message-port-absolute-path.mjs +0 -14
  150. package/test/register/v18.19-include-message-port.mjs +0 -14
  151. package/test/register/v18.19-include.mjs +0 -16
  152. package/test/typescript/iitm-ts-node-loader.mjs +0 -18
  153. package/test/typescript/ts-node.test.mts +0 -19
  154. package/test/version-check.js +0 -33
  155. package/tsconfig.json +0 -14
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## [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)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * 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))
9
+
10
+ ## [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)
11
+
12
+
13
+ ### ⚠ BREAKING CHANGES
14
+
15
+ Converting all modules running in the loader thread to ESM should not be a
16
+ breaking change for most users since it primarily affects internal implementation
17
+ details. However, if you were referencing internal CJS files like `hook.js` this will no longer work.
18
+
19
+ ### Features
20
+
21
+ * convert all modules running in loader thread to ESM ([#210](https://github.com/nodejs/import-in-the-middle/issues/210)) ([da7c7a6](https://github.com/nodejs/import-in-the-middle/commit/da7c7a6904a40bf394b7b2a271a2838711c5417c))
22
+
3
23
  ## [1.15.0](https://github.com/nodejs/import-in-the-middle/compare/import-in-the-middle-v1.14.4...import-in-the-middle-v1.15.0) (2025-10-09)
4
24
 
5
25
 
@@ -2,9 +2,14 @@
2
2
  //
3
3
  // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4
4
 
5
- const { URL, fileURLToPath } = require('url')
6
- const { inspect } = require('util')
7
- const { builtinModules } = require('module')
5
+ import { URL, fileURLToPath } from 'url'
6
+ import { inspect } from 'util'
7
+ import { builtinModules } from 'module'
8
+ import {
9
+ getExports as getExportsImpl,
10
+ hasModuleExportsCJSDefault
11
+ } from './lib/get-exports.mjs'
12
+
8
13
  const specifiers = new Map()
9
14
  const isWin = process.platform === 'win32'
10
15
  let experimentalPatchInternals = false
@@ -17,15 +22,15 @@ const NODE_MAJOR = Number(NODE_VERSION[0])
17
22
  const NODE_MINOR = Number(NODE_VERSION[1])
18
23
  const HANDLED_FORMATS = new Set(['builtin', 'module', 'commonjs'])
19
24
 
20
- let entrypoint
21
-
22
25
  let getExports
23
26
  if (NODE_MAJOR >= 20 || (NODE_MAJOR === 18 && NODE_MINOR >= 19)) {
24
- getExports = require('./lib/get-exports.js')
27
+ getExports = getExportsImpl
25
28
  } else {
26
29
  getExports = (url) => import(url).then(Object.keys)
27
30
  }
28
31
 
32
+ let entrypoint
33
+
29
34
  function hasIitm (url) {
30
35
  try {
31
36
  return new URL(url).searchParams.has('iitm')
@@ -35,7 +40,7 @@ function hasIitm (url) {
35
40
  }
36
41
 
37
42
  function isIitm (url, meta) {
38
- return url === meta.url || url === meta.url.replace('hook.mjs', 'hook.js')
43
+ return url === meta.url || url === meta.url.replace('hook.mjs', 'create-hook.mjs')
39
44
  }
40
45
 
41
46
  function deleteIitm (url) {
@@ -229,7 +234,16 @@ async function processModule ({ srcUrl, context, parentGetSource, parentResolve,
229
234
  }
230
235
 
231
236
  for (const n of exportNames) {
232
- 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
+ }
233
247
 
234
248
  if (isStarExportLine(n) === true) {
235
249
  const [, modFile] = n.split('* from ')
@@ -284,7 +298,7 @@ function addIitm (url) {
284
298
  return needsToAddFileProtocol(urlObj) ? 'file:' + urlObj.href : urlObj.href
285
299
  }
286
300
 
287
- function createHook (meta) {
301
+ export function createHook (meta) {
288
302
  let cachedResolve
289
303
  const iitmURL = new URL('lib/register.js', meta.url).toString()
290
304
  let includeModules, excludeModules
@@ -494,5 +508,3 @@ register(${JSON.stringify(realUrl)}, _, set, get, ${JSON.stringify(specifiers.ge
494
508
  }
495
509
  }
496
510
  }
497
-
498
- module.exports = { createHook }
package/hook.mjs CHANGED
@@ -2,7 +2,7 @@
2
2
  //
3
3
  // This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4
4
 
5
- import { createHook } from './hook.js'
5
+ import { createHook } from './create-hook.mjs'
6
6
 
7
7
  const { initialize, load, resolve, getFormat, getSource } = createHook(import.meta)
8
8
 
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,7 +140,9 @@ function Hook (modules, options, hookFn) {
140
140
 
141
141
  if (modules) {
142
142
  for (const moduleName of modules) {
143
- if (moduleName === name) {
143
+ if (moduleName === specifier) {
144
+ callHookFn(hookFn, namespace, name, baseDir)
145
+ } else if (moduleName === name) {
144
146
  if (baseDir) {
145
147
  if (internals) {
146
148
  name = name + path.sep + path.relative(baseDir, fileURLToPath(filename))
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
- const { Parser } = require('acorn')
4
- const { importAttributesOrAssertions } = require('acorn-import-attributes')
3
+ import { Parser } from 'acorn'
4
+ import { importAttributesOrAssertions } from 'acorn-import-attributes'
5
5
 
6
6
  const acornOpts = {
7
7
  ecmaVersion: 'latest',
@@ -32,7 +32,7 @@ function warn (txt) {
32
32
  * @returns {Set<string>} The identifiers exported by the module along with any
33
33
  * custom directives.
34
34
  */
35
- function getEsmExports (moduleSource) {
35
+ export default function getEsmExports (moduleSource) {
36
36
  const exportedNames = new Set()
37
37
  const tree = parser.parse(moduleSource, acornOpts)
38
38
  for (const node of tree.body) {
@@ -114,5 +114,3 @@ function parseSpecifiers (node, exportedNames) {
114
114
  }
115
115
  }
116
116
  }
117
-
118
- module.exports = getEsmExports
@@ -1,11 +1,16 @@
1
1
  'use strict'
2
2
 
3
- const getEsmExports = require('./get-esm-exports.js')
4
- const { parse: parseCjs } = require('cjs-module-lexer')
5
- const { readFileSync, existsSync } = require('fs')
6
- const { builtinModules } = require('module')
7
- const { fileURLToPath, pathToFileURL } = require('url')
8
- const { dirname, join } = require('path')
3
+ import getEsmExports from './get-esm-exports.mjs'
4
+ import { parse as parseCjs, init as parserInit } from 'cjs-module-lexer'
5
+ import { readFileSync, existsSync } from 'fs'
6
+ import { builtinModules, createRequire } from 'module'
7
+ import { fileURLToPath, pathToFileURL } from 'url'
8
+ import { dirname, join } from 'path'
9
+
10
+ const nodeMajor = Number(process.versions.node.split('.')[0])
11
+ export const hasModuleExportsCJSDefault = nodeMajor >= 23
12
+
13
+ let parserInitialized = false
9
14
 
10
15
  function addDefault (arr) {
11
16
  return new Set(['default', ...arr])
@@ -14,9 +19,15 @@ function addDefault (arr) {
14
19
  // Cached exports for Node built-in modules
15
20
  const BUILT_INS = new Map()
16
21
 
22
+ let require
23
+
17
24
  function getExportsForNodeBuiltIn (name) {
18
25
  let exports = BUILT_INS.get()
19
26
 
27
+ if (!require) {
28
+ require = createRequire(import.meta.url)
29
+ }
30
+
20
31
  if (!exports) {
21
32
  exports = new Set(addDefault(Object.keys(require(name))))
22
33
  BUILT_INS.set(name, exports)
@@ -90,6 +101,10 @@ async function getCjsExports (url, context, parentLoad, source) {
90
101
  urlsBeingProcessed.add(url)
91
102
 
92
103
  try {
104
+ if (!parserInitialized) {
105
+ await parserInit()
106
+ parserInitialized = true
107
+ }
93
108
  const result = parseCjs(source)
94
109
  const full = addDefault(result.exports)
95
110
 
@@ -110,6 +125,10 @@ async function getCjsExports (url, context, parentLoad, source) {
110
125
  [url, re] = resolved
111
126
  }
112
127
 
128
+ // Resolve the re-exported module relative to the current module.
129
+ if (!require) {
130
+ require = createRequire(import.meta.url)
131
+ }
113
132
  const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href
114
133
 
115
134
  if (newUrl.endsWith('.node') || newUrl.endsWith('.json')) {
@@ -122,6 +141,13 @@ async function getCjsExports (url, context, parentLoad, source) {
122
141
  }
123
142
  }))
124
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'
125
151
  return full
126
152
  } finally {
127
153
  urlsBeingProcessed.delete(url)
@@ -144,7 +170,7 @@ async function getCjsExports (url, context, parentLoad, source) {
144
170
  * Please see {@link getEsmExports} for caveats on special identifiers that may
145
171
  * be included in the result set.
146
172
  */
147
- async function getExports (url, context, parentLoad) {
173
+ export async function getExports (url, context, parentLoad) {
148
174
  // `parentLoad` gives us the possibility of getting the source
149
175
  // from an upstream loader. This doesn't always work though,
150
176
  // so later on we fall back to reading it from disk.
@@ -188,5 +214,3 @@ async function getExports (url, context, parentLoad) {
188
214
  throw err
189
215
  }
190
216
  }
191
-
192
- module.exports = getExports
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": "1.15.0",
3
+ "version": "2.0.1",
4
4
  "description": "Intercept imports in Node.js",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/.editorconfig DELETED
@@ -1,14 +0,0 @@
1
- # http://editorconfig.org
2
-
3
- root = true
4
-
5
- [*]
6
- indent_style = space
7
- indent_size = 2
8
- end_of_line = lf
9
- charset = utf-8
10
- trim_trailing_whitespace = true
11
- insert_final_newline = true
12
-
13
- [*.md]
14
- trim_trailing_whitespace = false
package/.eslintrc.yaml DELETED
@@ -1,26 +0,0 @@
1
- overrides:
2
- - files:
3
- - '**/*.{js,cjs,mjs}'
4
-
5
- parser: '@babel/eslint-parser'
6
- parserOptions:
7
- ecmaVersion: latest
8
- requireConfigFile: false
9
- sourceType: 'script'
10
- babelOptions:
11
- plugins:
12
- - '@babel/plugin-syntax-import-assertions'
13
-
14
- rules:
15
- "import/first": off
16
-
17
- extends:
18
- - "standard"
19
-
20
- ignorePatterns:
21
- - test/fixtures/circular-a.js
22
- - test/fixtures/circular-b.js
23
- - test/fixtures/reexport.js
24
- - test/fixtures/duplicate-explicit.mjs
25
- - test/fixtures/duplicate.mjs
26
- - test/fixtures/export-types/default-call-expression-renamed.mjs
@@ -1,3 +0,0 @@
1
- {
2
- ".": "1.15.0"
3
- }
@@ -1,10 +0,0 @@
1
- {
2
- "$schema": "https://raw.githubusercontent.com/googleapis/release-please/main/schemas/config.json",
3
- "bootstrap-sha": "57d2459748492abd55e04a679d8b5752b90dfab4",
4
- "packages": {
5
- ".": {
6
- "pull-request-title-pattern": "chore: release v${version}",
7
- "release-type": "node"
8
- }
9
- }
10
- }
package/test/README.md DELETED
@@ -1,17 +0,0 @@
1
- These tests are organized as follows:
2
-
3
- * Located in the `hook` directory if they use the `Hook` class.
4
- * Located in the `low-level` directory if they use the "low-level" API,
5
- `addHook` and `removeHook`.
6
- * Other tests are in other adjacent directories.
7
-
8
- The tests can be run individually as Node.js programs with non-zero exit codes
9
- upon failures. They should be run with the following Node.js command-line
10
- options (assuming they're run from the project root):
11
-
12
- ```
13
- --require ./test/version-check.js
14
- --experimental-loader ./test/generic-loader.mjs
15
- ```
16
-
17
- The entire test suite can be run with `npm test`.
@@ -1,317 +0,0 @@
1
- import { spawnSync } from 'child_process'
2
- import { deepStrictEqual } from 'assert'
3
- import { dirname, resolve } from 'path'
4
- import { fileURLToPath } from 'url'
5
-
6
- const cwd = dirname(fileURLToPath(import.meta.url))
7
- const hook = resolve(cwd, '..', '..', 'hook.mjs')
8
-
9
- const mostPopular240NpmModules = [
10
- 'ansi-styles',
11
- 'semver',
12
- 'supports-color',
13
- 'chalk', 'has-flag',
14
- 'debug',
15
- 'tslib',
16
- 'color-convert',
17
- 'ms',
18
- 'color-name',
19
- 'lru-cache',
20
- 'minimatch',
21
- 'strip-ansi',
22
- 'source-map',
23
- 'ansi-regex',
24
- 'glob',
25
- 'readable-stream',
26
- 'commander',
27
- 'yallist',
28
- 'string-width',
29
- 'escape-string-regexp',
30
- 'brace-expansion',
31
- 'find-up',
32
- 'p-locate',
33
- 'locate-path',
34
- 'wrap-ansi',
35
- 'p-limit',
36
- 'safe-buffer',
37
- 'kind-of',
38
- 'minipass',
39
- 'uuid',
40
- 'string_decoder',
41
- 'ajv',
42
- 'emoji-regex',
43
- 'isarray',
44
- 'react-is',
45
- 'fs-extra',
46
- 'is-fullwidth-code-point',
47
- 'get-stream',
48
- 'json-schema-traverse',
49
- 'yargs-parser',
50
- 'glob-parent',
51
- 'yargs',
52
- 'rimraf',
53
- 'acorn',
54
- 'which',
55
- 'estraverse',
56
- 'js-yaml',
57
- 'path-exists',
58
- 'argparse',
59
- 'pretty-format',
60
- 'resolve-from',
61
- 'cliui',
62
- 'schema-utils',
63
- 'globals',
64
- 'camelcase',
65
- 'execa',
66
- 'punycode',
67
- 'path-key',
68
- 'signal-exit',
69
- 'inherits',
70
- 'resolve',
71
- 'mkdirp',
72
- 'is-stream',
73
- 'ws',
74
- 'universalify',
75
- 'qs',
76
- 'slash',
77
- 'json5',
78
- 'iconv-lite',
79
- 'form-data',
80
- 'is-number',
81
- 'eslint-visitor-keys',
82
- '@jest/types',
83
- 'postcss',
84
- 'make-dir',
85
- 'pify',
86
- 'cross-spawn',
87
- 'braces',
88
- 'whatwg-url',
89
- 'fill-range',
90
- 'eslint-scope',
91
- 'tr46',
92
- 'micromatch',
93
- 'convert-source-map',
94
- 'define-property',
95
- 'agent-base',
96
- 'shebang-regex',
97
- 'shebang-command',
98
- 'mimic-fn',
99
- 'globby',
100
- 'npm-run-path',
101
- 'mime',
102
- '@babel/code-frame',
103
- 'extend-shallow',
104
- 'to-regex-range',
105
- 'onetime',
106
- 'https-proxy-agent',
107
- 'y18n',
108
- 'buffer',
109
- 'strip-bom',
110
- 'is-glob',
111
- 'doctrine',
112
- 'picocolors',
113
- 'pkg-dir',
114
- '@babel/types',
115
- 'regenerator-runtime',
116
- 'human-signals',
117
- '@jridgewell/trace-mapping',
118
- 'ignore',
119
- 'jsesc',
120
- 'parse-json',
121
- 'jest-worker',
122
- 'graceful-fs',
123
- 'jest-util',
124
- 'jsonfile',
125
- 'normalize-path',
126
- 'strip-json-comments',
127
- 'cosmiconfig',
128
- 'minimist',
129
- 'path-type',
130
- '@babel/parser',
131
- 'balanced-match',
132
- 'picomatch',
133
- 'typescript',
134
- 'isexe',
135
- 'statuses',
136
- 'entities',
137
- 'bytes',
138
- 'node-fetch',
139
- 'http-errors',
140
- '@babel/highlight',
141
- '@babel/helper-validator-identifier',
142
- 'function-bind',
143
- 'async',
144
- 'sprintf-js',
145
- '@babel/generator',
146
- 'is-extendable',
147
- 'get-intrinsic',
148
- 'lodash',
149
- 'mime-db',
150
- 'source-map-support',
151
- 'mime-types',
152
- 'is-arrayish',
153
- '@babel/core',
154
- 'once',
155
- 'anymatch',
156
- 'depd',
157
- 'hosted-git-info',
158
- 'path-to-regexp',
159
- 'axios',
160
- 'is-core-module',
161
- '@babel/template',
162
- 'cookie',
163
- 'write-file-atomic',
164
- 'js-tokens',
165
- '@typescript-eslint/typescript-estree',
166
- '@typescript-eslint/types',
167
- 'object-inspect',
168
- 'wrappy',
169
- 'is-extglob',
170
- 'chokidar',
171
- '@typescript-eslint/visitor-keys',
172
- 'call-bind',
173
- 'loader-utils',
174
- 'browserslist',
175
- 'http-proxy-agent',
176
- 'fast-glob',
177
- 'concat-map',
178
- 'inflight',
179
- 'ajv-keywords',
180
- 'ansi-escapes',
181
- 'ci-info',
182
- 'fast-deep-equal',
183
- 'caniuse-lite',
184
- 'fs.realpath',
185
- '@jridgewell/gen-mapping',
186
- 'setprototypeof',
187
- 'strip-final-newline',
188
- 'optionator',
189
- 'path-is-absolute',
190
- '@babel/traverse',
191
- 'core-util-is',
192
- 'has-symbols',
193
- 'yocto-queue',
194
- 'p-try',
195
- 'electron-to-chromium',
196
- '@smithy/smithy-client',
197
- 'yaml',
198
- 'ini',
199
- '@babel/helper-plugin-utils',
200
- 'jest-get-type',
201
- 'type-check',
202
- 'levn',
203
- 'is-descriptor',
204
- 'prelude-ls',
205
- 'slice-ansi',
206
- '@typescript-eslint/scope-manager',
207
- 'isobject',
208
- 'esprima',
209
- '@babel/helper-split-export-declaration',
210
- 'callsites',
211
- 'readdirp',
212
- 'escalade',
213
- 'import-fresh',
214
- 'get-caller-file',
215
- '@jridgewell/sourcemap-codec',
216
- 'acorn-walk',
217
- 'rxjs',
218
- 'ieee754',
219
- 'is-plain-obj',
220
- 'istanbul-lib-instrument',
221
- '@babel/helper-module-imports',
222
- 'side-channel',
223
- 'normalize-package-data',
224
- 'is-plain-object',
225
- '@jridgewell/resolve-uri',
226
- 'follow-redirects',
227
- 'array-union',
228
- 'json-parse-even-better-errors',
229
- 'path-parse',
230
- 'has-property-descriptors',
231
- 'uri-js',
232
- 'safer-buffer',
233
- '@babel/helpers',
234
- 'on-finished',
235
- '@babel/helper-function-name',
236
- 'p-map',
237
- 'postcss-value-parser',
238
- 'indent-string',
239
- '@babel/helper-module-transforms',
240
- 'object-assign',
241
- 'delayed-stream',
242
- '@nodelib/fs.stat',
243
- 'require-directory',
244
- 'diff',
245
- 'parse5',
246
- 'asynckit',
247
- 'tmp',
248
- 'combined-stream'
249
- ]
250
-
251
- const otherCommonModulesUsedWithInstrumentation = [
252
- 'express',
253
- 'fastify',
254
- '@hapi/hapi',
255
- 'connect',
256
- 'svelte',
257
- '@sveltejs/kit',
258
- 'next',
259
- 'gatsby',
260
- '@remix-run/node',
261
- '@remix-run/react'
262
- ]
263
-
264
- const modules = [...mostPopular240NpmModules, ...otherCommonModulesUsedWithInstrumentation]
265
-
266
- function installLibs (names) {
267
- spawnSync('npm', ['init', '-y'], { cwd })
268
- spawnSync('npm', ['install', ...names], { cwd })
269
- }
270
-
271
- function getExports (name, loader) {
272
- const args = ['--input-type=module', '--no-warnings', '-e', `import * as lib from '${name}'; console.log(JSON.stringify(Object.keys(lib)))`]
273
- if (loader) args.push(loader)
274
- const out = spawnSync(process.execPath, args, { cwd })
275
- if (out.status !== 0) {
276
- console.error(out.stderr.toString())
277
- throw new Error(`Getting exports returned non-zero exit code '${name}'`)
278
- }
279
- const stdout = out.stdout.toString()
280
- return JSON.parse(stdout).sort()
281
- }
282
-
283
- const NPM_LIST_SEMVER_PARSE = /└──.*@((0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?)/
284
-
285
- function getVersion (name) {
286
- const result = spawnSync('npm', ['list', name, '--depth', '0'], { cwd })
287
- const stdout = result.output.toString()
288
- const [, version] = stdout.match(NPM_LIST_SEMVER_PARSE)
289
- return version
290
- }
291
-
292
- function testLib (name) {
293
- const version = getVersion(name)
294
- try {
295
- const expected = getExports(name)
296
- const actual = getExports(name, `--experimental-loader=${hook}`)
297
- deepStrictEqual(actual, expected, `Exports for ${name} are different`)
298
- console.log(`✅ Exports for ${name}@${version} match`)
299
- return false
300
- } catch (err) {
301
- console.error(`❌ Error getting exports for ${name}@${version}:`, err)
302
- return true
303
- }
304
- }
305
-
306
- console.log(`📦 Installing ${modules.length} libraries...`)
307
- installLibs(modules)
308
-
309
- let errored = false
310
- for (const mod of modules) {
311
- errored += testLib(mod)
312
- }
313
-
314
- if (errored) {
315
- console.error('❌ Some tests failed')
316
- process.exit(1)
317
- }
@@ -1,7 +0,0 @@
1
- export const a = 'a'
2
-
3
- export function aFunc () {
4
- return a
5
- }
6
-
7
- export * from './foo.mjs'
@@ -1,5 +0,0 @@
1
- export const b = 'b'
2
-
3
- export function bFunc () {
4
- return b
5
- }
@@ -1,4 +0,0 @@
1
- import bar from './something.mjs'
2
- export default bar
3
- export * from './a.mjs'
4
- export * from './b.mjs'