dividab / tsconfig-paths

Load node modules according to tsconfig paths, in run-time or via API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature: search for "extends" config in node_modules in parent folders in monorepo.

allista opened this issue · comments

Hi,
we have a monorepo with a lot of packages, and some things, including our base tsconfig.json that we extend in all other packages, are installed in the root node_modules.

Currently tsconfig-paths is only searching for the "extends" file in the node_modules next to the original tsconfig.file.

I don't have capacity right now for a proper PR, but I made a pnpm patch that solves the issue for us.

I hope it'll be useful for someone in the meantime:

diff --git a/lib/tsconfig-loader.js b/lib/tsconfig-loader.js
index d298653fa4119fff689d60cce11cce73bb801420..619a374585119e37154393b7b63c6b14e88857eb 100644
--- a/lib/tsconfig-loader.js
+++ b/lib/tsconfig-loader.js
@@ -107,7 +107,18 @@ existsSync, readFileSync) {
         if (extendedConfig.indexOf("/") !== -1 &&
             extendedConfig.indexOf(".") !== -1 &&
             !existsSync(extendedConfigPath)) {
-            extendedConfigPath = path.join(currentDir, "node_modules", extendedConfig);
+            var lookupDir = currentDir;
+            while(existsSync(lookupDir)) {
+                extendedConfigPath = path.join(lookupDir, "node_modules", extendedConfig);
+                if(existsSync(extendedConfigPath)) {
+                    break;
+                }
+                var nextDir = path.normalize(path.resolve(lookupDir, '..'));
+                if(nextDir === lookupDir){
+                    break;
+                }
+                lookupDir = nextDir;
+            }
         }
         var base = loadTsconfig(extendedConfigPath, existsSync, readFileSync) || {};
         // baseUrl should be interpreted as relative to the base tsconfig,

This definitely is missing from tsconfig-paths. We have a slightly different use case where base tsconfigs are not in the monorepo root but in a package called tsconfig, so extends looks like this in a workspace's tsconfig:

{
  "extends": "@my-internal-scope/tsconfig/nodejs.json",
  "ts-node": {
    "files": true,
    "require": ["tsconfig-paths/register"],
    "transpileOnly": true
  }
}

In above example we extend from a preset called "nodejs" which in turn extends from a base, where all path mappings internal to the monorepo reside. I wish tsconfig-paths resolved those too.