import-in-the-middle 1.2.3 → 1.3.0
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/hook.mjs +3 -1
- package/package.json +6 -2
- package/test/fixtures/say-hi.mts +3 -0
- package/test/runtest +6 -1
- package/test/typescript/iitm-ts-node-loader.mjs +18 -0
- package/test/typescript/ts-node.test.mts +11 -0
- package/tsconfig.json +14 -0
package/hook.mjs
CHANGED
|
@@ -4,7 +4,9 @@
|
|
|
4
4
|
|
|
5
5
|
const specifiers = new Map()
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// FIXME: Typescript extensions are added temporarily until we find a better
|
|
8
|
+
// way of supporting arbitrary extensions
|
|
9
|
+
const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/
|
|
8
10
|
|
|
9
11
|
const NODE_MAJOR = Number(process.versions.node.split('.')[0])
|
|
10
12
|
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "import-in-the-middle",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Intercept imports in Node.js",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
|
|
8
|
+
"test:ts": "c8 imhotap --runner test/runtest --files test/typescript/*.test.mts",
|
|
8
9
|
"coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
|
|
9
10
|
},
|
|
10
11
|
"repository": {
|
|
@@ -26,8 +27,11 @@
|
|
|
26
27
|
},
|
|
27
28
|
"homepage": "https://github.com/DataDog/import-in-the-middle#readme",
|
|
28
29
|
"devDependencies": {
|
|
30
|
+
"@types/node": "^18.0.6",
|
|
29
31
|
"c8": "^7.8.0",
|
|
30
|
-
"imhotap": "^2.0.0"
|
|
32
|
+
"imhotap": "^2.0.0",
|
|
33
|
+
"ts-node": "^10.9.1",
|
|
34
|
+
"typescript": "^4.7.4"
|
|
31
35
|
},
|
|
32
36
|
"dependencies": {
|
|
33
37
|
"module-details-from-path": "^1.0.3"
|
package/test/runtest
CHANGED
|
@@ -15,7 +15,12 @@ const args = [
|
|
|
15
15
|
...process.argv.slice(2)
|
|
16
16
|
]
|
|
17
17
|
if (!filename.includes('disabled')) {
|
|
18
|
-
|
|
18
|
+
const isTypescript = path.extname(filename).slice(-2) === 'ts'
|
|
19
|
+
const loaderPath = isTypescript
|
|
20
|
+
? path.join(__dirname, 'typescript', 'iitm-ts-node-loader.mjs')
|
|
21
|
+
: path.join(__dirname, '..', 'hook.mjs')
|
|
22
|
+
|
|
23
|
+
args.unshift(`--experimental-loader=${loaderPath}`)
|
|
19
24
|
}
|
|
20
25
|
|
|
21
26
|
spawn('node', args, { stdio: 'inherit' }).on('close', code => {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as iitm from '../../hook.mjs'
|
|
2
|
+
import * as tsNode from 'ts-node/esm.mjs'
|
|
3
|
+
|
|
4
|
+
const makeNext = (loader, fnName, parentResolveOrLoad) => {
|
|
5
|
+
return (specifierOrUrl, context) => {
|
|
6
|
+
return loader[fnName](specifierOrUrl, context, parentResolveOrLoad)
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export async function resolve(specifier, context, defaultResolve) {
|
|
11
|
+
const next = makeNext(tsNode, 'resolve', defaultResolve)
|
|
12
|
+
return iitm.resolve(specifier, context, next)
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function load(url, context, defaultLoad) {
|
|
16
|
+
let next = makeNext(tsNode, 'load', defaultLoad)
|
|
17
|
+
return iitm.load(url, context, next)
|
|
18
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import assert from 'node:assert/strict'
|
|
2
|
+
import { addHook } from '../../index.js'
|
|
3
|
+
import { sayHi } from '../fixtures/say-hi.mjs'
|
|
4
|
+
|
|
5
|
+
addHook((url, exported) => {
|
|
6
|
+
if (url.toLowerCase().endsWith('say-hi.mts')) {
|
|
7
|
+
exported.sayHi = () => 'Hooked'
|
|
8
|
+
}
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
assert.equal(sayHi('test'), 'Hooked')
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "esnext",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"experimentalDecorators": true,
|
|
7
|
+
"emitDecoratorMetadata": true,
|
|
8
|
+
"allowSyntheticDefaultImports": true,
|
|
9
|
+
"noEmit": true
|
|
10
|
+
},
|
|
11
|
+
"ts-node": {
|
|
12
|
+
"esm": true
|
|
13
|
+
}
|
|
14
|
+
}
|