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 overwrite using TS_NODE_BASEURL environment v… #114

Closed
wants to merge 1 commit into from
Closed
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 be overwrite 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
19 changes: 15 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,18 @@ 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);
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 +66,9 @@ function loadSyncDefault(cwd: string, filename?: string): TsConfigLoaderResult {

return {
tsConfigPath: configPath,
baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl,
baseUrl: baseUrl
? baseUrl
: config && config.compilerOptions && config.compilerOptions.baseUrl,
paths: config && config.compilerOptions && config.compilerOptions.paths
};
}
Expand Down
35 changes: 35 additions & 0 deletions test/tsconfig-loader-tests.ts
Expand Up @@ -65,6 +65,41 @@ describe("tsconfig-loader", () => {
});
});

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: (cwd: string, fileName: 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: (key: string) => {
return undefined;
},
loadSync: (cwd: string, fileName: string, baseUrl: string) => {
return {
tsConfigPath: undefined,
baseUrl,
paths: {}
};
}
});

assert.equal(result.baseUrl, undefined);
});

describe("walkForTsConfig", () => {
it("should find tsconfig in starting directory", () => {
const pathToTsconfig = join("/root", "dir1", "tsconfig.json");
Expand Down