Skip to content

Commit

Permalink
feat(register): add try-ts-ext register entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
calebboyd committed Jun 3, 2021
1 parent aba4ae6 commit 22a15ba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"./register/files": "./register/files.js",
"./register/transpile-only": "./register/transpile-only.js",
"./register/type-check": "./register/type-check.js",
"./register/try-ts-ext": "./register/try-ts-ext.js",
"./esm": "./esm.mjs",
"./esm.mjs": "./esm.mjs",
"./esm/transpile-only": "./esm/transpile-only.mjs",
Expand Down
3 changes: 3 additions & 0 deletions register/try-ts-ext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require('../dist').register({
tryTsExt: true,
});
2 changes: 2 additions & 0 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ function filterRecognizedTsConfigTsNodeOptions(
ignoreDiagnostics,
logError,
preferTsExts,
tryTsExt,
pretty,
require,
skipIgnore,
Expand All @@ -208,6 +209,7 @@ function filterRecognizedTsConfigTsNodeOptions(
ignoreDiagnostics,
logError,
preferTsExts,
tryTsExt,
pretty,
require,
skipIgnore,
Expand Down
34 changes: 34 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export interface ProcessEnv {
TS_NODE_SKIP_PROJECT?: string;
TS_NODE_SKIP_IGNORE?: string;
TS_NODE_PREFER_TS_EXTS?: string;
TS_NODE_TRY_TS_EXT?: string;
TS_NODE_IGNORE_DIAGNOSTICS?: string;
TS_NODE_TRANSPILE_ONLY?: string;
TS_NODE_TYPE_CHECK?: string;
Expand Down Expand Up @@ -280,6 +281,13 @@ export interface RegisterOptions extends CreateOptions {
* @default false
*/
preferTsExts?: boolean;

/**
* Attempt to resolve the typescript file when the js file cannot be found.
*
* @default false
*/
tryTsExt?: boolean
}

/**
Expand Down Expand Up @@ -327,6 +335,7 @@ export const DEFAULTS: RegisterOptions = {
skipProject: yn(env.TS_NODE_SKIP_PROJECT),
skipIgnore: yn(env.TS_NODE_SKIP_IGNORE),
preferTsExts: yn(env.TS_NODE_PREFER_TS_EXTS),
tryTsExt: yn(env.TS_NODE_TRY_TS_EXT),
ignoreDiagnostics: split(env.TS_NODE_IGNORE_DIAGNOSTICS),
transpileOnly: yn(env.TS_NODE_TRANSPILE_ONLY),
typeCheck: yn(env.TS_NODE_TYPE_CHECK),
Expand Down Expand Up @@ -402,6 +411,27 @@ export function getExtensions(config: _ts.ParsedCommandLine) {
return { tsExtensions, jsExtensions };
}

function patchResolveFileName() {
const originalResolveFilename = (Module as any)._resolveFilename;
const jsExt = /\.[mc]?js(x)?/;

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

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

if (service.options.tryTsExt) {
patchResolveFileName();
}

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

Expand Down

0 comments on commit 22a15ba

Please sign in to comment.