Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tsconfig-extends-paths-base.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"enhanced-resolve": patch
---

Resolve `paths` inherited through `extends` (with no `baseUrl`) relative to the tsconfig that declares them, substitute `${configDir}` in an inherited `baseUrl`, and treat an empty `baseUrl` as the directory of its tsconfig, like tsc.
41 changes: 35 additions & 6 deletions lib/TsconfigPathsPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const _contextSelectionCache = new WeakMap();
* @typedef {object} TsconfigCompilerOptions
* @property {string=} baseUrl Base URL for resolving paths
* @property {{ [key: string]: string[] }=} paths TypeScript paths mapping
* @property {string=} pathsBasePath directory of the config file that declares `paths`, used when there is no `baseUrl`
*/

/**
Expand Down Expand Up @@ -138,13 +139,26 @@ function substituteConfigDir(pathValue, configDir) {
* @param {{ [key: string]: string[] }} paths TypeScript paths mapping
* @param {Resolver} resolver resolver instance
* @param {string=} baseUrl Base URL for resolving paths (relative to configDir)
* @param {string=} pathsBasePath directory of the config file that declares `paths` (defaults to configDir)
* @returns {TsconfigPathsData} the resolver options
*/
function tsconfigPathsToResolveOptions(configDir, paths, resolver, baseUrl) {
// Calculate absolute base URL
const absoluteBaseUrl = !baseUrl
? configDir
: resolver.join(configDir, baseUrl);
function tsconfigPathsToResolveOptions(
configDir,
paths,
resolver,
baseUrl,
pathsBasePath,
) {
// Substitute ${configDir} in baseUrl
if (baseUrl) baseUrl = substituteConfigDir(baseUrl, configDir);

// Calculate absolute base URL. Without baseUrl, tsc resolves `paths`
// relative to the config file that declares them. An empty baseUrl is
// still a baseUrl: the directory of the config file.
const absoluteBaseUrl =
baseUrl === undefined
? pathsBasePath || configDir
: resolver.join(configDir, baseUrl);

/** @type {string[]} */
const sortedKeys = sortByLongestPrefix(Object.keys(paths));
Expand Down Expand Up @@ -489,6 +503,7 @@ module.exports = class TsconfigPathsPlugin {
compilerOptions.paths || {},
resolver,
baseUrl,
compilerOptions.pathsBasePath,
);
/** @type {{ [baseUrl: string]: TsconfigPathsData }} */
const refs = {};
Expand Down Expand Up @@ -635,7 +650,13 @@ module.exports = class TsconfigPathsPlugin {
baseUrl: undefined,
};

if (compilerOptions.baseUrl) {
// ${configDir} stays as is: it means the directory of the
// root tsconfig, so it is substituted when the paths are built
if (
typeof compilerOptions.baseUrl === "string" &&
// eslint-disable-next-line no-template-curly-in-string
!compilerOptions.baseUrl.includes("${configDir}")
) {
const extendedConfigDir = resolver.dirname(extendedConfigPath);
compilerOptions.baseUrl = getAbsoluteBaseUrl(
extendedConfigDir,
Expand Down Expand Up @@ -772,6 +793,7 @@ module.exports = class TsconfigPathsPlugin {
cfg.compilerOptions.paths || {},
resolver,
cfg.compilerOptions.baseUrl,
cfg.compilerOptions.pathsBasePath,
);
}

Expand Down Expand Up @@ -826,6 +848,13 @@ module.exports = class TsconfigPathsPlugin {
const config = /** @type {Tsconfig} */ (parsed);
fileDependencies.add(configFilePath);

// Remember where `paths` is declared, so that paths inherited
// through `extends` resolve relative to their own config file
if (config.compilerOptions && config.compilerOptions.paths) {
config.compilerOptions.pathsBasePath =
resolver.dirname(configFilePath);
}

const extendedConfig = config.extends;
if (!extendedConfig) return callback(null, config);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "compilerOptions": { "baseUrl": "${configDir}/src" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "./configs/base.json" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "compilerOptions": { "baseUrl": "" } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "./configs/base.json", "compilerOptions": { "paths": { "@/*": ["src/*"] } } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "compilerOptions": { "paths": { "@/*": ["src/*"] } } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 2;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "./configs/base.json", "compilerOptions": { "baseUrl": "" } }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "shared-config/base.json" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "compilerOptions": { "paths": { "@/*": ["../src/*"] } } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "./configs/base.json" }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "compilerOptions": { "paths": { "@/*": ["./*"] } } }
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "./configs/base.json", "compilerOptions": { "baseUrl": "./lib" } }
74 changes: 74 additions & 0 deletions test/tsconfig-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1876,4 +1876,78 @@ describe("TsconfigPathsPlugin", () => {
});
}
});

describe("paths and baseUrl inherited through extends", () => {
const extendsPathsBaseDir = path.resolve(
__dirname,
"fixtures",
"tsconfig-paths",
"extends-paths-base",
);

/**
* @param {string} name fixture directory name
* @param {string} request request to resolve
* @returns {string} resolved path
*/
const resolveIn = (name, request) => {
const dir = path.join(extendsPathsBaseDir, name);
const resolver = ResolverFactory.createResolver({
fileSystem,
extensions: [".ts"],
useSyncFileSystemCalls: true,
tsconfig: path.join(dir, "tsconfig.json"),
});
return /** @type {string} */ (resolver.resolveSync({}, dir, request));
};

it("should resolve inherited paths without baseUrl relative to the config that declares them", () => {
assert.strictEqual(
resolveIn("relative", "@/foo"),
path.join(extendsPathsBaseDir, "relative", "src", "foo.ts"),
);
});

it("should resolve inherited paths without baseUrl from a package config relative to that config", () => {
assert.strictEqual(
resolveIn("package", "@/foo"),
path.join(extendsPathsBaseDir, "package", "src", "foo.ts"),
);
});

// eslint-disable-next-line no-template-curly-in-string
it("should substitute ${configDir} in an inherited baseUrl with the root config directory", () => {
assert.strictEqual(
resolveIn("config-dir-base-url", "foo"),
path.join(extendsPathsBaseDir, "config-dir-base-url", "src", "foo.ts"),
);
});

it("should resolve inherited paths relative to the baseUrl of the root config", () => {
assert.strictEqual(
resolveIn("root-base-url", "@/foo"),
path.join(extendsPathsBaseDir, "root-base-url", "lib", "foo.ts"),
);
});

it("should treat an empty baseUrl in the root config as the root config directory", () => {
assert.strictEqual(
resolveIn("empty-base-url-root", "@/foo"),
path.join(extendsPathsBaseDir, "empty-base-url-root", "src", "foo.ts"),
);
});

it("should treat an empty inherited baseUrl as the directory of the config that declares it", () => {
assert.strictEqual(
resolveIn("empty-base-url-inherited", "@/foo"),
path.join(
extendsPathsBaseDir,
"empty-base-url-inherited",
"configs",
"src",
"foo.ts",
),
);
});
});
});