@rsbuild/plugin-type-check 1.0.1-rc.4 → 1.0.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
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
- <p align="center">
2
- <a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
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
- # Rsbuild
13
+ ## Introduction
14
+
15
+ This plugin internally integrates with [fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin).
16
+
17
+ The type checking logic of `fork-ts-checker-webpack-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 `fork-ts-checker-webpack-plugin`, please refer to [fork-ts-checker-webpack-plugin - README](https://github.com/TypeStrong/fork-ts-checker-webpack-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 fork-ts-checker
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 `fork-ts-checker-webpack-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
+ `fork-ts-checker-webpack-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
- The Rspack-based build tool. It's fast, out-of-the-box and extensible.
217
+ <PackageManagerTabs command="add vue-tsc -D" />
8
218
 
9
- ## Documentation
219
+ 2. Add the `vue-tsc --noEmit` command to the `build` script in package.json:
10
220
 
11
- https://rsbuild.dev/
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
- ## Contributing
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
- Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
232
+ ```js
233
+ pluginTypeCheck({
234
+ enable: process.env.NODE_ENV === "development",
235
+ });
236
+ ```
16
237
 
17
238
  ## License
18
239
 
19
- Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
240
+ [MIT](./LICENSE).
package/dist/index.cjs CHANGED
@@ -34,10 +34,20 @@ __export(src_exports, {
34
34
  pluginTypeCheck: () => pluginTypeCheck
35
35
  });
36
36
  module.exports = __toCommonJS(src_exports);
37
- var import_node_fs = __toESM(require("fs"));
37
+
38
+ // node_modules/.pnpm/tsup@8.2.4_postcss@8.4.38_typescript@5.5.4/node_modules/tsup/assets/cjs_shims.js
39
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
40
+ var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
41
+
42
+ // src/index.ts
43
+ var import_node_fs = __toESM(require("fs"), 1);
44
+ var import_node_module = require("module");
38
45
  var import_core = require("@rsbuild/core");
39
- var import_deepmerge = __toESM(require("deepmerge"));
46
+ var import_deepmerge = __toESM(require("deepmerge"), 1);
47
+ var import_fork_ts_checker_webpack_plugin = __toESM(require("fork-ts-checker-webpack-plugin"), 1);
48
+ var import_json5 = __toESM(require("json5"), 1);
40
49
  var import_reduce_configs = require("reduce-configs");
50
+ var require2 = (0, import_node_module.createRequire)(importMetaUrl);
41
51
  var PLUGIN_TYPE_CHECK_NAME = "rsbuild:type-check";
42
52
  var pluginTypeCheck = (options = {}) => {
43
53
  return {
@@ -58,7 +68,7 @@ var pluginTypeCheck = (options = {}) => {
58
68
  checkedTsconfig.set(tsconfigPath, environment.name);
59
69
  let typescriptPath;
60
70
  try {
61
- typescriptPath = require.resolve("typescript", {
71
+ typescriptPath = require2.resolve("typescript", {
62
72
  paths: [api.context.rootPath]
63
73
  });
64
74
  } catch (err) {
@@ -67,9 +77,7 @@ var pluginTypeCheck = (options = {}) => {
67
77
  );
68
78
  return;
69
79
  }
70
- const { default: ForkTsCheckerWebpackPlugin } = await import("fork-ts-checker-webpack-plugin");
71
- const { default: json5 } = await import("json5");
72
- const { references } = json5.parse(
80
+ const { references } = import_json5.default.parse(
73
81
  import_node_fs.default.readFileSync(tsconfigPath, "utf-8")
74
82
  );
75
83
  const useReference = Array.isArray(references) && references.length > 0;
@@ -107,7 +115,7 @@ var pluginTypeCheck = (options = {}) => {
107
115
  if (isProd) {
108
116
  import_core.logger.info("Type checker is enabled. It may take some time.");
109
117
  }
110
- chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(ForkTsCheckerWebpackPlugin, [typeCheckerOptions]);
118
+ chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(import_fork_ts_checker_webpack_plugin.default, [typeCheckerOptions]);
111
119
  }
112
120
  );
113
121
  }
@@ -0,0 +1,21 @@
1
+ import { RsbuildPlugin } from '@rsbuild/core';
2
+ import ForkTSCheckerPlugin from 'fork-ts-checker-webpack-plugin';
3
+ import { ConfigChain } from 'reduce-configs';
4
+
5
+ type ForkTsCheckerOptions = NonNullable<ConstructorParameters<typeof ForkTSCheckerPlugin>[0]>;
6
+ type PluginTypeCheckerOptions = {
7
+ /**
8
+ * Whether to enable TypeScript type checking.
9
+ * @default true
10
+ */
11
+ enable?: boolean;
12
+ /**
13
+ * To modify the options of `fork-ts-checker-webpack-plugin`.
14
+ * @see https://github.com/TypeStrong/fork-ts-checker-webpack-plugin#readme
15
+ */
16
+ forkTsCheckerOptions?: ConfigChain<ForkTsCheckerOptions>;
17
+ };
18
+ declare const PLUGIN_TYPE_CHECK_NAME = "rsbuild:type-check";
19
+ declare const pluginTypeCheck: (options?: PluginTypeCheckerOptions) => RsbuildPlugin;
20
+
21
+ export { PLUGIN_TYPE_CHECK_NAME, type PluginTypeCheckerOptions, pluginTypeCheck };
@@ -1,8 +1,9 @@
1
- import { type RsbuildPlugin } from '@rsbuild/core';
2
- import type ForkTSCheckerPlugin from 'fork-ts-checker-webpack-plugin';
3
- import { type ConfigChain } from 'reduce-configs';
1
+ import { RsbuildPlugin } from '@rsbuild/core';
2
+ import ForkTSCheckerPlugin from 'fork-ts-checker-webpack-plugin';
3
+ import { ConfigChain } from 'reduce-configs';
4
+
4
5
  type ForkTsCheckerOptions = NonNullable<ConstructorParameters<typeof ForkTSCheckerPlugin>[0]>;
5
- export type PluginTypeCheckerOptions = {
6
+ type PluginTypeCheckerOptions = {
6
7
  /**
7
8
  * Whether to enable TypeScript type checking.
8
9
  * @default true
@@ -14,6 +15,7 @@ export type PluginTypeCheckerOptions = {
14
15
  */
15
16
  forkTsCheckerOptions?: ConfigChain<ForkTsCheckerOptions>;
16
17
  };
17
- export declare const PLUGIN_TYPE_CHECK_NAME = "rsbuild:type-check";
18
- export declare const pluginTypeCheck: (options?: PluginTypeCheckerOptions) => RsbuildPlugin;
19
- export {};
18
+ declare const PLUGIN_TYPE_CHECK_NAME = "rsbuild:type-check";
19
+ declare const pluginTypeCheck: (options?: PluginTypeCheckerOptions) => RsbuildPlugin;
20
+
21
+ export { PLUGIN_TYPE_CHECK_NAME, type PluginTypeCheckerOptions, pluginTypeCheck };
package/dist/index.js CHANGED
@@ -1,23 +1,12 @@
1
- import { createRequire } from 'module';
2
- var require = createRequire(import.meta['url']);
3
-
4
- var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
5
- get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
6
- }) : x)(function(x) {
7
- if (typeof require !== "undefined")
8
- return require.apply(this, arguments);
9
- throw Error('Dynamic require of "' + x + '" is not supported');
10
- });
11
-
12
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.58.3_typescript@5.5.2/node_modules/@modern-js/module-tools/shims/esm.js
13
- import path from "path";
14
- import { fileURLToPath } from "url";
15
-
16
1
  // src/index.ts
17
- import fs from "fs";
2
+ import fs from "node:fs";
3
+ import { createRequire } from "node:module";
18
4
  import { logger } from "@rsbuild/core";
19
5
  import deepmerge from "deepmerge";
6
+ import ForkTSCheckerPlugin from "fork-ts-checker-webpack-plugin";
7
+ import json5 from "json5";
20
8
  import { reduceConfigs } from "reduce-configs";
9
+ var require2 = createRequire(import.meta.url);
21
10
  var PLUGIN_TYPE_CHECK_NAME = "rsbuild:type-check";
22
11
  var pluginTypeCheck = (options = {}) => {
23
12
  return {
@@ -38,7 +27,7 @@ var pluginTypeCheck = (options = {}) => {
38
27
  checkedTsconfig.set(tsconfigPath, environment.name);
39
28
  let typescriptPath;
40
29
  try {
41
- typescriptPath = __require.resolve("typescript", {
30
+ typescriptPath = require2.resolve("typescript", {
42
31
  paths: [api.context.rootPath]
43
32
  });
44
33
  } catch (err) {
@@ -47,8 +36,6 @@ var pluginTypeCheck = (options = {}) => {
47
36
  );
48
37
  return;
49
38
  }
50
- const { default: ForkTsCheckerWebpackPlugin } = await import("fork-ts-checker-webpack-plugin");
51
- const { default: json5 } = await import("json5");
52
39
  const { references } = json5.parse(
53
40
  fs.readFileSync(tsconfigPath, "utf-8")
54
41
  );
@@ -87,7 +74,7 @@ var pluginTypeCheck = (options = {}) => {
87
74
  if (isProd) {
88
75
  logger.info("Type checker is enabled. It may take some time.");
89
76
  }
90
- chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(ForkTsCheckerWebpackPlugin, [typeCheckerOptions]);
77
+ chain.plugin(CHAIN_ID.PLUGIN.TS_CHECKER).use(ForkTSCheckerPlugin, [typeCheckerOptions]);
91
78
  }
92
79
  );
93
80
  }
package/package.json CHANGED
@@ -1,28 +1,30 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-type-check",
3
- "version": "1.0.1-rc.4",
4
- "description": "TS checker plugin of Rsbuild",
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.0.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-types/index.d.ts",
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.cjs",
21
- "types": "./dist-types/index.d.ts",
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
+ "simple-git-hooks": {
21
+ "pre-commit": "npx nano-staged"
22
+ },
23
+ "nano-staged": {
24
+ "*.{js,jsx,ts,tsx,mjs,cjs}": [
25
+ "biome check --write --no-errors-on-unmatched"
26
+ ]
27
+ },
26
28
  "dependencies": {
27
29
  "deepmerge": "^4.3.1",
28
30
  "fork-ts-checker-webpack-plugin": "9.0.2",
@@ -31,20 +33,35 @@
31
33
  "webpack": "^5.94.0"
32
34
  },
33
35
  "devDependencies": {
34
- "typescript": "^5.5.2",
35
- "@rsbuild/core": "1.0.1-rc.4",
36
- "@scripts/test-helper": "1.0.1-rc.4"
36
+ "@biomejs/biome": "^1.8.3",
37
+ "@playwright/test": "^1.46.1",
38
+ "@rsbuild/core": "^1.0.1-rc.5",
39
+ "@types/fs-extra": "^11.0.4",
40
+ "@types/node": "^20.16.2",
41
+ "fs-extra": "^11.2.0",
42
+ "nano-staged": "^0.8.0",
43
+ "playwright": "^1.46.1",
44
+ "simple-git-hooks": "^2.11.1",
45
+ "tsup": "^8.2.4",
46
+ "typescript": "^5.5.4"
37
47
  },
38
48
  "peerDependencies": {
39
- "@rsbuild/core": "^1.0.1-rc.4"
49
+ "@rsbuild/core": "1.x || ^1.0.1-beta.0"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "@rsbuild/core": {
53
+ "optional": true
54
+ }
40
55
  },
41
56
  "publishConfig": {
42
57
  "access": "public",
43
- "provenance": true,
44
58
  "registry": "https://registry.npmjs.org/"
45
59
  },
46
60
  "scripts": {
47
- "build": "modern build",
48
- "dev": "modern build --watch"
61
+ "build": "tsup",
62
+ "dev": "tsup --watch",
63
+ "lint": "biome check .",
64
+ "lint:write": "biome check . --write",
65
+ "test": "playwright test"
49
66
  }
50
67
  }
@@ -1 +0,0 @@
1
- {"//":"This file is for making TypeScript work with moduleResolution node16+.","version":"1.0.0"}