Skip to content

Commit 409c700

Browse files
committed
fix: delegate TypeScript runtime resolution
1 parent b8c3504 commit 409c700

4 files changed

Lines changed: 17 additions & 177 deletions

File tree

README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,8 @@ const defaultOptions = {
117117
memoryLimit: 8192,
118118
// use tsconfig of user project
119119
configFile: tsconfigPath,
120-
// use TypeScript Go automatically when TypeScript 7+ is installed
121-
tsgo: typescriptMajorVersion >= 7 ? true : false,
122-
// use TypeScript of user project
123-
typescriptPath:
124-
typescriptMajorVersion >= 7
125-
? require.resolve('typescript/package.json')
126-
: require.resolve('typescript'),
120+
// resolve the default TypeScript package from user project
121+
resolveRoot: api.context.rootPath,
127122
},
128123
issue: {
129124
// ignore types errors from node_modules
@@ -135,7 +130,11 @@ const defaultOptions = {
135130
// we only want to display error messages
136131
},
137132
error(message: string) {
138-
console.error(message.replace(/ERROR/g, 'Type Error'));
133+
console.error(
134+
message
135+
.replace(/ERROR/g, 'Type Error')
136+
.replace(/WARNING/g, 'Type Warning'),
137+
);
139138
},
140139
},
141140
};
@@ -170,7 +169,7 @@ pluginTypeCheck({
170169
});
171170
```
172171

173-
When `tsgo` is enabled, `typescript.typescriptPath` must point to an absolute `typescript/package.json` path from TypeScript 7+ or `@typescript/native-preview/package.json`. If TypeScript 7+ is not installed, the default path falls back to `@typescript/native-preview/package.json`.
172+
When `tsgo` is enabled and `typescript.typescriptPath` is set manually, it must point to an absolute `typescript/package.json` path from TypeScript 7+ or `@typescript/native-preview/package.json`.
174173

175174
> The `@typescript/native-preview` usage is deprecated and kept only for compatibility. We recommend installing `typescript@rc` to use `tsgo`.
176175

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"deepmerge": "^4.3.1",
3636
"json5": "^2.2.3",
3737
"reduce-configs": "^1.1.2",
38-
"ts-checker-rspack-plugin": "^1.5.0"
38+
"ts-checker-rspack-plugin": "^1.5.1"
3939
},
4040
"devDependencies": {
4141
"@playwright/test": "^1.61.0",

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 3 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,13 @@
11
import fs from 'node:fs';
2-
import { createRequire } from 'node:module';
32
import type { RsbuildPlugin } from '@rsbuild/core';
43
import deepmerge from 'deepmerge';
54
import json5 from 'json5';
65
import { type ConfigChain, reduceConfigs } from 'reduce-configs';
76
import { TsCheckerRspackPlugin } from 'ts-checker-rspack-plugin';
87

9-
const require = createRequire(import.meta.url);
10-
118
type TsCheckerOptions = NonNullable<
129
ConstructorParameters<typeof TsCheckerRspackPlugin>[0]
1310
>;
14-
type TypeScriptGoPackage = 'typescript' | 'preview';
15-
type TypeScriptOptions = NonNullable<TsCheckerOptions['typescript']>;
16-
type TypeScriptOptionsWithTsgoPackage = TypeScriptOptions & {
17-
tsgoPackage?: TypeScriptGoPackage;
18-
};
19-
20-
type ProjectTypeScriptPaths = {
21-
typescriptPath?: string;
22-
packageJsonPath?: string;
23-
previewPackageJsonPath?: string;
24-
supportsTsgo: boolean;
25-
};
26-
27-
const TYPESCRIPT_PACKAGE = 'typescript';
28-
const TYPESCRIPT_PACKAGE_JSON = `${TYPESCRIPT_PACKAGE}/package.json`;
29-
const TYPESCRIPT_PREVIEW_PACKAGE = '@typescript/native-preview';
30-
const TYPESCRIPT_PREVIEW_PACKAGE_JSON = `${TYPESCRIPT_PREVIEW_PACKAGE}/package.json`;
31-
32-
const resolveProjectPackage = (
33-
packageName: string,
34-
rootPath: string,
35-
): string | undefined => {
36-
try {
37-
return require.resolve(packageName, {
38-
paths: [rootPath],
39-
});
40-
} catch {
41-
return undefined;
42-
}
43-
};
44-
45-
const getTypeScriptGoPackage = (
46-
packageJsonPath: string | undefined,
47-
): TypeScriptGoPackage | undefined => {
48-
if (!packageJsonPath) {
49-
return undefined;
50-
}
51-
52-
try {
53-
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'));
54-
const version =
55-
typeof packageJson.version === 'string' ? packageJson.version : '';
56-
const versionMatch = version.match(/^(\d+)\.(\d+)(?:\.|$|-)/);
57-
58-
if (
59-
packageJson.name === TYPESCRIPT_PACKAGE &&
60-
versionMatch &&
61-
Number(versionMatch[1]) >= 7
62-
) {
63-
return 'typescript';
64-
}
65-
66-
if (packageJson.name === TYPESCRIPT_PREVIEW_PACKAGE) {
67-
return 'preview';
68-
}
69-
70-
return undefined;
71-
} catch {
72-
return undefined;
73-
}
74-
};
75-
76-
const isTypeScriptGoSupportedPackage = (
77-
packageJsonPath: string | undefined,
78-
): boolean => getTypeScriptGoPackage(packageJsonPath) === 'typescript';
79-
80-
const resolveProjectTypeScriptPaths = (
81-
rootPath: string,
82-
): ProjectTypeScriptPaths => {
83-
const typescriptPath = resolveProjectPackage(TYPESCRIPT_PACKAGE, rootPath);
84-
const packageJsonPath = resolveProjectPackage(
85-
TYPESCRIPT_PACKAGE_JSON,
86-
rootPath,
87-
);
88-
const previewPackageJsonPath = resolveProjectPackage(
89-
TYPESCRIPT_PREVIEW_PACKAGE_JSON,
90-
rootPath,
91-
);
92-
const supportsTsgo = isTypeScriptGoSupportedPackage(packageJsonPath);
93-
94-
return {
95-
typescriptPath,
96-
packageJsonPath,
97-
previewPackageJsonPath,
98-
supportsTsgo,
99-
};
100-
};
101-
102-
const applyTypeScriptDefaults = (
103-
typescriptOptions: TypeScriptOptions | undefined,
104-
projectPaths: ProjectTypeScriptPaths,
105-
): boolean => {
106-
if (!typescriptOptions) {
107-
return false;
108-
}
109-
110-
const configuredPath = typescriptOptions.typescriptPath;
111-
const normalizedOptions =
112-
typescriptOptions as TypeScriptOptionsWithTsgoPackage;
113-
114-
if (configuredPath) {
115-
const tsgoPackage = getTypeScriptGoPackage(configuredPath);
116-
117-
if (typescriptOptions.tsgo === undefined && tsgoPackage === 'typescript') {
118-
typescriptOptions.tsgo = true;
119-
}
120-
121-
if (typescriptOptions.tsgo === true && tsgoPackage) {
122-
normalizedOptions.tsgoPackage = tsgoPackage;
123-
}
124-
125-
return Boolean(typescriptOptions.tsgo);
126-
}
127-
128-
if (typescriptOptions.tsgo === false) {
129-
typescriptOptions.typescriptPath = projectPaths.typescriptPath;
130-
return false;
131-
}
132-
133-
if (projectPaths.supportsTsgo) {
134-
typescriptOptions.typescriptPath = projectPaths.packageJsonPath;
135-
typescriptOptions.tsgo = true;
136-
normalizedOptions.tsgoPackage = 'typescript';
137-
return true;
138-
}
139-
140-
if (typescriptOptions.tsgo === true) {
141-
typescriptOptions.typescriptPath = projectPaths.previewPackageJsonPath;
142-
normalizedOptions.tsgoPackage = 'preview';
143-
return true;
144-
}
145-
146-
typescriptOptions.typescriptPath = projectPaths.typescriptPath;
147-
return false;
148-
};
14911

15012
export type PluginTypeCheckerOptions = {
15113
/**
@@ -217,9 +79,6 @@ export const pluginTypeCheck = (
21779
);
21880
const useReference =
21981
Array.isArray(references) && references.length > 0;
220-
const projectTypescriptPaths = resolveProjectTypeScriptPaths(
221-
api.context.rootPath,
222-
);
22382

22483
const defaultOptions: TsCheckerOptions = {
22584
typescript: {
@@ -232,6 +91,8 @@ export const pluginTypeCheck = (
23291
memoryLimit: 8192,
23392
// use tsconfig of user project
23493
configFile: tsconfigPath,
94+
// resolve the default TypeScript package from user project
95+
resolveRoot: api.context.rootPath,
23596
},
23697
issue: {
23798
// ignore types errors from node_modules
@@ -258,28 +119,8 @@ export const pluginTypeCheck = (
258119
mergeFn: deepmerge,
259120
});
260121

261-
const typescriptOptions = mergedOptions.typescript;
262-
const isTypeScriptGoEnabled = applyTypeScriptDefaults(
263-
typescriptOptions,
264-
projectTypescriptPaths,
265-
);
266-
267-
if (typescriptOptions && !typescriptOptions.typescriptPath) {
268-
const typeCheckerPackage = isTypeScriptGoEnabled
269-
? TYPESCRIPT_PREVIEW_PACKAGE
270-
: TYPESCRIPT_PACKAGE;
271-
logger.warn(
272-
`"${typeCheckerPackage}" is not found in current project, Type checker will not work.`,
273-
);
274-
return;
275-
}
276-
277122
if (isProd) {
278-
logger.info(
279-
isTypeScriptGoEnabled
280-
? 'Type checker is enabled.'
281-
: 'Type checker is enabled. It may take some time. You can enable `typescript.tsgo` to speed up type checking.',
282-
);
123+
logger.info('Type checker is enabled.');
283124
}
284125

285126
chain

0 commit comments

Comments
 (0)