Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript extensions to EXTENSION_RE #14

Merged
merged 7 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- run: npm run test:ts
if: (matrix.node-version != '12.x' && matrix.node-version != '14.x' && matrix.node-version != '16.10.0')
2 changes: 1 addition & 1 deletion hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const specifiers = new Map()

const EXTENSION_RE = /\.(js|mjs|cjs)$/
const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/
fardjad marked this conversation as resolved.
Show resolved Hide resolved

const NODE_MAJOR = Number(process.versions.node.split('.')[0])

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"test": "c8 --check-coverage --lines 90 imhotap --runner test/runtest --files test/{hook,low-level,other}/*",
"test:ts": "c8 imhotap --runner test/runtest --files test/typescript/*.test.mts",
"coverage": "c8 --reporter html imhotap --runner test/runtest --files test/{hook,low-level,other}/* && echo '\nNow open coverage/index.html\n'"
},
"repository": {
Expand All @@ -26,8 +27,11 @@
},
"homepage": "https://github.com/DataDog/import-in-the-middle#readme",
"devDependencies": {
"@types/node": "^18.0.6",
"c8": "^7.8.0",
"imhotap": "^2.0.0"
"imhotap": "^2.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.7.4"
},
"dependencies": {
"module-details-from-path": "^1.0.3"
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/say-hi.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const sayHi = (name: string) => {
return `Hi ${name}`
};
7 changes: 6 additions & 1 deletion test/runtest
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const args = [
...process.argv.slice(2)
]
if (!filename.includes('disabled')) {
args.unshift(`--experimental-loader=${path.join(__dirname, '..', 'hook.mjs')}`)
const isTypescript = path.extname(filename).slice(-2) === 'ts'
const loaderPath = isTypescript
? path.join(__dirname, 'typescript', 'iitm-ts-node-loader.mjs')
: path.join(__dirname, '..', 'hook.mjs')

args.unshift(`--experimental-loader=${loaderPath}`)
}

spawn('node', args, { stdio: 'inherit' }).on('close', code => {
Expand Down
18 changes: 18 additions & 0 deletions test/typescript/iitm-ts-node-loader.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as iitm from '../../hook.mjs'
import * as tsNode from 'ts-node/esm.mjs'

const makeNext = (loader, fnName, parentResolveOrLoad) => {
return (specifierOrUrl, context) => {
return loader[fnName](specifierOrUrl, context, parentResolveOrLoad)
}
}

export async function resolve(specifier, context, defaultResolve) {
const next = makeNext(tsNode, 'resolve', defaultResolve)
return iitm.resolve(specifier, context, next)
}

export async function load(url, context, defaultLoad) {
let next = makeNext(tsNode, 'load', defaultLoad)
return iitm.load(url, context, next)
}
11 changes: 11 additions & 0 deletions test/typescript/ts-node.test.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import assert from 'node:assert/strict'
import { addHook } from '../../index.js'
import { sayHi } from '../fixtures/say-hi.mjs'

addHook((url, exported) => {
if (url.toLowerCase().endsWith('say-hi.mts')) {
exported.sayHi = () => 'Hooked'
}
})

assert.equal(sayHi('test'), 'Hooked')
14 changes: 14 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowSyntheticDefaultImports": true,
"noEmit": true
},
"ts-node": {
"esm": true
}
}