Skip to content

feat: support tsconfig references #4689

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions e2e/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"devDependencies": {
"@formatjs/ts-transformer": "^3.13.32",
"@testing-library/react": "^14.3.1",
"@types/jest": "^29.5.14",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.5",
"esbuild": "~0.25.1",
Expand Down
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/jest-compiler-cjs.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest'

export default {
displayName: 'tsconfig-projects-compiler-cjs',
transform: {
[TS_JS_TRANSFORM_PATTERN]: 'ts-jest',
},
} satisfies JestConfigWithTsJest
14 changes: 14 additions & 0 deletions e2e/tsconfig-projects/jest-compiler-esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest'

export default {
displayName: 'tsconfig-projects-compiler-esm',
extensionsToTreatAsEsm: ['.ts'],
transform: {
[TS_JS_TRANSFORM_PATTERN]: [
'ts-jest',
{
useESM: true,
},
],
},
} satisfies JestConfigWithTsJest
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/jest-transpiler-cjs.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest'

export default {
displayName: 'tsconfig-projects-transpiler-cjs',
transform: {
[TS_JS_TRANSFORM_PATTERN]: 'ts-jest',
},
} satisfies JestConfigWithTsJest
14 changes: 14 additions & 0 deletions e2e/tsconfig-projects/jest-transpiler-esm.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { type JestConfigWithTsJest, TS_JS_TRANSFORM_PATTERN } from 'ts-jest'

export default {
displayName: 'tsconfig-projects-transpiler-esm',
extensionsToTreatAsEsm: ['.ts'],
transform: {
[TS_JS_TRANSFORM_PATTERN]: [
'ts-jest',
{
useESM: true,
},
],
},
} satisfies JestConfigWithTsJest
4 changes: 4 additions & 0 deletions e2e/tsconfig-projects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "tsconfig-projects",
"private": true
}
7 changes: 7 additions & 0 deletions e2e/tsconfig-projects/src/foo.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { foo } from './foo'

describe('foo', () => {
it('should return "foo"', () => {
expect(foo()).toEqual('foo')
})
})
3 changes: 3 additions & 0 deletions e2e/tsconfig-projects/src/foo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 'foo'
}
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"include": [],
"references": [
{ "path": "./tsconfig.src.json" },
{ "path": "./tsconfig.spec.json" }
]
}
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"module": "Node16",
"typeRoots": ["../node_modules"],
"types": ["@types/jest", "@types/node"],
},
"include": ["src/**/*.spec.ts"]
}
8 changes: 8 additions & 0 deletions e2e/tsconfig-projects/tsconfig.src.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "Node16"
},
"include": ["src"],
"exclude": ["src/**/*.spec.ts"]
}
6 changes: 5 additions & 1 deletion examples/monorepo-app/project-1/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "../tsconfig-esm.base.json"
"extends": "../tsconfig-esm.json",
"compilerOptions": {
"types": ["jest"]
},
"include": ["test"]
}
8 changes: 6 additions & 2 deletions examples/monorepo-app/project-1/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "../tsconfig.base.json"
}
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["jest"]
},
"include": ["test"]
}
6 changes: 5 additions & 1 deletion examples/monorepo-app/project-2/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "../tsconfig-esm.base.json"
"extends": "../tsconfig-esm.json",
"compilerOptions": {
"types": ["jest"]
},
"include": ["test"]
}
6 changes: 5 additions & 1 deletion examples/monorepo-app/project-2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "../tsconfig.base.json"
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["jest"]
},
"include": ["test"]
}
4 changes: 4 additions & 0 deletions examples/monorepo-app/shared/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../tsconfig.json",
"include": ["."]
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.base.json",
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"esModuleInterop": true
}
},
"include": [],
}
10 changes: 0 additions & 10 deletions examples/monorepo-app/tsconfig.base.json

This file was deleted.

