Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,4 @@ docs-old/
git-short
git-short.sh
.claude/CLAUDE.local.md
mise.toml
3 changes: 3 additions & 0 deletions examples/tsconfig.inherit.array-extends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["./tsconfig.inherit.base.json"]
}
7 changes: 7 additions & 0 deletions examples/tsconfig.inherit.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"strict": true
},
"include": ["src/**/*.ts", "src/**/*.vue"],
"exclude": ["dist/**", "node_modules/**"]
}
4 changes: 4 additions & 0 deletions examples/tsconfig.inherit.invalid-extends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": [123],
"include": ["src/**/*.ts"]
}
3 changes: 3 additions & 0 deletions examples/tsconfig.inherit.leaf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.inherit.mid.json"
}
4 changes: 4 additions & 0 deletions examples/tsconfig.inherit.mid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.inherit.base.json",
"exclude": ["dist/**", "node_modules/**", "**/*.test.ts"]
}
3 changes: 3 additions & 0 deletions examples/tsconfig.inherit.no-ext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./tsconfig.inherit.base"
}
6 changes: 6 additions & 0 deletions examples/tsconfig.inherit.no-include.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strict": true
},
"exclude": ["dist/**"]
}
4 changes: 4 additions & 0 deletions examples/tsconfig.inherit.override.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.inherit.base.json",
"include": ["src/**/*.ts"]
}
94 changes: 94 additions & 0 deletions src/compilers/__tests__/get.inherited.file.scope.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { getInheritedFileScope } from '#/compilers/getInheritedFileScope';
import { posixJoin } from '#/modules/path/modules/posixJoin';
import { describe, expect, it } from 'vitest';

const examplesDir = posixJoin(process.cwd(), 'examples');

describe('getInheritedFileScope', () => {
it('returns include and exclude directly declared in the file', () => {
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.base.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
});

it('inherits include from base when child has no include', () => {
// tsconfig.inherit.mid.json extends base and has only exclude
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.mid.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
expect(result.exclude).toEqual(['dist/**', 'node_modules/**', '**/*.test.ts']);
});

it('traverses two levels to find include', () => {
// tsconfig.inherit.leaf.json → mid (no include) → base (has include)
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.leaf.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
expect(result.exclude).toEqual(['dist/**', 'node_modules/**', '**/*.test.ts']);
});

it('child include overrides base include', () => {
// tsconfig.inherit.override.json extends base but declares its own include
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.override.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts']);
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
});

it('returns empty arrays when no include or exclude found in chain', () => {
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.empty.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual([]);
expect(result.exclude).toEqual([]);
});

it('returns only exclude when chain has no include', () => {
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.no-include.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual([]);
expect(result.exclude).toEqual(['dist/**']);
});

it('does not loop infinitely on a non-existent extends target', () => {
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.base.json');
// base has no extends, so traversal stops after one file
const result = getInheritedFileScope(tsconfigPath);

expect(result).toBeDefined();
});

it('handles array extends (TypeScript 5.0+) and inherits include from the first entry', () => {
// tsconfig.inherit.array-extends.json: { "extends": ["./tsconfig.inherit.base.json"] }
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.array-extends.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
});

it('resolves extends path that omits the .json extension', () => {
// tsconfig.inherit.no-ext.json: { "extends": "./tsconfig.inherit.base" }
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.no-ext.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts', 'src/**/*.vue']);
expect(result.exclude).toEqual(['dist/**', 'node_modules/**']);
});

it('stops traversal when extends array contains a non-string entry', () => {
// tsconfig.inherit.invalid-extends.json: { "extends": [123], "include": [...] }
// has its own include, but extends[0] is a number → traversal stops before following parent
const tsconfigPath = posixJoin(examplesDir, 'tsconfig.inherit.invalid-extends.json');
const result = getInheritedFileScope(tsconfigPath);

expect(result.include).toEqual(['src/**/*.ts']);
expect(result.exclude).toEqual([]);
});
});
60 changes: 60 additions & 0 deletions src/compilers/getInheritedFileScope.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { getFileScope } from '#/compilers/getFileScope';
import path from 'node:path';
import * as tsm from 'ts-morph';

interface IFileScope {
include: string[];
exclude: string[];
}

