Skip to content

Commit

Permalink
feat(register): swap js -> ts on MODULE_NOT_FOUND
Browse files Browse the repository at this point in the history
  • Loading branch information
calebboyd committed Jun 3, 2021
1 parent aba4ae6 commit cf9f1f4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,25 @@ export function getExtensions(config: _ts.ParsedCommandLine) {
return { tsExtensions, jsExtensions };
}

function patchResolveFileName() {
const originalResolveFilename = (Module as any)._resolveFilename;

(Module as any)._resolveFilename = function (...args: any[]) {
if (args[2]) {
return originalResolveFilename.apply(this, args);
}
try {
return originalResolveFilename.apply(this, args);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
args[0] = args[0].replace(/\.[mc]?js(x)?$/, '.ts$1');
return originalResolveFilename.apply(this, args);
}
throw e;
}
};
}

/**
* Register TypeScript compiler instance onto node.js
*/
Expand All @@ -422,6 +441,8 @@ export function register(opts: RegisterOptions = {}): Service {
originalJsHandler
);

patchResolveFileName();

// Require specified modules before start-up.
(Module as any)._preloadModules(service.options.require);

Expand Down

0 comments on commit cf9f1f4

Please sign in to comment.