Skip to content

Commit

Permalink
feat: drop js from request string
Browse files Browse the repository at this point in the history
  • Loading branch information
calebboyd committed Aug 21, 2021
1 parent d936236 commit f88eb40
Show file tree
Hide file tree
Showing 5 changed files with 75 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 @@ -263,6 +263,7 @@ function filterRecognizedTsConfigTsNodeOptions(
ignoreDiagnostics,
logError,
preferTsExts,
tryTsExt,
pretty,
require,
skipIgnore,
Expand All @@ -286,6 +287,7 @@ function filterRecognizedTsConfigTsNodeOptions(
ignoreDiagnostics,
logError,
preferTsExts,
tryTsExt,
pretty,
require,
skipIgnore,
Expand Down
54 changes: 54 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
parse,
split,
yn,
isRelativeSpecifier,
} from './util';
import { readConfig } from './configuration';
import type { TSCommon, TSInternal } from './ts-compiler-types';
Expand Down Expand Up @@ -117,6 +118,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 @@ -353,6 +355,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 @@ -400,6 +409,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 @@ -477,6 +487,45 @@ export function getExtensions(config: _ts.ParsedCommandLine) {
return { tsExtensions, jsExtensions };
}

function canDropJsExt(request: string, parentPath?: string) {
if (isRelativeSpecifier(request) && request.slice(-3) === '.js') {
if (!parentPath) return true
const paths = require.main?.paths || []
for(let i = 0; i < paths.length; i++) {
if (parentPath.startsWith(paths[i])) {
return false
}
}
return true
}
}


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

(Module as any)._resolveFilename = function (...args: any[]) {
const [request, parent, isMain ] = args
if (isMain) {
return originalResolveFilename.apply(this, args);
}
if(canDropJsExt(request, parent?.path)) {
try {
return originalResolveFilename.call(this, request.slice(0, -3), ...args.slice(1))
} catch(e) {
const mainFile = originalResolveFilename.apply(this, args);
if (mainFile.endsWith('.js')) {
//re-resolve with ts preference
//look-up tsconfig drop outDir??
return originalResolveFilename.call(this, mainFile.slice(0, -3), ...args.slice(1))
}
return mainFile
}
}
return originalResolveFilename.apply(this, args);
};
}

/**
* Register TypeScript compiler instance onto node.js
*/
Expand All @@ -497,6 +546,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 Expand Up @@ -548,6 +601,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
...(tsNodeOptionsFromTsconfig.require || []),
...(rawOptions.require || []),
];
options.preferTsExts = options.preferTsExts || options.tryTsExt

// Experimental REPL await is not compatible targets lower than ES2018
const targetSupportsTla = config.options.target! >= ts.ScriptTarget.ES2018;
Expand Down
15 changes: 15 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,18 @@ export function cachedLookup<T, R>(fn: (arg: T) => R): (arg: T) => R {
* `trace` options in hosts, I am using this placeholder.
*/
export function trace(s: string): void {}

/**
*
* Determine if a specifier is relative (from node core)
* @internal
*/
export function isRelativeSpecifier(specifier: string) {
if (specifier[0] === '.') {
if (specifier.length === 1 || specifier[1] === '/') return true;
if (specifier[1] === '.') {
if (specifier.length === 2 || specifier[2] === '/') return true;
}
}
return false;
}

0 comments on commit f88eb40

Please sign in to comment.