Skip to content

Commit

Permalink
feat(register): try-ts-ext register option
Browse files Browse the repository at this point in the history
  • Loading branch information
calebboyd committed Jun 3, 2021
1 parent cf9f1f4 commit 9c4b462
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
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
@@ -0,0 +1,3 @@
require('../dist').register({
tryTsExt: true,
});
13 changes: 12 additions & 1 deletion src/index.ts
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_ESM_COMPAT),
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 @@ -441,7 +450,9 @@ export function register(opts: RegisterOptions = {}): Service {
originalJsHandler
);

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

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

0 comments on commit 9c4b462

Please sign in to comment.