Skip to content

Commit c5bd5db

Browse files
cspotcodecalebboyd
authored andcommitted
changes
1 parent bb0ee63 commit c5bd5db

File tree

3 files changed

+47
-60
lines changed

3 files changed

+47
-60
lines changed

src/cjs-resolve-filename-hook.ts

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type Module = require('module');
22
import type { Service } from '.';
3+
import { isRelativeSpecifier } from './util';
34

45
/** @internal */
56
export type ModuleConstructorWithInternals = typeof Module & {
@@ -19,6 +20,10 @@ interface ModuleResolveFilenameOptions {
1920

2021
/**
2122
* @internal
23+
*
24+
* If any features of this service require patching Module._resolveFilename,
25+
* then install our hook. Logic within the hook conditionally implements
26+
* multiple resolver behaviors.
2227
*/
2328
export function installCommonjsResolveHookIfNecessary(tsNodeService: Service) {
2429
const Module = require('module') as ModuleConstructorWithInternals;
@@ -35,7 +40,7 @@ export function installCommonjsResolveHookIfNecessary(tsNodeService: Service) {
3540
options?: ModuleResolveFilenameOptions,
3641
...rest: any[]
3742
): string {
38-
if (!tsNodeService.enabled())
43+
function defer(this: any) {
3944
return originalResolveFilename.call(
4045
this,
4146
request,
@@ -44,16 +49,48 @@ export function installCommonjsResolveHookIfNecessary(tsNodeService: Service) {
4449
options,
4550
...rest
4651
);
52+
}
53+
if (!tsNodeService.enabled())
54+
return defer();
4755

56+
// Map from emit to source extensions
57+
if (!isMain && canReplaceJsWithTsExt(tsNodeService, request, parent?.filename)) {
58+
try {
59+
return originalResolveFilename.call(
60+
this,
61+
request.slice(0, -3),
62+
parent, isMain, options, ...rest
63+
);
64+
} catch (e) {
65+
const mainFile = defer();
66+
if (mainFile.endsWith('.js')) {
67+
//re-resolve with configured extension preference
68+
return originalResolveFilename.call(
69+
this,
70+
mainFile.slice(0, -3),
71+
parent, isMain, options, ...rest
72+
);
73+
}
74+
return mainFile;
75+
}
76+
}
4877
// This is a stub to support other pull requests that will be merged in the near future
4978
// Right now, it does nothing.
50-
return originalResolveFilename.call(
51-
this,
52-
request,
53-
parent,
54-
isMain,
55-
options,
56-
...rest
57-
);
79+
return defer();
80+
}
81+
}
82+
83+
function canReplaceJsWithTsExt(service: Service, request: string, parentPath?: string) {
84+
if (!parentPath || service.ignored(parentPath)) return false;
85+
if (isRelativeSpecifier(request) && request.slice(-3) === '.js') {
86+
if (!parentPath) return true;
87+
const paths = require.main?.paths || [];
88+
// This logic is intending to exclude node_modules
89+
for (let i = 0; i < paths.length; i++) {
90+
if (parentPath.startsWith(paths[i])) {
91+
return false;
92+
}
93+
}
94+
return true;
5895
}
5996
}

src/configuration.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,6 @@ function filterRecognizedTsConfigTsNodeOptions(jsonObject: any): {
406406
scope,
407407
scopeDir,
408408
moduleTypes,
409-
experimentalResolverFeatures,
410409
swc,
411410
experimentalResolverFeatures,
412411
esm,

src/index.ts

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
ProjectLocalResolveHelper,
1717
split,
1818
yn,
19-
isRelativeSpecifier,
2019
} from './util';
2120
import { findAndReadConfig, loadCompiler } from './configuration';
2221
import type { TSCommon, TSInternal } from './ts-compiler-types';
@@ -26,10 +25,7 @@ import {
2625
} from './module-type-classifier';
2726
import { createResolverFunctions } from './resolver-functions';
2827
import type { createEsmHooks as createEsmHooksFn } from './esm';
29-
import {
30-
installCommonjsResolveHookIfNecessary,
31-
ModuleConstructorWithInternals,
32-
} from './cjs-resolve-filename-hook';
28+
import {installCommonjsResolveHookIfNecessary, ModuleConstructorWithInternals} from './cjs-resolve-filename-hook';
3329

3430
export { TSCommon };
3531
export {
@@ -572,51 +568,6 @@ export function getExtensions(config: _ts.ParsedCommandLine) {
572568
return { tsExtensions, jsExtensions };
573569
}
574570

575-
function canDropJsExt(request: string, parentPath?: string) {
576-
if (isRelativeSpecifier(request) && request.slice(-3) === '.js') {
577-
if (!parentPath) return true;
578-
const paths = require.main?.paths || [];
579-
for (let i = 0; i < paths.length; i++) {
580-
if (parentPath.startsWith(paths[i])) {
581-
return false;
582-
}
583-
}
584-
return true;
585-
}
586-
}
587-
588-
function patchResolveFileName() {
589-
const originalResolveFilename = (Module as any)._resolveFilename;
590-
591-
(Module as any)._resolveFilename = function (...args: any[]) {
592-
const [request, parent, isMain] = args;
593-
if (isMain) {
594-
return originalResolveFilename.apply(this, args);
595-
}
596-
if (canDropJsExt(request, parent?.path)) {
597-
try {
598-
return originalResolveFilename.call(
599-
this,
600-
request.slice(0, -3),
601-
...args.slice(1)
602-
);
603-
} catch (e) {
604-
const mainFile = originalResolveFilename.apply(this, args);
605-
if (mainFile.endsWith('.js')) {
606-
//re-resolve with configured extension preference
607-
return originalResolveFilename.call(
608-
this,
609-
mainFile.slice(0, -3),
610-
...args.slice(1)
611-
);
612-
}
613-
return mainFile;
614-
}
615-
}
616-
return originalResolveFilename.apply(this, args);
617-
};
618-
}
619-
620571
/**
621572
* Create a new TypeScript compiler instance and register it onto node.js
622573

0 commit comments

Comments
 (0)