Skip to content

Commit

Permalink
fix: support configDir for other compilerOptions fields (#84)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <[email protected]>
  • Loading branch information
trikadin and privatenumber authored Sep 12, 2024
1 parent eba8538 commit 7e58ea3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# JetBrains (WebStorm)
.idea

# Dependency directories
node_modules/

Expand Down
94 changes: 65 additions & 29 deletions src/parse-tsconfig/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { readJsonc } from '../utils/read-jsonc.js';
import { implicitBaseUrlSymbol, configDirPlaceholder } from '../utils/constants.js';
import { resolveExtendsPath } from './resolve-extends-path.js';

const filesProperties = ['files', 'include', 'exclude'] as const;
const pathRelative = (from: string, to: string) => normalizeRelativePath(path.relative(from, to));

const filesProperties = [
'files',
'include',
'exclude',
] as const;

const resolveExtends = (

Check warning on line 17 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Release

Arrow function has a complexity of 12. Maximum allowed is 10
extendsPath: string,
Expand Down Expand Up @@ -37,7 +43,7 @@ const resolveExtends = (
const { compilerOptions } = extendsConfig;
if (compilerOptions) {
const { baseUrl } = compilerOptions;
if (baseUrl) {
if (baseUrl && !baseUrl.startsWith(configDirPlaceholder)) {
compilerOptions.baseUrl = slash(
path.relative(
fromDirectoryPath,
Expand Down Expand Up @@ -80,6 +86,11 @@ const resolveExtends = (
return extendsConfig;
};

const outputFields = [
'outDir',
'declarationDir',
] as const;

const _parseTsconfig = (

Check warning on line 94 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Release

Arrow function has a complexity of 25. Maximum allowed is 10
tsconfigPath: string,
cache?: Cache<string>,
Expand Down Expand Up @@ -166,30 +177,31 @@ const _parseTsconfig = (

for (const property of normalizedPaths) {
const unresolvedPath = compilerOptions[property];
if (unresolvedPath) {
if (unresolvedPath && !unresolvedPath.startsWith(configDirPlaceholder)) {
const resolvedBaseUrl = path.resolve(directoryPath, unresolvedPath);
const relativeBaseUrl = normalizeRelativePath(path.relative(
directoryPath,
resolvedBaseUrl,
));
const relativeBaseUrl = pathRelative(directoryPath, resolvedBaseUrl);
compilerOptions[property] = relativeBaseUrl;
}
}

let { outDir } = compilerOptions;
if (outDir) {
if (!Array.isArray(config.exclude)) {
config.exclude = [];
}
for (const outputField of outputFields) {
let outputPath = compilerOptions[outputField];

if (!config.exclude.includes(outDir)) {
config.exclude.push(outDir);
}
if (outputPath) {
if (!Array.isArray(config.exclude)) {
config.exclude = [];
}

if (!outDir.startsWith(configDirPlaceholder)) {
outDir = normalizeRelativePath(outDir);
if (!config.exclude.includes(outputPath)) {
config.exclude.push(outputPath);
}

if (!outputPath.startsWith(configDirPlaceholder)) {
outputPath = normalizeRelativePath(outputPath);
}

compilerOptions[outputField] = outputPath;
}
compilerOptions.outDir = outDir;
}
} else {
config.compilerOptions = {};
Expand Down Expand Up @@ -231,25 +243,48 @@ const interpolateConfigDir = (
}
};

/**
* @see https://github.com/microsoft/TypeScript/issues/57485#issuecomment-2027787456
* exclude paths, as it requires custom processing
*/
const compilerFieldsWithConfigDir = [
'outDir',
'declarationDir',
'outFile',
'rootDir',
'baseUrl',
'tsBuildInfoFile',
] as const;

export const parseTsconfig = (

Check warning on line 259 in src/parse-tsconfig/index.ts

View workflow job for this annotation

GitHub Actions / Release

Arrow function has a complexity of 11. Maximum allowed is 10
tsconfigPath: string,
cache: Cache<string> = new Map(),
): TsConfigJsonResolved => {
const resolvedTsconfigPath = path.resolve(tsconfigPath);
const config = _parseTsconfig(resolvedTsconfigPath, cache);

const configDir = path.dirname(resolvedTsconfigPath);
if (config.compilerOptions) {
let { outDir } = config.compilerOptions;
if (outDir) {
const interpolated = interpolateConfigDir(outDir, configDir);
if (interpolated) {
outDir = normalizeRelativePath(path.relative(configDir, interpolated));
config.compilerOptions.outDir = outDir;

const { compilerOptions } = config;
if (compilerOptions) {
for (const property of compilerFieldsWithConfigDir) {
const value = compilerOptions[property];
if (value) {
const resolvedPath = interpolateConfigDir(value, configDir);
compilerOptions[property] = resolvedPath ? pathRelative(configDir, resolvedPath) : value;
}
}

for (const property of ['rootDirs', 'typeRoots'] as const) {
const value = compilerOptions[property];
if (value) {
compilerOptions[property] = value.map((v) => {
const resolvedPath = interpolateConfigDir(v, configDir);
return resolvedPath ? pathRelative(configDir, resolvedPath) : v;
});
}
}

const { paths } = config.compilerOptions;
const { paths } = compilerOptions;
if (paths) {
for (const name of Object.keys(paths)) {
paths[name] = paths[name].map(
Expand All @@ -260,8 +295,9 @@ export const parseTsconfig = (
}

for (const property of filesProperties) {
if (config[property]) {
config[property] = config[property].map(
const value = config[property];
if (value) {
config[property] = value.map(
filePath => interpolateConfigDir(filePath, configDir) ?? filePath,
);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/specs/parse-tsconfig/extends/merges.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ export default testSuite(({ describe }) => {
'tsconfig.json': createTsconfigJson({
compilerOptions: {
outDir: '${configDir}-asdf/dist',
declarationDir: '${configDir}/dist/declaration',
outFile: '${configDir}/dist/outfile.js',
rootDir: '${configDir}/dist/src',
baseUrl: '${configDir}/dist/src',
tsBuildInfoFile: '${configDir}/dist/dist.tsbuildinfo',
rootDirs: ['${configDir}/src', '${configDir}/static'],
typeRoots: ['${configDir}/src/type', '${configDir}/types'],
paths: {
a: ['${configDir}_a/*'],
b: ['ignores/${configDir}/*'],
Expand Down
1 change: 1 addition & 0 deletions tests/specs/parse-tsconfig/parses.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default testSuite(({ describe }) => {
esModuleInterop: true,
declaration: true,
outDir: 'dist',
declarationDir: 'dist-declaration',
strict: true,
target: 'esnext',
rootDir: 'root-dir',
Expand Down

0 comments on commit 7e58ea3

Please sign in to comment.