/**
* Traverse the tsconfig extends chain and collect include/exclude patterns.
*
* TypeScript's extends is an overwrite (not merge): the most-derived config wins.
* include and exclude are resolved independently — each comes from the first file
* in the chain that explicitly declares it.
*
* @param tsconfigPath - absolute path to the tsconfig file
*/
export function getInheritedFileScope(tsconfigPath: string): IFileScope {
const visited = new Set<string>();
let currentPath = path.resolve(tsconfigPath);
let foundInclude: string[] | null = null;
let foundExclude: string[] | null = null;

while (!visited.has(currentPath)) {
visited.add(currentPath);

const configFile = tsm.ts.readConfigFile(currentPath, tsm.ts.sys.readFile.bind(tsm.ts));
if (configFile.error != null) break;

const { include, exclude } = getFileScope(configFile.config);

if (foundInclude == null && include.length > 0) {
foundInclude = include;
}

if (foundExclude == null && exclude.length > 0) {
foundExclude = exclude;
}

if (foundInclude != null && foundExclude != null) break;

const raw = configFile.config as { extends?: unknown };
const extendsValue: unknown = raw.extends;
if (extendsValue == null) break;

// TypeScript 5.0+ supports array extends; earlier versions use a string
const extendsEntries: unknown[] = Array.isArray(extendsValue) ? extendsValue : [extendsValue];
const firstExtends: unknown = extendsEntries[0];
if (typeof firstExtends !== 'string') break;

const resolved = path.resolve(path.dirname(currentPath), firstExtends);
currentPath = resolved.endsWith('.json') ? resolved : `${resolved}.json`;
}

return {
include: foundInclude ?? [],
exclude: foundExclude ?? [],
};
}
58 changes: 50 additions & 8 deletions src/configs/transforms/createBuildOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,23 @@ export async function createBuildOptions(
...option,
include: getTsIncludeFiles({
config: { include: option.include },
extend: { tsconfig, resolved: { projectDirPath: projectPath } },
extend: {
tsconfig,
resolved: {
projectDirPath: path.dirname(projectPath),
projectFilePath: projectPath,
},
},
}),
exclude: getTsExcludeFiles({
config: { exclude: option.exclude },
extend: { tsconfig },
extend: {
tsconfig,
resolved: {
projectDirPath: path.dirname(projectPath),
projectFilePath: projectPath,
},
},
}),
},
);
Expand All @@ -81,11 +93,23 @@ export async function createBuildOptions(
...option,
include: getTsIncludeFiles({
config: { include: option.include },
extend: { tsconfig, resolved: { projectDirPath: projectPath } },
extend: {
tsconfig,
resolved: {
projectDirPath: path.dirname(projectPath),
projectFilePath: projectPath,
},
},
}),
exclude: getTsExcludeFiles({
config: { exclude: option.exclude },
extend: { tsconfig },
extend: {
tsconfig,
resolved: {
projectDirPath: path.dirname(projectPath),
projectFilePath: projectPath,
},
},
}),
},
);
Expand All @@ -102,11 +126,23 @@ export async function createBuildOptions(
...option,
include: getTsIncludeFiles({
config: { include: option.include },
extend: { tsconfig, resolved: { projectDirPath: projectPath } },
extend: {
tsconfig,
resolved: {
projectDirPath: path.dirname(projectPath),
projectFilePath: projectPath,
},
},
}),
exclude: getTsExcludeFiles({
config: { exclude: option.exclude },
extend: { tsconfig },
extend: {
tsconfig,
resolved: {
projectDirPath: path.dirname(projectPath),
projectFilePath: projectPath,
},
},
}),
},
);
Expand All @@ -125,15 +161,21 @@ export async function createBuildOptions(
? toArray(argv.include)
: getTsIncludeFiles({
config: { include: [] },
extend: { tsconfig, resolved: { projectDirPath: projectPath } },
extend: {
tsconfig,
resolved: { projectDirPath: path.dirname(projectPath), projectFilePath: projectPath },
},
});

const exclude =
argv.exclude != null
? toArray(argv.exclude)
: getTsExcludeFiles({
config: { exclude: [] },
extend: { tsconfig },
extend: {
tsconfig,
resolved: { projectDirPath: path.dirname(projectPath), projectFilePath: projectPath },
},
});

const mode = argv.mode ?? CE_CTIX_BUILD_MODE.BUNDLE_MODE;
Expand Down
8 changes: 5 additions & 3 deletions src/modules/file/getTsExcludeFiles.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { getFileScope } from '#/compilers/getFileScope';
import { getInheritedFileScope } from '#/compilers/getInheritedFileScope';
import type { IExtendOptions } from '#/configs/interfaces/IExtendOptions';
import type { IModeGenerateOptions } from '#/configs/interfaces/IModeGenerateOptions';

export function getTsExcludeFiles(config: {
config: Pick<IModeGenerateOptions, 'exclude'>;
extend: Pick<IExtendOptions, 'tsconfig'>;
extend: Pick<IExtendOptions, 'tsconfig'> & {
resolved: Pick<IExtendOptions['resolved'], 'projectDirPath' | 'projectFilePath'>;
};
}): string[] {
if (config.config.exclude != null && config.config.exclude.length > 0) {
return config.config.exclude;
}

const { exclude } = getFileScope(config.extend.tsconfig.raw);
const { exclude } = getInheritedFileScope(config.extend.resolved.projectFilePath);
return exclude;
}
17 changes: 4 additions & 13 deletions src/modules/file/getTsIncludeFiles.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
import { getFileScope } from '#/compilers/getFileScope';
import { getInheritedFileScope } from '#/compilers/getInheritedFileScope';
import type { IExtendOptions } from '#/configs/interfaces/IExtendOptions';
import type { IModeGenerateOptions } from '#/configs/interfaces/IModeGenerateOptions';
import { isDescendant } from 'my-node-fp';

export function getTsIncludeFiles(config: {
config: Pick<IModeGenerateOptions, 'include'>;
extend: Pick<IExtendOptions, 'tsconfig'> & {
resolved: Pick<IExtendOptions['resolved'], 'projectDirPath'>;
resolved: Pick<IExtendOptions['resolved'], 'projectDirPath' | 'projectFilePath'>;
};
}): string[] {
if (config.config.include != null && config.config.include.length > 0) {
return config.config.include;
}

const { include } = getFileScope(config.extend.tsconfig.raw);
const { include } = getInheritedFileScope(config.extend.resolved.projectFilePath);

if (include.length > 0) {
return include;
}

const filePaths = config.extend.tsconfig.fileNames.filter((filePath) =>
isDescendant(config.extend.resolved.projectDirPath, filePath),
);

return filePaths;
return include;
}
Loading