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

feat(type-check): allow to check multiple tsconfig files #2684

Merged
merged 2 commits into from
Jun 24, 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
2 changes: 2 additions & 0 deletions .github/workflows/release-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ name: Release Nightly

on:
workflow_dispatch:
# Nightly release disabled
# schedule:
# 00:00 AM Beijing Time.
# - cron: "0 16 * * *"

permissions:
# To publish packages with provenance
id-token: write

jobs:
Expand Down
30 changes: 30 additions & 0 deletions e2e/cases/type-check/multiple-tsconfig/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { build } from '@e2e/helper';
import { proxyConsole } from '@e2e/helper';
import { expect, test } from '@playwright/test';

test('should check multiple tsconfig.json as expected', async () => {
const { logs, restore } = proxyConsole();
await expect(
build({
cwd: __dirname,
}),
).rejects.toThrowError('build failed!');

expect(
logs.find((log) =>
log.includes(
`Argument of type 'string' is not assignable to parameter of type 'number'.`,
),
),
).toBeTruthy();

expect(
logs.find((log) =>
log.includes(
`Argument of type '{}' is not assignable to parameter of type 'number'.`,
),
),
).toBeTruthy();

restore();
});
21 changes: 21 additions & 0 deletions e2e/cases/type-check/multiple-tsconfig/rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig } from '@rsbuild/core';
import { pluginTypeCheck } from '@rsbuild/plugin-type-check';

export default defineConfig({
plugins: [pluginTypeCheck()],
environments: {
web: {
output: {
target: 'web',
},
},
node: {
source: {
tsconfigPath: './tsconfig.server.json',
},
output: {
target: 'node',
},
},
},
});
4 changes: 4 additions & 0 deletions e2e/cases/type-check/multiple-tsconfig/server/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const add = (a: number, b: number) => a + b;

// this is a type error
add(1, '2');
4 changes: 4 additions & 0 deletions e2e/cases/type-check/multiple-tsconfig/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const add = (a: number, b: number) => a + b;

// this is a type error
add(1, {});
12 changes: 12 additions & 0 deletions e2e/cases/type-check/multiple-tsconfig/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@rsbuild/config/tsconfig",
"compilerOptions": {
"jsx": "react-jsx",
"baseUrl": "./",
"outDir": "./dist",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"]
}
12 changes: 12 additions & 0 deletions e2e/cases/type-check/multiple-tsconfig/tsconfig.server.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"extends": "@rsbuild/config/tsconfig",
"compilerOptions": {
"jsx": "react-jsx",
"baseUrl": "./",
"outDir": "./dist",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["server"]
}
17 changes: 14 additions & 3 deletions packages/plugin-type-check/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export const pluginTypeCheck = (
name: PLUGIN_TYPE_CHECK_NAME,

setup(api) {
const checkedTsconfig = new Map<
// tsconfig path
string,
// environment
string
>();

api.modifyBundlerChain(async (chain, { isProd, environment }) => {
const { enable = true, forkTsCheckerOptions } = options;
const { tsconfigPath } = api.context.environments[environment];
Expand All @@ -42,11 +49,15 @@ export const pluginTypeCheck = (
return;
}

// If there is multiple environment, only apply type checker to the first target
// to avoid multiple type checker running at the same time
if (environment !== Object.keys(api.context.environments)[0]) {
// If there are identical tsconfig.json files,
// apply type checker only once to avoid duplicate checks.
if (
checkedTsconfig.has(tsconfigPath) &&
checkedTsconfig.get(tsconfigPath) !== environment
) {
return;
}
checkedTsconfig.set(tsconfigPath, environment);

// use typescript of user project
let typescriptPath: string;
Expand Down
Loading