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 dd9044b
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 0 deletions.
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,
});
2 changes: 2 additions & 0 deletions src/configuration.ts
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
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,14 @@ export interface RegisterOptions extends CreateOptions {
* @default false
*/
preferTsExts?: boolean;

/**
* Attempt to resolve the typescript file when a request with a js extension is provided.
* Implies preferTsExts.
*
* @default false
*/
tryTsExt?: boolean
}

/**
Expand Down Expand Up @@ -400,6 +410,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 +488,44 @@ 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 configured extension preference
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
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 dd9044b

Please sign in to comment.