Skip to content

Commit

Permalink
perf: Prioritize .ts extension look-up over .js
Browse files Browse the repository at this point in the history
  • Loading branch information
Septh committed Sep 6, 2024
1 parent 30af7c6 commit 297e986
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions source/esm-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
import { readFile } from 'node:fs/promises'
import { transform } from './transform.cjs'
import type { InitializeHook, ResolveHook, LoadHook } from 'node:module'

const hookData: HookData = Object.create(null)
const jsExtRx = /\.([cm])?js$/
const tsExtRx = /\.([cm])?ts$/
const { transform } = await import('./transform.cjs')

const hookData: HookData = Object.create(null)
export const initialize: InitializeHook<HookData> = ({ self, defaultModuleType }) => {
hookData.self = self
hookData.defaultModuleType = defaultModuleType
}

export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
// when run with `ts-run <script>` or `node path/to/index.js <script>`,
// we need to resolve the <script> specifier relative to process.cwd()
// we need to resolve the entry <script> specifier relative to process.cwd()
// because otherwise Node would resolve it relative to our own index.js.
//
// FIXME: this prevents running a script from node_modules with a bare specifier,
// we may want to add support for this later.
if (context.parentURL === hookData.self) {
context = {
...context,
Expand All @@ -34,14 +31,17 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
specifier = './' + specifier
}

let result: ReturnType<ResolveHook>
try {
result = await nextResolve(specifier, context)
}
catch {
result = await nextResolve(specifier.replace(jsExtRx, '.$1ts'), context)
// Let's try first with the .ts extension...
const ts = specifier.replace(jsExtRx, '.$1ts')
if (ts !== specifier) {
try {
return await nextResolve(ts, context)
}
catch {}
}
return result

// Otherwise, go as-is.
return nextResolve(specifier, context)
}

const pkgTypeCache = new Map<string, NodeJS.ModuleType | null>()
Expand Down

0 comments on commit 297e986

Please sign in to comment.