Skip to content

Commit

Permalink
feat(try-ts-ext): drop js extension from requests, and imply preferTs…
Browse files Browse the repository at this point in the history
…Exts
  • Loading branch information
calebboyd committed Jun 5, 2021
1 parent 675c053 commit ce8ad2c
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ce8ad2c

Please sign in to comment.