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 Dec 23, 2021
1 parent 3a2848c commit 1e82e3c
Show file tree
Hide file tree
Showing 5 changed files with 88 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 @@ -267,6 +267,7 @@ function filterRecognizedTsConfigTsNodeOptions(
ignoreDiagnostics,
logError,
preferTsExts,
tryTsExt,
pretty,
require,
skipIgnore,
Expand All @@ -291,6 +292,7 @@ function filterRecognizedTsConfigTsNodeOptions(
ignoreDiagnostics,
logError,
preferTsExts,
tryTsExt,
pretty,
require,
skipIgnore,
Expand Down
61 changes: 61 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 @@ -136,6 +137,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 @@ -378,6 +380,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 @@ -425,6 +435,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 @@ -510,6 +521,51 @@ 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);
};
}

/**
* Create a new TypeScript compiler instance and register it onto node.js
*/
Expand Down Expand Up @@ -543,6 +599,10 @@ export function register(
originalJsHandler
);

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

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

Expand Down Expand Up @@ -594,6 +654,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
21 changes: 21 additions & 0 deletions src/util.ts
Expand Up @@ -90,3 +90,24 @@ export function cachedLookup<T, R>(fn: (arg: T) => R): (arg: T) => R {
return cache.get(arg)!;
};
}

/**
* We do not support ts's `trace` option yet. In the meantime, rather than omit
* `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 1e82e3c

Please sign in to comment.