Rspack TypeScript Checker Plugin
The ts-checker-rspack-plugin is an essential Rspack plugin designed to accelerate TypeScript type checking by offloading it to a separate process. This approach significantly improves build performance, especially in larger projects. Currently stable at version 1.3.0, the package maintains an active release cadence, with frequent minor and patch updates addressing bug fixes, performance enhancements, and dependency updates. Key differentiators include its robust support for modern TypeScript features like project references and incremental mode, a visually appealing code frame formatter for error messages, and its origin as a fork of the widely used `fork-ts-checker-webpack-plugin`, specifically adapted for the Rspack ecosystem. It requires Node.js >=16.0.0+, Rspack ^1.0.0 || ^2.0.0-0, and TypeScript >=3.8.0 as peer dependencies.
Common errors
-
ERROR in <file>: Cannot find name 'MyComponentProps'.
cause TypeScript is unable to resolve types or modules, often due to a misconfigured `tsconfig.json` that doesn't align with the project structure or uses incorrect path mappings.fixVerify your `tsconfig.json` settings, specifically `baseUrl`, `paths`, `include`, and `exclude`. The plugin relies on TypeScript's own resolution, not Rspack's. Run `tsc --traceResolution` to debug TypeScript's module resolution. -
TypeError: TsCheckerRspackPlugin is not a constructor
cause Attempting to use `new TsCheckerRspackPlugin()` without correctly importing the named export. This often happens when mixing CommonJS `require` with `import` syntax or incorrect destructuring.fixFor ES Modules, ensure `import { TsCheckerRspackPlugin } from 'ts-checker-rspack-plugin';` is used. For CommonJS, use `const { TsCheckerRspackPlugin } = require('ts-checker-rspack-plugin');`. -
Rspack build finishes without type errors, but 'tsc --noEmit' reports errors.
cause The plugin's configuration (or the `tsconfig.json` it uses) might not be checking all necessary files or directories, leading to a discrepancy between the plugin's output and a direct `tsc` run.fixEnsure the `tsconfig.json` file specified for the plugin via `typescript.configFile` (if any) and its `include`/`exclude` options cover all your source files. Double-check that `tsconfig.json` is consistent between your Rspack configuration and standalone `tsc` commands.
Warnings
- breaking The plugin updated its peer dependency to officially support `@rspack/core` v2. Users upgrading Rspack to v2 should update ts-checker-rspack-plugin to v1.2.4 or newer to ensure compatibility and correct behavior.
- gotcha This plugin utilizes TypeScript's internal module resolution logic, not Rspack's. Incorrect `tsconfig.json` configurations for paths or includes can lead to missing type errors or slow performance, even if Rspack's module resolution is correctly set up.
- deprecated As of v1.2.4, the README no longer recommends TypeScript's 'Build mode' (`--build` or `incremental`) for incremental builds directly with this plugin, suggesting that other approaches might be more performant or reliable.
- breaking Versions prior to `1.1.7` could lead to an infinite rebuild loop due to incorrect output path exclusion, severely impacting build performance and stability.
- gotcha The plugin requires Node.js version 16.0.0 or higher. Using an older Node.js version will result in errors or unexpected behavior during installation or execution.
Install
-
npm install ts-checker-rspack-plugin -
yarn add ts-checker-rspack-plugin -
pnpm add ts-checker-rspack-plugin
Imports
- TsCheckerRspackPlugin
import TsCheckerRspackPlugin from 'ts-checker-rspack-plugin';
import { TsCheckerRspackPlugin } from 'ts-checker-rspack-plugin'; - TsCheckerRspackPlugin (CommonJS)
const TsCheckerRspackPlugin = require('ts-checker-rspack-plugin');const { TsCheckerRspackPlugin } = require('ts-checker-rspack-plugin'); - Plugin options type (example)
import type { Options } from 'ts-checker-rspack-plugin';
Quickstart
import { TsCheckerRspackPlugin } from 'ts-checker-rspack-plugin';
export default {
entry: './src/index.ts',
resolve: {
extensions: ['.ts', '.tsx', '.js'],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'builtin:swc-loader',
options: {
jsc: {
parser: {
syntax: 'typescript',
tsx: true // Enable TSX parsing if you're using React/Preact with TypeScript
},
transform: {
react: {
runtime: 'automatic'
}
}
},
// Optional: specify target environment for SWC to match your project's browserlist/targets
env: {
targets: ['chrome 90', 'safari 15', 'firefox 90']
}
},
},
],
},
plugins: [
new TsCheckerRspackPlugin({
// Optional: specify your tsconfig.json file path if it's not at the root
typescript: {
configFile: './tsconfig.json'
},
// Optional: Enable async mode for non-blocking type checking (default is false)
async: false
})
],
// Performance optimization: prevent the plugin from infinitely rebuilding
output: {
path: './dist'
}
};