22 changes: 22 additions & 0 deletions examples/monorepo-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "@tsconfig/strictest/tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"target": "ESNext",
"module": "Node16",
"lib": ["ESNext", "dom"],
"isolatedModules": false
},
"include": [],
"references": [
{
"path": "./project-1/tsconfig.json"
},
{
"path": "./project-2/tsconfig.json"
},
{
"path": "./shared/tsconfig.json"
}
]
}
2 changes: 0 additions & 2 deletions src/legacy/config/config-set.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ describe('_resolveTsConfig', () => {
expect(conf.options.configFilePath).toBeUndefined()
expect(findConfig).not.toHaveBeenCalled()
expect(readConfig.mock.calls[0][0]).toBe('/foo/tsconfig.bar.json')
expect(parseConfig).not.toHaveBeenCalled()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will now be called to find out if we there are project references in the tsconfig to check.

})
})

Expand Down Expand Up @@ -917,7 +916,6 @@ describe('_resolveTsConfig', () => {

const conf = cs.parsedTsConfig
expect(conf.options.path).toBe(tscfgPathStub)
expect(findConfig).not.toHaveBeenCalled()
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will now be called to find out if we there are project references in the tsconfig to check.

expect(readConfig.mock.calls[0][0]).toBe(tscfgPathStub)
expect(parseConfig.mock.calls[0][2]).toBe('/foo')
expect(parseConfig.mock.calls[0][4]).toBe(tscfgPathStub)
Expand Down
55 changes: 51 additions & 4 deletions src/legacy/config/config-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,7 @@ export class ConfigSet {
let basePath = normalizeSlashes(this.rootDir)
const ts = this.compilerModule
// Read project configuration when available.
this.tsconfigFilePath = resolvedConfigFile
? normalizeSlashes(resolvedConfigFile)
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)
this.tsconfigFilePath = this._findTsconfigFile(resolvedConfigFile)
if (this.tsconfigFilePath) {
this.logger.debug({ tsConfigFileName: this.tsconfigFilePath }, 'readTsConfig(): reading', this.tsconfigFilePath)

Expand All @@ -596,7 +594,56 @@ export class ConfigSet {
}

// parse json, merge config extending others, ...
return ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, this.tsconfigFilePath)
return this._parseTsconfig(config, basePath, this.tsconfigFilePath)
}

protected _findTsconfigFile(resolvedConfigFile?: string) {
const ts = this.compilerModule

const configFileName: string | undefined = resolvedConfigFile
? normalizeSlashes(resolvedConfigFile)
: ts.findConfigFile(normalizeSlashes(this.rootDir), ts.sys.fileExists)

const newTsconfigFile = this._findReferenceTsconfig(configFileName)

return newTsconfigFile ?? configFileName
}

protected _findReferenceTsconfig(tsconfigFileName?: string): string | undefined {
const ts = this.compilerModule

if (!tsconfigFileName) return

const parsedTsconfig = this._parseTsconfig(
ts.readConfigFile(tsconfigFileName, ts.sys.readFile).config || {},
dirname(tsconfigFileName),
tsconfigFileName,
)

if (this._includesTestFilesInConfig(parsedTsconfig)) return tsconfigFileName

if (parsedTsconfig.projectReferences) {
for (const ref of parsedTsconfig.projectReferences) {
const filePath = ts.resolveProjectReferencePath(ref)

if (ts.sys.fileExists(filePath)) {
const newTsconfigFileName = this._findReferenceTsconfig(ts.resolveProjectReferencePath(ref))
if (newTsconfigFileName) return newTsconfigFileName
}
}
}

return
}

protected _includesTestFilesInConfig(parsedConfig?: ts.ParsedCommandLine) {
return parsedConfig?.fileNames?.length ? parsedConfig.fileNames.some((path) => this.isTestFile(path)) : false
}

protected _parseTsconfig(config: unknown, basePath: string, configFileName = this.tsconfigFilePath) {
const ts = this.compilerModule

return ts.parseJsonConfigFileContent(config, ts.sys, basePath, undefined, configFileName)
}

isTestFile(fileName: string): boolean {
Expand Down
Loading