A webpack plugin that runs TypeScript type checking on a separate process to speed up webpack compilation by moving type checking out of the main build process. It supports modern TypeScript features like project references and incremental mode, provides formatted error messages with code frames, and offers comprehensive configuration options for memory limits, TypeScript configuration overrides, issue filtering, and diagnostic selection.
npm install --save-dev fork-ts-checker-webpack-pluginimport ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";For CommonJS:
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");// webpack.config.js
import ForkTsCheckerWebpackPlugin from "fork-ts-checker-webpack-plugin";
export default {
context: __dirname, // to automatically find tsconfig.json
entry: './src/index.ts',
resolve: {
extensions: [".ts", ".tsx", ".js"],
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
transpileOnly: true // Important: disable type checking in ts-loader
}
}
]
},
plugins: [
new ForkTsCheckerWebpackPlugin({
async: false, // Run synchronously for production builds
typescript: {
configFile: './tsconfig.json',
memoryLimit: 4096
}
})
]
};Fork TS Checker Webpack Plugin is built around several key components:
ForkTsCheckerWebpackPlugin class that integrates with webpack's plugin systemCore plugin instantiation and configuration options for integrating with webpack builds.
class ForkTsCheckerWebpackPlugin {
constructor(options?: ForkTsCheckerWebpackPluginOptions);
apply(compiler: webpack.Compiler): void;
static getCompilerHooks(compiler: webpack.Compiler): ForkTsCheckerWebpackPluginHooks;
static readonly version: string;
static readonly issuesPool: object;
static readonly dependenciesPool: object;
}
interface ForkTsCheckerWebpackPluginOptions {
async?: boolean;
typescript?: TypeScriptWorkerOptions;
formatter?: FormatterOptions;
issue?: IssueOptions;
logger?: Logger | 'webpack-infrastructure';
devServer?: boolean;
}Advanced TypeScript compiler configuration including memory limits, config file overrides, build modes, and diagnostic options.
interface TypeScriptWorkerOptions {
memoryLimit?: number;
configFile?: string;
configOverwrite?: TypeScriptConfigOverwrite;
context?: string;
build?: boolean;
mode?: 'readonly' | 'write-tsbuildinfo' | 'write-dts' | 'write-references';
diagnosticOptions?: Partial<TypeScriptDiagnosticsOptions>;
profile?: boolean;
typescriptPath?: string;
}Type checking issue representation, severity levels, location information, and filtering options.
interface Issue {
severity: IssueSeverity;
code: string;
message: string;
file?: string;
location?: IssueLocation;
}
type IssueSeverity = 'error' | 'warning';
interface IssueLocation {
start: IssuePosition;
end: IssuePosition;
}
interface IssueOptions {
include?: IssuePredicateOption;
exclude?: IssuePredicateOption;
}Output formatters for displaying type checking results with customizable path formats and code frame options.
type FormatterOptions =
| undefined
| FormatterType
| BasicFormatterOptions
| CodeframeFormatterOptions
| Formatter;
type Formatter = (issue: Issue) => string;
interface BasicFormatterOptions {
type: 'basic';
pathType?: FormatterPathType;
}
interface CodeframeFormatterOptions {
type: 'codeframe';
pathType?: FormatterPathType;
options?: BabelCodeFrameOptions;
}Extensible hook system for intercepting and customizing type checking lifecycle events.
interface ForkTsCheckerWebpackPluginHooks {
start: AsyncSeriesWaterfallHook<[FilesChange, webpack.Compilation]>;
waiting: SyncHook<[webpack.Compilation]>;
canceled: SyncHook<[webpack.Compilation]>;
error: SyncHook<[unknown, webpack.Compilation]>;
issues: SyncWaterfallHook<[Issue[], webpack.Compilation | undefined], void>;
}
function getPluginHooks(compiler: webpack.Compiler | webpack.MultiCompiler): ForkTsCheckerWebpackPluginHooks;interface Logger {
log: (message: string) => void;
error: (message: string) => void;
}interface FilesChange {
changedFiles?: string[];
deletedFiles?: string[];
}
/**
* Get current files change for a webpack compiler
* @param compiler - Webpack compiler instance
* @returns Current files change state
*/
function getFilesChange(compiler: webpack.Compiler): FilesChange;
/**
* Consume and clear files change for a webpack compiler
* @param compiler - Webpack compiler instance
* @returns Files change state before clearing
*/
function consumeFilesChange(compiler: webpack.Compiler): FilesChange;
/**
* Update files change for a webpack compiler
* @param compiler - Webpack compiler instance
* @param change - Files change to merge
*/
function updateFilesChange(compiler: webpack.Compiler, change: FilesChange): void;
/**
* Clear files change for a webpack compiler
* @param compiler - Webpack compiler instance
*/
function clearFilesChange(compiler: webpack.Compiler): void;
/**
* Aggregate multiple files changes into one
* @param changes - Array of files changes to aggregate
* @returns Aggregated files change
*/
function aggregateFilesChanges(changes: FilesChange[]): FilesChange;interface IssuePosition {
line: number; // 1-based line number
column: number; // 0-based column number
}type IssuePredicateOption = IssuePredicate | IssueMatch | (IssuePredicate | IssueMatch)[];
type IssuePredicate = (issue: Issue) => boolean;
interface IssueMatch {
file?: string;
code?: string;
severity?: IssueSeverity;
}type FormatterPathType = 'relative' | 'absolute';