From f4112f15e212ae727ea5e25bf0476d2f53dd30d5 Mon Sep 17 00:00:00 2001 From: Martin Madsen Date: Wed, 31 Jul 2024 23:37:02 +0200 Subject: [PATCH 1/3] Change config context when referencing outside the main project --- src/config-loader.ts | 30 +++++++++++++++++++++++++ src/register.ts | 53 +++++++++++++++++++++++++------------------- 2 files changed, 60 insertions(+), 23 deletions(-) diff --git a/src/config-loader.ts b/src/config-loader.ts index 1ea0c0c..de85fbb 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -1,4 +1,5 @@ import * as TsConfigLoader2 from "./tsconfig-loader"; +import { MatchPath, createMatchPath } from "./match-path-sync"; import * as path from "path"; export interface ExplicitParams { @@ -87,3 +88,32 @@ export function configLoader({ addMatchAll: loadResult.baseUrl !== undefined, }; } + +export function findConfigMatcher({ + cwd, + explicitParams, +}: ConfigLoaderParams): { config: ConfigLoaderResult; matchPath: MatchPath } { + const configLoaderResult = configLoader({ + cwd, + explicitParams, + }); + + if (configLoaderResult.resultType === "failed") { + console.warn( + "".concat(configLoaderResult.message, ". tsconfig-paths will be skipped") + ); + return noOp; + } + + const matchPath = createMatchPath( + configLoaderResult.absoluteBaseUrl, + configLoaderResult.paths, + configLoaderResult.mainFields, + configLoaderResult.addMatchAll + ); + + return { + config: configLoaderResult, + matchPath, + }; +} diff --git a/src/register.ts b/src/register.ts index bf81052..7c67977 100644 --- a/src/register.ts +++ b/src/register.ts @@ -1,11 +1,16 @@ -import { createMatchPath } from "./match-path-sync"; -import { configLoader, ExplicitParams } from "./config-loader"; +import { + configLoader, + findConfigMatcher, + ExplicitParams, +} from "./config-loader"; const noOp = (): void => void 0; function getCoreModules( builtinModules: string[] | undefined -): { [key: string]: boolean } { +): { + [key: string]: boolean; +} { builtinModules = builtinModules || [ "assert", "buffer", @@ -76,25 +81,12 @@ export function register(params?: RegisterParams): () => void { cwd = argv.project; } - const configLoaderResult = configLoader({ - cwd: cwd ?? process.cwd(), - explicitParams, - }); - - if (configLoaderResult.resultType === "failed") { - console.warn( - `${configLoaderResult.message}. tsconfig-paths will be skipped` - ); - - return noOp; - } - - const matchPath = createMatchPath( - configLoaderResult.absoluteBaseUrl, - configLoaderResult.paths, - configLoaderResult.mainFields, - configLoaderResult.addMatchAll - ); + const configMatchers = [ + findConfigMatcher({ + cwd: cwd !== null && cwd !== void 0 ? cwd : process.cwd(), + explicitParams, + }), + ]; // Patch node's module loading // eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires @@ -106,7 +98,22 @@ export function register(params?: RegisterParams): () => void { Module._resolveFilename = function (request: string, _parent: any): string { const isCoreModule = coreModules.hasOwnProperty(request); if (!isCoreModule) { - const found = matchPath(request); + const parentFilename = parent?.filename; + + let matcher = configMatchers.find( + ({ config }) => + !parentFilename || parentFilename.startsWith(config.absoluteBaseUrl) + ); + + if (!matcher) { + matcher = findConfigMatcher({ + cwd: path.dirname(parentFilename), + explicitParams, + }); + configMatchers.push(matcher); + } + + const found = matcher.matchPath(request); if (found) { const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above. return originalResolveFilename.apply(this, modifiedArguments); From 3f272002bc1cf969213ca08455813b0f69dfebd8 Mon Sep 17 00:00:00 2001 From: Martin Madsen Date: Wed, 31 Jul 2024 23:58:37 +0200 Subject: [PATCH 2/3] A bit of cleanup --- src/config-loader.ts | 7 +++++-- src/register.ts | 23 +++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/config-loader.ts b/src/config-loader.ts index de85fbb..10d61c9 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -92,7 +92,9 @@ export function configLoader({ export function findConfigMatcher({ cwd, explicitParams, -}: ConfigLoaderParams): { config: ConfigLoaderResult; matchPath: MatchPath } { +}: ConfigLoaderParams): + | { config: ConfigLoaderResult; matchPath: MatchPath } + | undefined { const configLoaderResult = configLoader({ cwd, explicitParams, @@ -102,7 +104,8 @@ export function findConfigMatcher({ console.warn( "".concat(configLoaderResult.message, ". tsconfig-paths will be skipped") ); - return noOp; + + return; } const matchPath = createMatchPath( diff --git a/src/register.ts b/src/register.ts index 7c67977..73a2977 100644 --- a/src/register.ts +++ b/src/register.ts @@ -81,12 +81,16 @@ export function register(params?: RegisterParams): () => void { cwd = argv.project; } - const configMatchers = [ - findConfigMatcher({ - cwd: cwd !== null && cwd !== void 0 ? cwd : process.cwd(), - explicitParams, - }), - ]; + const primaryMatcher = findConfigMatcher({ + cwd: cwd !== null && cwd !== void 0 ? cwd : process.cwd(), + explicitParams, + }); + + if (!primaryMatcher) { + return noOp; + } + + const configMatchers = [primaryMatcher]; // Patch node's module loading // eslint-disable-next-line @typescript-eslint/no-require-imports,@typescript-eslint/no-var-requires @@ -106,14 +110,17 @@ export function register(params?: RegisterParams): () => void { ); if (!matcher) { + const targetProject = path.dirname(parentFilename); + matcher = findConfigMatcher({ - cwd: path.dirname(parentFilename), + cwd: targetProject, explicitParams, }); + configMatchers.push(matcher); } - const found = matcher.matchPath(request); + const found = matcher?.matchPath(request); if (found) { const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above. return originalResolveFilename.apply(this, modifiedArguments); From 40e88d742d8589fd08ada7e70ba4dc3c70c49c36 Mon Sep 17 00:00:00 2001 From: Martin Madsen Date: Thu, 1 Aug 2024 00:04:21 +0200 Subject: [PATCH 3/3] Resolve build errors --- src/config-loader.ts | 2 +- src/register.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/config-loader.ts b/src/config-loader.ts index 10d61c9..7f8efcd 100644 --- a/src/config-loader.ts +++ b/src/config-loader.ts @@ -93,7 +93,7 @@ export function findConfigMatcher({ cwd, explicitParams, }: ConfigLoaderParams): - | { config: ConfigLoaderResult; matchPath: MatchPath } + | { config: ConfigLoaderSuccessResult; matchPath: MatchPath } | undefined { const configLoaderResult = configLoader({ cwd, diff --git a/src/register.ts b/src/register.ts index 73a2977..c0e7540 100644 --- a/src/register.ts +++ b/src/register.ts @@ -1,8 +1,6 @@ -import { - configLoader, - findConfigMatcher, - ExplicitParams, -} from "./config-loader"; +import * as path from "path"; + +import { findConfigMatcher, ExplicitParams } from "./config-loader"; const noOp = (): void => void 0; @@ -99,7 +97,7 @@ export function register(params?: RegisterParams): () => void { const originalResolveFilename = Module._resolveFilename; const coreModules = getCoreModules(Module.builtinModules); // eslint-disable-next-line @typescript-eslint/no-explicit-any,no-underscore-dangle - Module._resolveFilename = function (request: string, _parent: any): string { + Module._resolveFilename = function (request: string, parent: any): string { const isCoreModule = coreModules.hasOwnProperty(request); if (!isCoreModule) { const parentFilename = parent?.filename; @@ -117,7 +115,9 @@ export function register(params?: RegisterParams): () => void { explicitParams, }); - configMatchers.push(matcher); + if (matcher) { + configMatchers.push(matcher); + } } const found = matcher?.matchPath(request);