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

fix: support configDir for other compilerOptions fields #84

Merged
merged 6 commits into from
Sep 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,9 +6,15 @@
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 / Test (ubuntu-latest)

Arrow function has a complexity of 12. Maximum allowed is 10
extendsPath: string,
fromDirectoryPath: string,
circularExtendsTracker: Set<string>,
Expand Down Expand Up @@ -37,7 +43,7 @@
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,7 +86,12 @@
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 / Test (ubuntu-latest)

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

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 @@
}
};

/**
* @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 / Test (ubuntu-latest)

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 @@
}

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
Loading