Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getTsconfig, TsConfigJsonResolved } from 'get-tsconfig';
import * as micromatch from 'micromatch';
import * as fs from 'fs';
import * as path from 'path';
import * as process from 'process';
import { Project } from 'ts-morph';
import { Compiler, Module } from 'webpack';
import { DEFAULT_TS_CONFIG_PATH, PLUGIN_NAME } from '../constants';
Expand Down Expand Up @@ -308,16 +308,49 @@ export class WebpackAngularTypesPlugin {
private getIncludedPathsFromTsconfig(
tsconfigPath: string,
tsconfig: TsConfigJsonResolved,
visited = new Set<string>(),
): string[] {
const tsConfigRootDir = path.join(process.cwd(), tsconfigPath, '..');
const resolvedTsconfigPath = path.resolve(tsconfigPath);
if (visited.has(resolvedTsconfigPath)) return [];
visited.add(resolvedTsconfigPath);

const tsConfigRootDir = path.resolve(tsconfigPath, '..');

const includedPaths = tsconfig.include || [];
const excludedPaths = tsconfig.exclude || [];

return [
const ownPaths = [
...this.transformToAbsolutePaths(tsConfigRootDir, includedPaths),
...this.transformToAbsolutePaths(tsConfigRootDir, excludedPaths).map(this.negateGlob),
].map(this.toUnixPath);

const rawReferences = this.readRawTsconfigReferences(resolvedTsconfigPath);
const referencedPaths = rawReferences.flatMap((ref) => {
let refTsconfigPath = path.isAbsolute(ref.path)
? ref.path
: path.join(tsConfigRootDir, ref.path);

if (!refTsconfigPath.endsWith('.json')) {
refTsconfigPath = path.join(refTsconfigPath, 'tsconfig.json');
}

const refConfig = getTsconfig(refTsconfigPath)?.config;
if (!refConfig) return [];

return this.getIncludedPathsFromTsconfig(refTsconfigPath, refConfig, visited);
});

return [...ownPaths, ...referencedPaths];
}

private readRawTsconfigReferences(tsconfigPath: string): { path: string }[] {
try {
const content = fs.readFileSync(tsconfigPath, 'utf8');
const json = content.replace(/\/\/[^\n]*/g, '').replace(/,(\s*[}\]])/g, '$1');
return JSON.parse(json).references || [];
} catch {
return [];
}
}

private transformToAbsolutePaths(base: string, paths: string[]): string[] {
Expand Down