Skip to content

Commit

Permalink
Add TypeScript extensions to EXTENSION_RE (#14)
Browse files Browse the repository at this point in the history
* add compatibility with typescript when using ts-node

* add missing newline

* fix typescript tests

* run typescript tests with imhotap

* separate js tests from ts tests

* do not run test:ts with node 16.10.0

* add a FIXME comment for EXTENSION_RE changes

The addition of TS extensions to EXTENSION_RE should be temporary
until we find a better way of supporting arbitrary extensions

Co-authored-by: rochdev <roch.devost@gmail.com>
  • Loading branch information
fardjad and rochdev authored Jul 27, 2022
1 parent 2c92f48 commit 9b55a81
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
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')
4 changes: 3 additions & 1 deletion hook.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

const specifiers = new Map()

const EXTENSION_RE = /\.(js|mjs|cjs)$/
// FIXME: Typescript extensions are added temporarily until we find a better
// way of supporting arbitrary extensions
const EXTENSION_RE = /\.(js|mjs|cjs|ts|mts|cts)$/

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
}
}

0 comments on commit 9b55a81

Please sign in to comment.