diff --git a/.changeset/tsconfig-extends-paths-base.md b/.changeset/tsconfig-extends-paths-base.md new file mode 100644 index 00000000..116811d5 --- /dev/null +++ b/.changeset/tsconfig-extends-paths-base.md @@ -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. diff --git a/lib/TsconfigPathsPlugin.js b/lib/TsconfigPathsPlugin.js index ef32f6f5..819ce46f 100644 --- a/lib/TsconfigPathsPlugin.js +++ b/lib/TsconfigPathsPlugin.js @@ -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` */ /** @@ -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)); @@ -489,6 +503,7 @@ module.exports = class TsconfigPathsPlugin { compilerOptions.paths || {}, resolver, baseUrl, + compilerOptions.pathsBasePath, ); /** @type {{ [baseUrl: string]: TsconfigPathsData }} */ const refs = {}; @@ -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, @@ -772,6 +793,7 @@ module.exports = class TsconfigPathsPlugin { cfg.compilerOptions.paths || {}, resolver, cfg.compilerOptions.baseUrl, + cfg.compilerOptions.pathsBasePath, ); } @@ -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); diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/configs/base.json b/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/configs/base.json new file mode 100644 index 00000000..1c7c1570 --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/configs/base.json @@ -0,0 +1 @@ +{ "compilerOptions": { "baseUrl": "${configDir}/src" } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/src/foo.ts new file mode 100644 index 00000000..37ffcc5a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/src/foo.ts @@ -0,0 +1 @@ +export const v = 1; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/tsconfig.json b/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/tsconfig.json new file mode 100644 index 00000000..ee3de99c --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/config-dir-base-url/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "./configs/base.json" } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/configs/base.json b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/configs/base.json new file mode 100644 index 00000000..130c7b9a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/configs/base.json @@ -0,0 +1 @@ +{ "compilerOptions": { "baseUrl": "" } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/configs/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/configs/src/foo.ts new file mode 100644 index 00000000..abfbcd2c --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/configs/src/foo.ts @@ -0,0 +1 @@ +export const v = 2; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/src/foo.ts new file mode 100644 index 00000000..37ffcc5a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/src/foo.ts @@ -0,0 +1 @@ +export const v = 1; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/tsconfig.json b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/tsconfig.json new file mode 100644 index 00000000..7e50b840 --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-inherited/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "./configs/base.json", "compilerOptions": { "paths": { "@/*": ["src/*"] } } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/configs/base.json b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/configs/base.json new file mode 100644 index 00000000..37be215e --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/configs/base.json @@ -0,0 +1 @@ +{ "compilerOptions": { "paths": { "@/*": ["src/*"] } } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/configs/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/configs/src/foo.ts new file mode 100644 index 00000000..abfbcd2c --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/configs/src/foo.ts @@ -0,0 +1 @@ +export const v = 2; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/src/foo.ts new file mode 100644 index 00000000..37ffcc5a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/src/foo.ts @@ -0,0 +1 @@ +export const v = 1; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/tsconfig.json b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/tsconfig.json new file mode 100644 index 00000000..725e931f --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/empty-base-url-root/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "./configs/base.json", "compilerOptions": { "baseUrl": "" } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/base.json b/test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/base.json new file mode 100644 index 00000000..bf4b7624 --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/base.json @@ -0,0 +1 @@ +{ "compilerOptions": { "paths": { "@/*": ["../../src/*"] } } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/package.json b/test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/package.json new file mode 100644 index 00000000..1c3e923f --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/package/node_modules/shared-config/package.json @@ -0,0 +1 @@ +{ "name": "shared-config" } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/package/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/package/src/foo.ts new file mode 100644 index 00000000..37ffcc5a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/package/src/foo.ts @@ -0,0 +1 @@ +export const v = 1; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/package/tsconfig.json b/test/fixtures/tsconfig-paths/extends-paths-base/package/tsconfig.json new file mode 100644 index 00000000..63136c9c --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/package/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "shared-config/base.json" } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/relative/configs/base.json b/test/fixtures/tsconfig-paths/extends-paths-base/relative/configs/base.json new file mode 100644 index 00000000..aa02587d --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/relative/configs/base.json @@ -0,0 +1 @@ +{ "compilerOptions": { "paths": { "@/*": ["../src/*"] } } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/relative/src/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/relative/src/foo.ts new file mode 100644 index 00000000..37ffcc5a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/relative/src/foo.ts @@ -0,0 +1 @@ +export const v = 1; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/relative/tsconfig.json b/test/fixtures/tsconfig-paths/extends-paths-base/relative/tsconfig.json new file mode 100644 index 00000000..ee3de99c --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/relative/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "./configs/base.json" } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/configs/base.json b/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/configs/base.json new file mode 100644 index 00000000..3f2cada4 --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/configs/base.json @@ -0,0 +1 @@ +{ "compilerOptions": { "paths": { "@/*": ["./*"] } } } diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/lib/foo.ts b/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/lib/foo.ts new file mode 100644 index 00000000..37ffcc5a --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/lib/foo.ts @@ -0,0 +1 @@ +export const v = 1; diff --git a/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/tsconfig.json b/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/tsconfig.json new file mode 100644 index 00000000..e45a9b5f --- /dev/null +++ b/test/fixtures/tsconfig-paths/extends-paths-base/root-base-url/tsconfig.json @@ -0,0 +1 @@ +{ "extends": "./configs/base.json", "compilerOptions": { "baseUrl": "./lib" } } diff --git a/test/tsconfig-paths.test.js b/test/tsconfig-paths.test.js index e3968126..15113548 100644 --- a/test/tsconfig-paths.test.js +++ b/test/tsconfig-paths.test.js @@ -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", + ), + ); + }); + }); });