From ce8ad2cb73af2b4126f26790052bc80244186ed8 Mon Sep 17 00:00:00 2001 From: calebboyd Date: Sat, 5 Jun 2021 07:57:09 -0500 Subject: [PATCH] feat(try-ts-ext): drop js extension from requests, and imply preferTsExts --- src/index.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index b6da2d8d2..7b1a608d0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -413,22 +413,17 @@ export function getExtensions(config: _ts.ParsedCommandLine) { function patchResolveFileName() { const originalResolveFilename = (Module as any)._resolveFilename; - const jsExt = /\.[mc]?js(x)?/; + const jsExt = '.js'; (Module as any)._resolveFilename = function (...args: any[]) { const [request, _, isMain ] = args if (isMain) { return originalResolveFilename.apply(this, args); } - try { - return originalResolveFilename.apply(this, args); - } catch (e) { - if (e.code === 'MODULE_NOT_FOUND' && jsExt.test(request)) { - args[0] = request.replace(jsExt, '.ts$1'); - return originalResolveFilename.apply(this, args); - } - throw e; + if(request.slice(-3) === jsExt) { + args[0] = request.slice(0, -3) } + return originalResolveFilename.apply(this, args); }; } @@ -505,6 +500,7 @@ export function create(rawOptions: CreateOptions = {}): Service { ...(tsNodeOptionsFromTsconfig.require || []), ...(rawOptions.require || []), ]; + options.preferTsExts = options.preferTsExts || options.tryTsExt // Re-load the compiler in case it has changed. // Compiler is loaded relative to tsconfig.json, so tsconfig discovery may cause us to load a