From 835bab170a80d47616595c31a9d9b92e46acde6f Mon Sep 17 00:00:00 2001 From: Farzan Date: Sat, 20 Nov 2021 13:50:27 +0330 Subject: [PATCH] Add support for baseUrl override using TS_NODE_BASEURL env var (#185) --- README.md | 4 +++ src/tsconfig-loader.ts | 20 +++++++++++--- test/tsconfig-loader-tests.ts | 49 ++++++++++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 35369cb..0ccab34 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,10 @@ npm install --save-dev tsconfig-paths `node -r tsconfig-paths/register main.js` +If `process.env.TS_NODE_BASEURL` is set it will override the value of `baseUrl` in tsconfig.json: + +`TS_NODE_BASEURL=./dist node -r tsconfig-paths/register main.js` + ### With ts-node `ts-node -r tsconfig-paths/register main.ts` diff --git a/src/tsconfig-loader.ts b/src/tsconfig-loader.ts index 8ccdcff..c2dfb04 100644 --- a/src/tsconfig-loader.ts +++ b/src/tsconfig-loader.ts @@ -26,7 +26,11 @@ export interface TsConfigLoaderResult { export interface TsConfigLoaderParams { getEnv: (key: string) => string | undefined; cwd: string; - loadSync?(cwd: string, filename?: string): TsConfigLoaderResult; + loadSync?( + cwd: string, + filename?: string, + baseUrl?: string + ): TsConfigLoaderResult; } export function tsConfigLoader({ @@ -35,13 +39,19 @@ export function tsConfigLoader({ loadSync = loadSyncDefault, }: TsConfigLoaderParams): TsConfigLoaderResult { const TS_NODE_PROJECT = getEnv("TS_NODE_PROJECT"); + const TS_NODE_BASEURL = getEnv("TS_NODE_BASEURL"); // tsconfig.loadSync handles if TS_NODE_PROJECT is a file or directory - const loadResult = loadSync(cwd, TS_NODE_PROJECT); + // and also overrides baseURL if TS_NODE_BASEURL is available. + const loadResult = loadSync(cwd, TS_NODE_PROJECT, TS_NODE_BASEURL); return loadResult; } -function loadSyncDefault(cwd: string, filename?: string): TsConfigLoaderResult { +function loadSyncDefault( + cwd: string, + filename?: string, + baseUrl?: string +): TsConfigLoaderResult { // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename const configPath = resolveConfigPath(cwd, filename); @@ -57,7 +67,9 @@ function loadSyncDefault(cwd: string, filename?: string): TsConfigLoaderResult { return { tsConfigPath: configPath, - baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl, + baseUrl: + baseUrl || + (config && config.compilerOptions && config.compilerOptions.baseUrl), paths: config && config.compilerOptions && config.compilerOptions.paths, }; } diff --git a/test/tsconfig-loader-tests.ts b/test/tsconfig-loader-tests.ts index 9849d96..37d5494 100644 --- a/test/tsconfig-loader-tests.ts +++ b/test/tsconfig-loader-tests.ts @@ -63,6 +63,41 @@ describe("tsconfig-loader", () => { assert.equal(result.tsConfigPath, "/foo/baz/tsconfig.json"); }); + + it("should use TS_NODE_BASEURL env if exists", () => { + const result = tsConfigLoader({ + cwd: "/foo/bar", + getEnv: (key: string) => + key === "TS_NODE_BASEURL" ? "SOME_BASEURL" : undefined, + loadSync: (_0: string, _1: string, baseUrl: string) => { + return { + tsConfigPath: undefined, + baseUrl, + paths: {}, + }; + }, + }); + + assert.equal(result.baseUrl, "SOME_BASEURL"); + }); + + it("should not use TS_NODE_BASEURL env if it does not exist", () => { + const result = tsConfigLoader({ + cwd: "/foo/bar", + getEnv: (_: string) => { + return undefined; + }, + loadSync: (_0: string, _1: string, baseUrl: string) => { + return { + tsConfigPath: undefined, + baseUrl, + paths: {}, + }; + }, + }); + + assert.equal(result.baseUrl, undefined); + }); }); describe("walkForTsConfig", () => { @@ -171,15 +206,15 @@ describe("loadConfig", () => { it("It should load a config with extends from node_modules and overwrite all options", () => { const firstConfig = { extends: "my-package/base-config.json", - compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } } + compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] } }, }; const firstConfigPath = join("/root", "dir1", "tsconfig.json"); const baseConfig = { compilerOptions: { baseUrl: "olle", paths: { foo: ["bar1"] }, - strict: true - } + strict: true, + }, }; const baseConfigPath = join( "/root", @@ -190,8 +225,8 @@ describe("loadConfig", () => { ); const res = loadTsconfig( join("/root", "dir1", "tsconfig.json"), - path => path === firstConfigPath || path === baseConfigPath, - path => { + (path) => path === firstConfigPath || path === baseConfigPath, + (path) => { if (path === firstConfigPath) { return JSON.stringify(firstConfig); } @@ -207,8 +242,8 @@ describe("loadConfig", () => { compilerOptions: { baseUrl: "kalle", paths: { foo: ["bar2"] }, - strict: true - } + strict: true, + }, }); });