11import fs from 'node:fs' ;
2- import { createRequire } from 'node:module' ;
32import type { RsbuildPlugin } from '@rsbuild/core' ;
43import deepmerge from 'deepmerge' ;
54import json5 from 'json5' ;
65import { type ConfigChain , reduceConfigs } from 'reduce-configs' ;
76import { TsCheckerRspackPlugin } from 'ts-checker-rspack-plugin' ;
87
9- const require = createRequire ( import . meta. url ) ;
10-
118type 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
15012export 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