Skip to content

Commit a2a6945

Browse files
committed
initial commit
1 parent 8949654 commit a2a6945

File tree

4 files changed

+80
-1
lines changed

4 files changed

+80
-1
lines changed

src/cjs-resolve-filename-hook.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import type Module = require('module');
2+
import type { Service } from '.';
3+
4+
/** @internal */
5+
export type ModuleConstructorWithInternals = typeof Module & {
6+
_resolveFilename(
7+
request: string,
8+
parent?: Module,
9+
isMain?: boolean,
10+
options?: ModuleResolveFilenameOptions,
11+
...rest: any[]
12+
): string;
13+
_preloadModules(requests?: string[]): void;
14+
};
15+
16+
interface ModuleResolveFilenameOptions {
17+
paths?: Array<string>;
18+
}
19+
20+
/**
21+
* @internal
22+
*/
23+
export function installCommonjsResolveHookIfNecessary(tsNodeService: Service) {
24+
const Module = require('module') as ModuleConstructorWithInternals;
25+
const originalResolveFilename = Module._resolveFilename;
26+
const shouldInstallHook = tsNodeService.options.experimentalResolverFeatures;
27+
if (shouldInstallHook) {
28+
Module._resolveFilename = _resolveFilename;
29+
}
30+
function _resolveFilename(
31+
this: any,
32+
request: string,
33+
parent?: Module,
34+
isMain?: boolean,
35+
options?: ModuleResolveFilenameOptions,
36+
...rest: any[]
37+
): string {
38+
if (!tsNodeService.enabled())
39+
return originalResolveFilename.call(
40+
this,
41+
request,
42+
parent,
43+
isMain,
44+
options,
45+
...rest
46+
);
47+
48+
// This is a stub to support other pull requests that will be merged in the near future
49+
// Right now, it does nothing.
50+
return originalResolveFilename.call(
51+
this,
52+
request,
53+
parent,
54+
isMain,
55+
options,
56+
...rest
57+
);
58+
}
59+
}

src/configuration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
277277
moduleTypes,
278278
experimentalReplAwait,
279279
swc,
280+
experimentalResolverFeatures,
280281
...unrecognized
281282
} = jsonObject as TsConfigOptions;
282283
const filteredTsConfigOptions = {
@@ -300,6 +301,7 @@ function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
300301
scopeDir,
301302
moduleTypes,
302303
swc,
304+
experimentalResolverFeatures,
303305
};
304306
// Use the typechecker to make sure this implementation has the correct set of properties
305307
const catchExtraneousProps: keyof TsConfigOptions =

src/index.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ import {
2525
} from './module-type-classifier';
2626
import { createResolverFunctions } from './resolver-functions';
2727
import type { createEsmHooks as createEsmHooksFn } from './esm';
28+
import {
29+
installCommonjsResolveHookIfNecessary,
30+
ModuleConstructorWithInternals,
31+
} from './cjs-resolve-filename-hook';
2832

2933
export { TSCommon };
3034
export {
@@ -379,6 +383,15 @@ export interface RegisterOptions extends CreateOptions {
379383
* @default false
380384
*/
381385
preferTsExts?: boolean;
386+
387+
/**
388+
* Enable experimental features that re-map imports and require calls to support:
389+
* `baseUrl`, `paths`, `rootDirs`, `.js` to `.ts` file extension mappings,
390+
* `outDir` to `rootDir` mappings for composite projects and monorepos.
391+
*
392+
* For details, see https://github.com/TypeStrong/ts-node/issues/1514
393+
*/
394+
experimentalResolverFeatures?: boolean;
382395
}
383396

384397
/**
@@ -546,8 +559,12 @@ export function register(
546559
originalJsHandler
547560
);
548561

562+
installCommonjsResolveHookIfNecessary(service);
563+
549564
// Require specified modules before start-up.
550-
(Module as any)._preloadModules(service.options.require);
565+
(Module as ModuleConstructorWithInternals)._preloadModules(
566+
service.options.require
567+
);
551568

552569
return service;
553570
}

website/docs/options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ _Environment variables, where available, are in `ALL_CAPS`_
5757
- `moduleType` Override the module type of certain files, ignoring the `package.json` `"type"` field. See [Module type overrides](./module-type-overrides.md) for details.<br/>*Default:* obeys `package.json` `"type"` and `tsconfig.json` `"module"` <br/>*Can only be specified via `tsconfig.json` or API.*
5858
- `TS_NODE_HISTORY` Path to history file for REPL <br/>*Default:* `~/.ts_node_repl_history`<br/>
5959
- `--noExperimentalReplAwait` Disable top-level await in REPL. Equivalent to node's [`--no-experimental-repl-await`](https://nodejs.org/api/cli.html#cli_no_experimental_repl_await)<br/>*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.<br/>*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable
60+
- `experimentalResolverFeatures` Enable experimental features that re-map imports and require calls to support: `baseUrl`, `paths`, `rootDirs`, `.js` to `.ts` file extension mappings, `outDir` to `rootDir` mappings for composite projects and monorepos. For details, see [#1514](https://github.com/TypeStrong/ts-node/issues/1514)<br/>*Default:* `false`<br/>*Can only be specified via `tsconfig.json` or API.*
6061

6162
## API
6263

0 commit comments

Comments
 (0)