@rsbuild/plugin-type-check 1.0.1-rc.5 → 1.1.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +230 -9
- package/dist/index.cjs +136 -115
- package/{dist-types → dist}/index.d.ts +4 -4
- package/dist/index.js +77 -97
- package/package.json +45 -26
- package/dist-types/package.json +0 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2024 Rspack Contrib
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -1,19 +1,240 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
# @rsbuild/plugin-type-check
|
|
2
|
+
|
|
3
|
+
An Rsbuild plugin to run TypeScript type checker in a separate process.
|
|
4
|
+
|
|
5
|
+
<p>
|
|
6
|
+
<a href="https://npmjs.com/package/@rsbuild/plugin-type-check">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/@rsbuild/plugin-type-check?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
|
|
8
|
+
</a>
|
|
9
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
|
|
10
|
+
<a href="https://npmcharts.com/compare/@rsbuild/plugin-type-check?minimal=true"><img src="https://img.shields.io/npm/dm/@rsbuild/plugin-type-check.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
|
|
3
11
|
</p>
|
|
4
12
|
|
|
5
|
-
|
|
13
|
+
## Introduction
|
|
14
|
+
|
|
15
|
+
This plugin internally integrates with [ts-checker-rspack-plugin](https://github.com/rspack-team/ts-checker-rspack-plugin).
|
|
16
|
+
|
|
17
|
+
The type checking logic of `ts-checker-rspack-plugin` is similar to the native `tsc` command of TypeScript. It automatically reads the configuration options from `tsconfig.json` and can also be modified via the configuration options provided by the Type Check plugin.
|
|
18
|
+
|
|
19
|
+
The behavior of the plugin differs in the development and production builds:
|
|
20
|
+
|
|
21
|
+
- In development mode, type errors do not block the build process. They are only logged in the terminal.
|
|
22
|
+
- In production mode, type errors cause the build to fail in order to ensure the stability of the production code.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
Install:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm add @rsbuild/plugin-type-check -D
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Add plugin to your `rsbuild.config.ts`:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
// rsbuild.config.ts
|
|
36
|
+
import { pluginTypeCheck } from "@rsbuild/plugin-type-check";
|
|
37
|
+
|
|
38
|
+
export default {
|
|
39
|
+
plugins: [pluginTypeCheck()],
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Configuring tsconfig.json
|
|
44
|
+
|
|
45
|
+
The Type Check plugin by default performs checks based on the `tsconfig.json` file in the root directory of the current project. Below is an example of a `tsconfig.json` file, which you can also adjust according to the needs of your project.
|
|
46
|
+
|
|
47
|
+
```json title="tsconfig.json"
|
|
48
|
+
{
|
|
49
|
+
"compilerOptions": {
|
|
50
|
+
"target": "ES2020",
|
|
51
|
+
"lib": ["DOM", "ES2020"],
|
|
52
|
+
"module": "ESNext",
|
|
53
|
+
"strict": true,
|
|
54
|
+
"skipLibCheck": true,
|
|
55
|
+
"isolatedModules": true,
|
|
56
|
+
"resolveJsonModule": true,
|
|
57
|
+
"moduleResolution": "bundler"
|
|
58
|
+
},
|
|
59
|
+
"include": ["src"]
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Please note that the fields in `tsconfig.json` will not affect the compilation behavior and output of Rsbuild, but will only affect the results of type checking.
|
|
64
|
+
|
|
65
|
+
## Options
|
|
66
|
+
|
|
67
|
+
### enable
|
|
68
|
+
|
|
69
|
+
Whether to enable TypeScript type checking.
|
|
70
|
+
|
|
71
|
+
- **Type:** `boolean`
|
|
72
|
+
- **Default:** `true`
|
|
73
|
+
- **Example:**
|
|
74
|
+
|
|
75
|
+
Disable TypeScript type checking:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
pluginTypeCheck({
|
|
79
|
+
enable: false,
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Enable type checking only in production mode:
|
|
84
|
+
|
|
85
|
+
```js
|
|
86
|
+
pluginTypeCheck({
|
|
87
|
+
enable: process.env.NODE_ENV === "production",
|
|
88
|
+
});
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Enable type checking only in development mode (it is not recommended to disable type checking in production mode, as it may reduce the stability of the production code):
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
pluginTypeCheck({
|
|
95
|
+
enable: process.env.NODE_ENV === "development",
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### forkTsCheckerOptions
|
|
100
|
+
|
|
101
|
+
To modify the options of `ts-checker-rspack-plugin`, please refer to [ts-checker-rspack-plugin - README](https://github.com/rspack-team/ts-checker-rspack-plugin#readme) to learn about available options.
|
|
102
|
+
|
|
103
|
+
- **Type:** `Object | Function`
|
|
104
|
+
- **Default:**
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
const defaultOptions = {
|
|
108
|
+
typescript: {
|
|
109
|
+
// set 'readonly' to avoid emitting tsbuildinfo,
|
|
110
|
+
// as the generated tsbuildinfo will break ts-checker-rspack-plugin
|
|
111
|
+
mode: "readonly",
|
|
112
|
+
// enable build when using project reference
|
|
113
|
+
build: useReference,
|
|
114
|
+
// avoid OOM issue
|
|
115
|
+
memoryLimit: 8192,
|
|
116
|
+
// use tsconfig of user project
|
|
117
|
+
configFile: tsconfigPath,
|
|
118
|
+
// use typescript of user project
|
|
119
|
+
typescriptPath: require.resolve("typescript"),
|
|
120
|
+
},
|
|
121
|
+
issue: {
|
|
122
|
+
// ignore types errors from node_modules
|
|
123
|
+
exclude: [({ file = "" }) => /[\\/]node_modules[\\/]/.test(file)],
|
|
124
|
+
},
|
|
125
|
+
logger: {
|
|
126
|
+
log() {
|
|
127
|
+
// do nothing
|
|
128
|
+
// we only want to display error messages
|
|
129
|
+
},
|
|
130
|
+
error(message: string) {
|
|
131
|
+
console.error(message.replace(/ERROR/g, "Type Error"));
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### Object Type
|
|
138
|
+
|
|
139
|
+
When the value of `forkTsCheckerOptions` is an object, it will be deeply merged with the default configuration.
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
pluginTypeCheck({
|
|
143
|
+
forkTsCheckerOptions: {
|
|
144
|
+
issue: {
|
|
145
|
+
exclude: [({ file = "" }) => /[\\/]some-folder[\\/]/.test(file)],
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### Function Type
|
|
152
|
+
|
|
153
|
+
When the value of `forkTsCheckerOptions` is a function, the default configuration will be passed as the first argument. You can directly modify the configuration object or return an object as the final configuration.
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
pluginTypeCheck({
|
|
157
|
+
forkTsCheckerOptions(options) {
|
|
158
|
+
options.async = false;
|
|
159
|
+
return options;
|
|
160
|
+
},
|
|
161
|
+
});
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
#### exclude Example
|
|
165
|
+
|
|
166
|
+
The `exclude` option can filter based on the `code`, `message`, or `file` from TS errors.
|
|
167
|
+
|
|
168
|
+
For example, the type mismatch error can be excluded using `code: 'TS2345'`:
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
pluginTypeCheck({
|
|
172
|
+
forkTsCheckerOptions: {
|
|
173
|
+
issue: {
|
|
174
|
+
// Ignore "Argument of type 'string' is not assignable to parameter of type 'number'.ts(2345)"
|
|
175
|
+
exclude: [{ code: "TS2345" }],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Or exclude files under `/some-folder/` using `file`:
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
pluginTypeCheck({
|
|
185
|
+
forkTsCheckerOptions: {
|
|
186
|
+
issue: {
|
|
187
|
+
exclude: [({ file = "" }) => /[\\/]some-folder[\\/]/.test(file)],
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Notes
|
|
194
|
+
|
|
195
|
+
- If you have enabled `ts-loader` in your project and manually configured `compileOnly: false`, please disable the Type Check plugin to avoid duplicate type checking.
|
|
196
|
+
- Some errors will be displayed as warnings in IDEs such as VS Code, but they will still be displayed as errors in the `ts-checker-rspack-plugin` check. For details, please refer to: [Why are some errors reported as warnings?](https://code.visualstudio.com/docs/typescript/typescript-compiling#_why-are-some-errors-reported-as-warnings).
|
|
197
|
+
|
|
198
|
+
## Performance Optimization
|
|
199
|
+
|
|
200
|
+
Type checking has a significant performance overhead. You can refer to the [Performance Guide](https://github.com/microsoft/TypeScript/wiki/Performance) in the official TypeScript documentation for performance optimization.
|
|
201
|
+
|
|
202
|
+
For example, properly configuring the `include` and `exclude` scopes in `tsconfig.json` can significantly reduce unnecessary type checking and improve TypeScript performance:
|
|
203
|
+
|
|
204
|
+
```json title="tsconfig.json"
|
|
205
|
+
{
|
|
206
|
+
"include": ["src"],
|
|
207
|
+
"exclude": ["**/node_modules", "**/.*/"]
|
|
208
|
+
}
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
## Vue Components
|
|
212
|
+
|
|
213
|
+
`ts-checker-rspack-plugin` does not support checking TypeScript code in `.vue` components. You can check for type issues in `.vue` files in the following ways:
|
|
214
|
+
|
|
215
|
+
1. Install the [vue-tsc](https://github.com/vuejs/language-tools/tree/master/packages/tsc) package, which provides the ability to check types in `.vue` files.
|
|
6
216
|
|
|
7
|
-
|
|
217
|
+
<PackageManagerTabs command="add vue-tsc -D" />
|
|
8
218
|
|
|
9
|
-
|
|
219
|
+
2. Add the `vue-tsc --noEmit` command to the `build` script in package.json:
|
|
10
220
|
|
|
11
|
-
|
|
221
|
+
```diff title="package.json"
|
|
222
|
+
{
|
|
223
|
+
"scripts": {
|
|
224
|
+
- "build": "rsbuild build"
|
|
225
|
+
+ "build": "vue-tsc --noEmit && rsbuild build"
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
```
|
|
12
229
|
|
|
13
|
-
|
|
230
|
+
3. Since the production build uses `vue-tsc` for type checking, you can disable the Type Check plugin in production mode to avoid redundant checks.
|
|
14
231
|
|
|
15
|
-
|
|
232
|
+
```js
|
|
233
|
+
pluginTypeCheck({
|
|
234
|
+
enable: process.env.NODE_ENV === "development",
|
|
235
|
+
});
|
|
236
|
+
```
|
|
16
237
|
|
|
17
238
|
## License
|
|
18
239
|
|
|
19
|
-
|
|
240
|
+
[MIT](./LICENSE).
|
package/dist/index.cjs
CHANGED
|
@@ -1,120 +1,141 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
));
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
2
|
+
// The require scope
|
|
3
|
+
var __webpack_require__ = {};
|
|
4
|
+
/************************************************************************/ // webpack/runtime/compat_get_default_export
|
|
5
|
+
(()=>{
|
|
6
|
+
// getDefaultExport function for compatibility with non-ESM modules
|
|
7
|
+
__webpack_require__.n = function(module1) {
|
|
8
|
+
var getter = module1 && module1.__esModule ? function() {
|
|
9
|
+
return module1['default'];
|
|
10
|
+
} : function() {
|
|
11
|
+
return module1;
|
|
12
|
+
};
|
|
13
|
+
__webpack_require__.d(getter, {
|
|
14
|
+
a: getter
|
|
15
|
+
});
|
|
16
|
+
return getter;
|
|
17
|
+
};
|
|
18
|
+
})();
|
|
19
|
+
// webpack/runtime/define_property_getters
|
|
20
|
+
(()=>{
|
|
21
|
+
__webpack_require__.d = function(exports1, definition) {
|
|
22
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
get: definition[key]
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
})();
|
|
28
|
+
// webpack/runtime/has_own_property
|
|
29
|
+
(()=>{
|
|
30
|
+
__webpack_require__.o = function(obj, prop) {
|
|
31
|
+
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
32
|
+
};
|
|
33
|
+
})();
|
|
34
|
+
// webpack/runtime/make_namespace_object
|
|
35
|
+
(()=>{
|
|
36
|
+
// define __esModule on exports
|
|
37
|
+
__webpack_require__.r = function(exports1) {
|
|
38
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
39
|
+
value: 'Module'
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(exports1, '__esModule', {
|
|
42
|
+
value: true
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
})();
|
|
46
|
+
/************************************************************************/ var __webpack_exports__ = {};
|
|
47
|
+
// ESM COMPAT FLAG
|
|
48
|
+
__webpack_require__.r(__webpack_exports__);
|
|
49
|
+
// EXPORTS
|
|
50
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
51
|
+
PLUGIN_TYPE_CHECK_NAME: ()=>/* binding */ PLUGIN_TYPE_CHECK_NAME,
|
|
52
|
+
pluginTypeCheck: ()=>/* binding */ pluginTypeCheck
|
|
35
53
|
});
|
|
36
|
-
|
|
37
|
-
var
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
const external_node_fs_namespaceObject = require("node:fs");
|
|
55
|
+
var external_node_fs_default = /*#__PURE__*/ __webpack_require__.n(external_node_fs_namespaceObject);
|
|
56
|
+
const external_node_module_namespaceObject = require("node:module");
|
|
57
|
+
const core_namespaceObject = require("@rsbuild/core");
|
|
58
|
+
const external_deepmerge_namespaceObject = require("deepmerge");
|
|
59
|
+
var external_deepmerge_default = /*#__PURE__*/ __webpack_require__.n(external_deepmerge_namespaceObject);
|
|
60
|
+
const external_json5_namespaceObject = require("json5");
|
|
61
|
+
var external_json5_default = /*#__PURE__*/ __webpack_require__.n(external_json5_namespaceObject);
|
|
62
|
+
const external_reduce_configs_namespaceObject = require("reduce-configs");
|
|
63
|
+
const external_ts_checker_rspack_plugin_namespaceObject = require("ts-checker-rspack-plugin");
|
|
64
|
+
const src_require = (0, external_node_module_namespaceObject.createRequire)(/*#__PURE__*/ function() {
|
|
65
|
+
return 'undefined' == typeof document ? new (module.require('url'.replace('', ''))).URL('file:' + __filename).href : document.currentScript && document.currentScript.src || new URL('main.js', document.baseURI).href;
|
|
66
|
+
}());
|
|
67
|
+
const PLUGIN_TYPE_CHECK_NAME = 'rsbuild:type-check';
|
|
68
|
+
const pluginTypeCheck = (options = {})=>({
|
|
69
|
+
name: PLUGIN_TYPE_CHECK_NAME,
|
|
70
|
+
setup (api) {
|
|
71
|
+
const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/;
|
|
72
|
+
const checkedTsconfig = new Map();
|
|
73
|
+
api.modifyBundlerChain(async (chain, { isProd, environment, CHAIN_ID })=>{
|
|
74
|
+
const { enable = true, forkTsCheckerOptions } = options;
|
|
75
|
+
const { tsconfigPath } = environment;
|
|
76
|
+
if (!tsconfigPath || false === enable) return;
|
|
77
|
+
// If there are identical tsconfig.json files,
|
|
78
|
+
// apply type checker only once to avoid duplicate checks.
|
|
79
|
+
if (checkedTsconfig.has(tsconfigPath) && checkedTsconfig.get(tsconfigPath) !== environment.name) return;
|
|
80
|
+
checkedTsconfig.set(tsconfigPath, environment.name);
|
|
81
|
+
// use typescript of user project
|
|
82
|
+
let typescriptPath;
|
|
83
|
+
try {
|
|
84
|
+
typescriptPath = src_require.resolve('typescript', {
|
|
85
|
+
paths: [
|
|
86
|
+
api.context.rootPath
|
|
87
|
+
]
|
|
88
|
+
});
|
|
89
|
+
} catch (err) {
|
|
90
|
+
core_namespaceObject.logger.warn('"typescript" is not found in current project, Type checker will not work.');
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const { references } = external_json5_default().parse(external_node_fs_default().readFileSync(tsconfigPath, 'utf-8'));
|
|
94
|
+
const useReference = Array.isArray(references) && references.length > 0;
|
|
95
|
+
const defaultOptions = {
|
|
96
|
+
typescript: {
|
|
97
|
+
// set 'readonly' to avoid emitting tsbuildinfo,
|
|
98
|
+
// as the generated tsbuildinfo will break ts-checker-rspack-plugin
|
|
99
|
+
mode: 'readonly',
|
|
100
|
+
// enable build when using project reference
|
|
101
|
+
build: useReference,
|
|
102
|
+
// avoid OOM issue
|
|
103
|
+
memoryLimit: 8192,
|
|
104
|
+
// use tsconfig of user project
|
|
105
|
+
configFile: tsconfigPath,
|
|
106
|
+
// use typescript of user project
|
|
107
|
+
typescriptPath
|
|
108
|
+
},
|
|
109
|
+
issue: {
|
|
110
|
+
// ignore types errors from node_modules
|
|
111
|
+
exclude: [
|
|
112
|
+
({ file = '' })=>NODE_MODULES_REGEX.test(file)
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
logger: {
|
|
116
|
+
log () {
|
|
117
|
+
// do nothing
|
|
118
|
+
// we only want to display error messages
|
|
119
|
+
},
|
|
120
|
+
error (message) {
|
|
121
|
+
console.error(message.replace(/ERROR/g, 'Type Error'));
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const typeCheckerOptions = (0, external_reduce_configs_namespaceObject.reduceConfigs)({
|
|
126
|
+
initial: defaultOptions,
|
|
127
|
+
config: forkTsCheckerOptions,
|
|
128
|
+
mergeFn: external_deepmerge_default()
|
|
129
|
+
});
|
|
130
|
+
if (isProd) core_namespaceObject.logger.info('Type checker is enabled. It may take some time.');
|
|
131
|
+
chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(external_ts_checker_rspack_plugin_namespaceObject.TsCheckerRspackPlugin, [
|
|
132
|
+
typeCheckerOptions
|
|
133
|
+
]);
|
|
63
134
|
});
|
|
64
|
-
} catch (err) {
|
|
65
|
-
import_core.logger.warn(
|
|
66
|
-
'"typescript" is not found in current project, Type checker will not work.'
|
|
67
|
-
);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
const { default: ForkTsCheckerWebpackPlugin } = await import("fork-ts-checker-webpack-plugin");
|
|
71
|
-
const { default: json5 } = await import("json5");
|
|
72
|
-
const { references } = json5.parse(
|
|
73
|
-
import_node_fs.default.readFileSync(tsconfigPath, "utf-8")
|
|
74
|
-
);
|
|
75
|
-
const useReference = Array.isArray(references) && references.length > 0;
|
|
76
|
-
const defaultOptions = {
|
|
77
|
-
typescript: {
|
|
78
|
-
// set 'readonly' to avoid emitting tsbuildinfo,
|
|
79
|
-
// as the generated tsbuildinfo will break fork-ts-checker
|
|
80
|
-
mode: "readonly",
|
|
81
|
-
// enable build when using project reference
|
|
82
|
-
build: useReference,
|
|
83
|
-
// avoid OOM issue
|
|
84
|
-
memoryLimit: 8192,
|
|
85
|
-
// use tsconfig of user project
|
|
86
|
-
configFile: tsconfigPath,
|
|
87
|
-
// use typescript of user project
|
|
88
|
-
typescriptPath
|
|
89
|
-
},
|
|
90
|
-
issue: {
|
|
91
|
-
// ignore types errors from node_modules
|
|
92
|
-
exclude: [({ file = "" }) => NODE_MODULES_REGEX.test(file)]
|
|
93
|
-
},
|
|
94
|
-
logger: {
|
|
95
|
-
log() {
|
|
96
|
-
},
|
|
97
|
-
error(message) {
|
|
98
|
-
console.error(message.replace(/ERROR/g, "Type Error"));
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
const typeCheckerOptions = (0, import_reduce_configs.reduceConfigs)({
|
|
103
|
-
initial: defaultOptions,
|
|
104
|
-
config: forkTsCheckerOptions,
|
|
105
|
-
mergeFn: import_deepmerge.default
|
|
106
|
-
});
|
|
107
|
-
if (isProd) {
|
|
108
|
-
import_core.logger.info("Type checker is enabled. It may take some time.");
|
|
109
|
-
}
|
|
110
|
-
chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(ForkTsCheckerWebpackPlugin, [typeCheckerOptions]);
|
|
111
135
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
0 && (module.exports = {
|
|
118
|
-
PLUGIN_TYPE_CHECK_NAME,
|
|
119
|
-
pluginTypeCheck
|
|
136
|
+
});
|
|
137
|
+
var __webpack_export_target__ = exports;
|
|
138
|
+
for(var i in __webpack_exports__)__webpack_export_target__[i] = __webpack_exports__[i];
|
|
139
|
+
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
|
|
140
|
+
value: true
|
|
120
141
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
-
import type ForkTSCheckerPlugin from 'fork-ts-checker-webpack-plugin';
|
|
3
2
|
import { type ConfigChain } from 'reduce-configs';
|
|
4
|
-
|
|
3
|
+
import { TsCheckerRspackPlugin } from 'ts-checker-rspack-plugin';
|
|
4
|
+
type ForkTsCheckerOptions = NonNullable<ConstructorParameters<typeof TsCheckerRspackPlugin>[0]>;
|
|
5
5
|
export type PluginTypeCheckerOptions = {
|
|
6
6
|
/**
|
|
7
7
|
* Whether to enable TypeScript type checking.
|
|
@@ -9,8 +9,8 @@ export type PluginTypeCheckerOptions = {
|
|
|
9
9
|
*/
|
|
10
10
|
enable?: boolean;
|
|
11
11
|
/**
|
|
12
|
-
* To modify the options of `
|
|
13
|
-
* @see https://github.com/
|
|
12
|
+
* To modify the options of `ts-checker-rspack-plugin`.
|
|
13
|
+
* @see https://github.com/rspack-contrib/ts-checker-rspack-plugin#readme
|
|
14
14
|
*/
|
|
15
15
|
forkTsCheckerOptions?: ConfigChain<ForkTsCheckerOptions>;
|
|
16
16
|
};
|
package/dist/index.js
CHANGED
|
@@ -1,99 +1,79 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
1
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_fs__ from "node:fs";
|
|
2
|
+
import * as __WEBPACK_EXTERNAL_MODULE_node_module__ from "node:module";
|
|
3
|
+
import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core__ from "@rsbuild/core";
|
|
4
|
+
import * as __WEBPACK_EXTERNAL_MODULE_deepmerge__ from "deepmerge";
|
|
5
|
+
import * as __WEBPACK_EXTERNAL_MODULE_json5__ from "json5";
|
|
6
|
+
import * as __WEBPACK_EXTERNAL_MODULE_reduce_configs__ from "reduce-configs";
|
|
7
|
+
import * as __WEBPACK_EXTERNAL_MODULE_ts_checker_rspack_plugin__ from "ts-checker-rspack-plugin";
|
|
8
|
+
const src_require = (0, __WEBPACK_EXTERNAL_MODULE_node_module__.createRequire)(import.meta.url);
|
|
9
|
+
const PLUGIN_TYPE_CHECK_NAME = 'rsbuild:type-check';
|
|
10
|
+
const pluginTypeCheck = (options = {})=>({
|
|
11
|
+
name: PLUGIN_TYPE_CHECK_NAME,
|
|
12
|
+
setup (api) {
|
|
13
|
+
const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/;
|
|
14
|
+
const checkedTsconfig = new Map();
|
|
15
|
+
api.modifyBundlerChain(async (chain, { isProd, environment, CHAIN_ID })=>{
|
|
16
|
+
const { enable = true, forkTsCheckerOptions } = options;
|
|
17
|
+
const { tsconfigPath } = environment;
|
|
18
|
+
if (!tsconfigPath || false === enable) return;
|
|
19
|
+
// If there are identical tsconfig.json files,
|
|
20
|
+
// apply type checker only once to avoid duplicate checks.
|
|
21
|
+
if (checkedTsconfig.has(tsconfigPath) && checkedTsconfig.get(tsconfigPath) !== environment.name) return;
|
|
22
|
+
checkedTsconfig.set(tsconfigPath, environment.name);
|
|
23
|
+
// use typescript of user project
|
|
24
|
+
let typescriptPath;
|
|
25
|
+
try {
|
|
26
|
+
typescriptPath = src_require.resolve('typescript', {
|
|
27
|
+
paths: [
|
|
28
|
+
api.context.rootPath
|
|
29
|
+
]
|
|
30
|
+
});
|
|
31
|
+
} catch (err) {
|
|
32
|
+
__WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.warn('"typescript" is not found in current project, Type checker will not work.');
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const { references } = __WEBPACK_EXTERNAL_MODULE_json5__["default"].parse(__WEBPACK_EXTERNAL_MODULE_node_fs__["default"].readFileSync(tsconfigPath, 'utf-8'));
|
|
36
|
+
const useReference = Array.isArray(references) && references.length > 0;
|
|
37
|
+
const defaultOptions = {
|
|
38
|
+
typescript: {
|
|
39
|
+
// set 'readonly' to avoid emitting tsbuildinfo,
|
|
40
|
+
// as the generated tsbuildinfo will break ts-checker-rspack-plugin
|
|
41
|
+
mode: 'readonly',
|
|
42
|
+
// enable build when using project reference
|
|
43
|
+
build: useReference,
|
|
44
|
+
// avoid OOM issue
|
|
45
|
+
memoryLimit: 8192,
|
|
46
|
+
// use tsconfig of user project
|
|
47
|
+
configFile: tsconfigPath,
|
|
48
|
+
// use typescript of user project
|
|
49
|
+
typescriptPath
|
|
50
|
+
},
|
|
51
|
+
issue: {
|
|
52
|
+
// ignore types errors from node_modules
|
|
53
|
+
exclude: [
|
|
54
|
+
({ file = '' })=>NODE_MODULES_REGEX.test(file)
|
|
55
|
+
]
|
|
56
|
+
},
|
|
57
|
+
logger: {
|
|
58
|
+
log () {
|
|
59
|
+
// do nothing
|
|
60
|
+
// we only want to display error messages
|
|
61
|
+
},
|
|
62
|
+
error (message) {
|
|
63
|
+
console.error(message.replace(/ERROR/g, 'Type Error'));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const typeCheckerOptions = (0, __WEBPACK_EXTERNAL_MODULE_reduce_configs__.reduceConfigs)({
|
|
68
|
+
initial: defaultOptions,
|
|
69
|
+
config: forkTsCheckerOptions,
|
|
70
|
+
mergeFn: __WEBPACK_EXTERNAL_MODULE_deepmerge__["default"]
|
|
71
|
+
});
|
|
72
|
+
if (isProd) __WEBPACK_EXTERNAL_MODULE__rsbuild_core__.logger.info('Type checker is enabled. It may take some time.');
|
|
73
|
+
chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(__WEBPACK_EXTERNAL_MODULE_ts_checker_rspack_plugin__.TsCheckerRspackPlugin, [
|
|
74
|
+
typeCheckerOptions
|
|
75
|
+
]);
|
|
43
76
|
});
|
|
44
|
-
} catch (err) {
|
|
45
|
-
logger.warn(
|
|
46
|
-
'"typescript" is not found in current project, Type checker will not work.'
|
|
47
|
-
);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
const { default: ForkTsCheckerWebpackPlugin } = await import("fork-ts-checker-webpack-plugin");
|
|
51
|
-
const { default: json5 } = await import("json5");
|
|
52
|
-
const { references } = json5.parse(
|
|
53
|
-
fs.readFileSync(tsconfigPath, "utf-8")
|
|
54
|
-
);
|
|
55
|
-
const useReference = Array.isArray(references) && references.length > 0;
|
|
56
|
-
const defaultOptions = {
|
|
57
|
-
typescript: {
|
|
58
|
-
// set 'readonly' to avoid emitting tsbuildinfo,
|
|
59
|
-
// as the generated tsbuildinfo will break fork-ts-checker
|
|
60
|
-
mode: "readonly",
|
|
61
|
-
// enable build when using project reference
|
|
62
|
-
build: useReference,
|
|
63
|
-
// avoid OOM issue
|
|
64
|
-
memoryLimit: 8192,
|
|
65
|
-
// use tsconfig of user project
|
|
66
|
-
configFile: tsconfigPath,
|
|
67
|
-
// use typescript of user project
|
|
68
|
-
typescriptPath
|
|
69
|
-
},
|
|
70
|
-
issue: {
|
|
71
|
-
// ignore types errors from node_modules
|
|
72
|
-
exclude: [({ file = "" }) => NODE_MODULES_REGEX.test(file)]
|
|
73
|
-
},
|
|
74
|
-
logger: {
|
|
75
|
-
log() {
|
|
76
|
-
},
|
|
77
|
-
error(message) {
|
|
78
|
-
console.error(message.replace(/ERROR/g, "Type Error"));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
};
|
|
82
|
-
const typeCheckerOptions = reduceConfigs({
|
|
83
|
-
initial: defaultOptions,
|
|
84
|
-
config: forkTsCheckerOptions,
|
|
85
|
-
mergeFn: deepmerge
|
|
86
|
-
});
|
|
87
|
-
if (isProd) {
|
|
88
|
-
logger.info("Type checker is enabled. It may take some time.");
|
|
89
|
-
}
|
|
90
|
-
chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(ForkTsCheckerWebpackPlugin, [typeCheckerOptions]);
|
|
91
77
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
};
|
|
95
|
-
};
|
|
96
|
-
export {
|
|
97
|
-
PLUGIN_TYPE_CHECK_NAME,
|
|
98
|
-
pluginTypeCheck
|
|
99
|
-
};
|
|
78
|
+
});
|
|
79
|
+
export { PLUGIN_TYPE_CHECK_NAME, pluginTypeCheck };
|
package/package.json
CHANGED
|
@@ -1,50 +1,69 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-type-check",
|
|
3
|
-
"version": "1.0.1
|
|
4
|
-
"
|
|
5
|
-
"homepage": "https://rsbuild.dev",
|
|
6
|
-
"repository": {
|
|
7
|
-
"type": "git",
|
|
8
|
-
"url": "https://github.com/web-infra-dev/rsbuild",
|
|
9
|
-
"directory": "packages/plugin-type-check"
|
|
10
|
-
},
|
|
3
|
+
"version": "1.1.0-beta.1",
|
|
4
|
+
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-type-check",
|
|
11
5
|
"license": "MIT",
|
|
12
6
|
"type": "module",
|
|
13
7
|
"exports": {
|
|
14
8
|
".": {
|
|
15
|
-
"types": "./dist
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
16
10
|
"import": "./dist/index.js",
|
|
17
11
|
"require": "./dist/index.cjs"
|
|
18
12
|
}
|
|
19
13
|
},
|
|
20
|
-
"main": "./dist/index.
|
|
21
|
-
"
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"module": "./dist/index.mjs",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
22
17
|
"files": [
|
|
23
|
-
"dist"
|
|
24
|
-
"dist-types"
|
|
18
|
+
"dist"
|
|
25
19
|
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rslib build",
|
|
22
|
+
"dev": "rslib build --watch",
|
|
23
|
+
"lint": "biome check .",
|
|
24
|
+
"lint:write": "biome check . --write",
|
|
25
|
+
"prepare": "simple-git-hooks && npm run build",
|
|
26
|
+
"test": "playwright test",
|
|
27
|
+
"bump": "npx bumpp"
|
|
28
|
+
},
|
|
29
|
+
"simple-git-hooks": {
|
|
30
|
+
"pre-commit": "npx nano-staged"
|
|
31
|
+
},
|
|
32
|
+
"nano-staged": {
|
|
33
|
+
"*.{js,jsx,ts,tsx,mjs,cjs}": [
|
|
34
|
+
"biome check --write --no-errors-on-unmatched"
|
|
35
|
+
]
|
|
36
|
+
},
|
|
26
37
|
"dependencies": {
|
|
27
38
|
"deepmerge": "^4.3.1",
|
|
28
|
-
"
|
|
39
|
+
"ts-checker-rspack-plugin": "^1.0.0",
|
|
29
40
|
"json5": "^2.2.3",
|
|
30
|
-
"reduce-configs": "^1.0.0"
|
|
31
|
-
"webpack": "^5.94.0"
|
|
41
|
+
"reduce-configs": "^1.0.0"
|
|
32
42
|
},
|
|
33
43
|
"devDependencies": {
|
|
34
|
-
"
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
44
|
+
"@biomejs/biome": "^1.9.4",
|
|
45
|
+
"@playwright/test": "^1.48.2",
|
|
46
|
+
"@rsbuild/core": "^1.0.19",
|
|
47
|
+
"@rslib/core": "^0.0.16",
|
|
48
|
+
"@types/fs-extra": "^11.0.4",
|
|
49
|
+
"@types/node": "^22.8.6",
|
|
50
|
+
"fs-extra": "^11.2.0",
|
|
51
|
+
"nano-staged": "^0.8.0",
|
|
52
|
+
"playwright": "^1.48.2",
|
|
53
|
+
"simple-git-hooks": "^2.11.1",
|
|
54
|
+
"typescript": "^5.6.3"
|
|
37
55
|
},
|
|
38
56
|
"peerDependencies": {
|
|
39
|
-
"@rsbuild/core": "
|
|
57
|
+
"@rsbuild/core": "1.x"
|
|
40
58
|
},
|
|
59
|
+
"peerDependenciesMeta": {
|
|
60
|
+
"@rsbuild/core": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
"packageManager": "pnpm@9.12.3",
|
|
41
65
|
"publishConfig": {
|
|
42
66
|
"access": "public",
|
|
43
|
-
"provenance": true,
|
|
44
67
|
"registry": "https://registry.npmjs.org/"
|
|
45
|
-
},
|
|
46
|
-
"scripts": {
|
|
47
|
-
"build": "modern build",
|
|
48
|
-
"dev": "modern build --watch"
|
|
49
68
|
}
|
|
50
|
-
}
|
|
69
|
+
}
|
package/dist-types/package.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"//":"This file is for making TypeScript work with moduleResolution node16+.","version":"1.0.0"}
|