Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for baseUrl override using TS_NODE_BASEURL env var #185

Merged
merged 1 commit into from Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -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`
Expand Down
20 changes: 16 additions & 4 deletions src/tsconfig-loader.ts
Expand Up @@ -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({
Expand All @@ -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);
Expand All @@ -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,
};
}
Expand Down
49 changes: 42 additions & 7 deletions test/tsconfig-loader-tests.ts
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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",
Expand All @@ -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);
}
Expand All @@ -207,8 +242,8 @@ describe("loadConfig", () => {
compilerOptions: {
baseUrl: "kalle",
paths: { foo: ["bar2"] },
strict: true
}
strict: true,
},
});
});

Expand Down