1 | [![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)
|
2 |
|
3 | <div align="center">
|
4 |
|
5 | <img alt="" width="300" src="./media/logo.svg" />
|
6 | <h1>Fork TS Checker Webpack Plugin</h1>
|
7 | <p>Webpack plugin that runs TypeScript type checker on a separate process.</p>
|
8 |
|
9 | [![npm version](https://img.shields.io/npm/v/fork-ts-checker-webpack-plugin.svg)](https://www.npmjs.com/package/fork-ts-checker-webpack-plugin)
|
10 | [![build status](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/workflows/CI/CD/badge.svg?branch=main&event=push)](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/actions?query=branch%3Amain+event%3Apush)
|
11 | [![downloads](http://img.shields.io/npm/dm/fork-ts-checker-webpack-plugin.svg)](https://npmjs.org/package/fork-ts-checker-webpack-plugin)
|
12 | [![commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
|
13 | [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
|
14 | [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
|
15 |
|
16 | </div>
|
17 |
|
18 | ## Features
|
19 |
|
20 | * Speeds up [TypeScript](https://github.com/Microsoft/TypeScript) type checking (by moving it to a separate process) 🏎
|
21 | * Supports modern TypeScript features like [project references](https://www.typescriptlang.org/docs/handbook/project-references.html) and [incremental mode](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#faster-subsequent-builds-with-the---incremental-flag) ✨
|
22 | * Displays nice error messages with the [code frame](https://babeljs.io/docs/en/next/babel-code-frame.html) formatter 🌈
|
23 |
|
24 | ## Installation
|
25 |
|
26 | This plugin requires **Node.js >=14.0.0+**, **Webpack ^5.11.0**, **TypeScript ^3.6.0**
|
27 |
|
28 | * If you depend on **TypeScript 2.1 - 2.6.2**, please use [version 4](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/tree/v4.1.4) of the plugin.
|
29 | * If you depend on **Webpack 4**, **TypeScript 2.7 - 3.5.3** or **ESLint** feature, please use [version 6](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/tree/v6.2.6) of the plugin.
|
30 | * If you depend on **Node.js 12**, please use [version 8](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/tree/v8.0.0) of the plugin.
|
31 | * If you need Vue.js support, please use [version 6](https://github.com/TypeStrong/fork-ts-checker-webpack-plugin/tree/v6.5.2) of ths plugin
|
32 |
|
33 | ```sh
|
34 | # with npm
|
35 | npm install --save-dev fork-ts-checker-webpack-plugin
|
36 |
|
37 | # with yarn
|
38 | yarn add --dev fork-ts-checker-webpack-plugin
|
39 | ```
|
40 |
|
41 | The minimal webpack config (with [ts-loader](https://github.com/TypeStrong/ts-loader))
|
42 |
|
43 | ```js
|
44 | // webpack.config.js
|
45 | const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
46 |
|
47 | module.exports = {
|
48 | context: __dirname, // to automatically find tsconfig.json
|
49 | entry: './src/index.ts',
|
50 | resolve: {
|
51 | extensions: [".ts", ".tsx", ".js"],
|
52 | },
|
53 | module: {
|
54 | rules: [
|
55 | {
|
56 | test: /\.tsx?$/,
|
57 | loader: 'ts-loader',
|
58 | // add transpileOnly option if you use ts-loader < 9.3.0
|
59 | // options: {
|
60 | // transpileOnly: true
|
61 | // }
|
62 | }
|
63 | ]
|
64 | },
|
65 | plugins: [new ForkTsCheckerWebpackPlugin()],
|
66 | watchOptions: {
|
67 | // for some systems, watching many files can result in a lot of CPU or memory usage
|
68 | // https://webpack.js.org/configuration/watch/#watchoptionsignored
|
69 | // don't use this pattern, if you have a monorepo with linked packages
|
70 | ignored: /node_modules/,
|
71 | },
|
72 | };
|
73 | ```
|
74 |
|
75 | > Examples how to configure it with [babel-loader](https://github.com/babel/babel-loader), [ts-loader](https://github.com/TypeStrong/ts-loader)
|
76 | > and [Visual Studio Code](https://code.visualstudio.com/) are in the [**examples**](./examples) directory.
|
77 |
|
78 | ## Modules resolution
|
79 |
|
80 | It's very important to be aware that **this plugin uses [TypeScript](https://github.com/Microsoft/TypeScript)'s, not
|
81 | [webpack](https://github.com/webpack/webpack)'s modules resolution**. It means that you have to setup `tsconfig.json` correctly.
|
82 |
|
83 | > It's because of the performance - with TypeScript's module resolution we don't have to wait for webpack to compile files.
|
84 | >
|
85 | > To debug TypeScript's modules resolution, you can use `tsc --traceResolution` command.
|
86 |
|
87 | ## Options
|
88 |
|
89 | This plugin uses [`cosmiconfig`](https://github.com/davidtheclark/cosmiconfig). This means that besides the plugin constructor,
|
90 | you can place your configuration in the:
|
91 | * `"fork-ts-checker"` field in the `package.json`
|
92 | * `.fork-ts-checkerrc` file in JSON or YAML format
|
93 | * `fork-ts-checker.config.js` file exporting a JS object
|
94 |
|
95 | Options passed to the plugin constructor will overwrite options from the cosmiconfig (using [deepmerge](https://github.com/TehShrike/deepmerge)).
|
96 |
|
97 | | Name | Type | Default value | Description |
|
98 | |--------------|--------------------------------------|-------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
99 | | `async` | `boolean` | `compiler.options.mode === 'development'` | If `true`, reports issues **after** webpack's compilation is done. Thanks to that it doesn't block the compilation. Used only in the `watch` mode. |
|
100 | | `typescript` | `object` | `{}` | See [TypeScript options](#typescript-options). |
|
101 | | `issue` | `object` | `{}` | See [Issues options](#issues-options). |
|
102 | | `formatter` | `string` or `object` or `function` | `codeframe` | Available formatters are `basic`, `codeframe` and a custom `function`. To [configure](https://babeljs.io/docs/en/babel-code-frame#options) `codeframe` formatter, pass: `{ type: 'codeframe', options: { <coderame options> } }`. To use absolute file path, pass: `{ type: 'codeframe', pathType: 'absolute' }`. |
|
103 | | `logger` | `{ log: function, error: function }` or `webpack-infrastructure` | `console` | Console-like object to print issues in `async` mode. |
|
104 | | `devServer` | `boolean` | `true` | If set to `false`, errors will not be reported to Webpack Dev Server. |
|
105 |
|
106 | ### TypeScript options
|
107 |
|
108 | Options for the TypeScript checker (`typescript` option object).
|
109 |
|
110 | | Name | Type | Default value | Description |
|
111 | |---------------------|--------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
112 | | `memoryLimit` | `number` | `2048` | Memory limit for the checker process in MB. If the process exits with the allocation failed error, try to increase this number. |
|
113 | | `configFile` | `string` | `'tsconfig.json'` | Path to the `tsconfig.json` file (path relative to the `compiler.options.context` or absolute path) |
|
114 | | `configOverwrite` | `object` | `{ compilerOptions: { skipLibCheck: true, sourceMap: false, inlineSourceMap: false, declarationMap: false } }` | This configuration will overwrite configuration from the `tsconfig.json` file. Supported fields are: `extends`, `compilerOptions`, `include`, `exclude`, `files`, and `references`. |
|
115 | | `context` | `string` | `dirname(configuration.configFile)` | The base path for finding files specified in the `tsconfig.json`. Same as the `context` option from the [ts-loader](https://github.com/TypeStrong/ts-loader#context). Useful if you want to keep your `tsconfig.json` in an external package. Keep in mind that **not** having a `tsconfig.json` in your project root can cause different behaviour between `fork-ts-checker-webpack-plugin` and `tsc`. When using editors like `VS Code` it is advised to add a `tsconfig.json` file to the root of the project and extend the config file referenced in option `configFile`. |
|
116 | | `build` | `boolean` | `false` | The equivalent of the `--build` flag for the `tsc` command. |
|
117 | | `mode` | `'readonly'` or `'write-dts'` or `'write-tsbuildinfo'` or `'write-references'` | `build === true ? 'write-tsbuildinfo' ? 'readonly'` | Use `readonly` if you don't want to write anything on the disk, `write-dts` to write only `.d.ts` files, `write-tsbuildinfo` to write only `.tsbuildinfo` files, `write-references` to write both `.js` and `.d.ts` files of project references (last 2 modes requires `build: true`). |
|
118 | | `diagnosticOptions` | `object` | `{ syntactic: false, semantic: true, declaration: false, global: false }` | Settings to select which diagnostics do we want to perform. |
|
119 | | `profile` | `boolean` | `false` | Measures and prints timings related to the TypeScript performance. |
|
120 | | `typescriptPath` | `string` | `require.resolve('typescript')` | If supplied this is a custom path where TypeScript can be found. |
|
121 |
|
122 | ### Issues options
|
123 |
|
124 | Options for the issues filtering (`issue` option object).
|
125 | I could write some plain text explanation of these options but I think code will explain it better:
|
126 |
|
127 | ```typescript
|
128 | interface Issue {
|
129 | severity: 'error' | 'warning';
|
130 | code: string;
|
131 | file?: string;
|
132 | }
|
133 |
|
134 | type IssueMatch = Partial<Issue>; // file field supports glob matching
|
135 | type IssuePredicate = (issue: Issue) => boolean;
|
136 | type IssueFilter = IssueMatch | IssuePredicate | (IssueMatch | IssuePredicate)[];
|
137 | ```
|
138 |
|
139 | | Name | Type | Default value | Description |
|
140 | |-----------|---------------|---------------|------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
141 | | `include` | `IssueFilter` | `undefined` | If `object`, defines issue properties that should be [matched](src/issue/issue-match.ts). If `function`, acts as a predicate where `issue` is an argument. |
|
142 | | `exclude` | `IssueFilter` | `undefined` | Same as `include` but issues that match this predicate will be excluded. |
|
143 |
|
144 | <details>
|
145 | <summary>Expand example</summary>
|
146 |
|
147 | Include issues from the `src` directory, exclude issues from `.spec.ts` files:
|
148 |
|
149 | ```js
|
150 | module.exports = {
|
151 | // ...the webpack configuration
|
152 | plugins: [
|
153 | new ForkTsCheckerWebpackPlugin({
|
154 | issue: {
|
155 | include: [
|
156 | { file: '**/src/**/*' }
|
157 | ],
|
158 | exclude: [
|
159 | { file: '**/*.spec.ts' }
|
160 | ]
|
161 | }
|
162 | })
|
163 | ]
|
164 | };
|
165 | ```
|
166 |
|
167 | </details>
|
168 |
|
169 | ## Plugin hooks
|
170 |
|
171 | This plugin provides some custom webpack hooks:
|
172 |
|
173 | | Hook key | Type | Params | Description |
|
174 | |------------|----------------------------|-----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
175 | | `start` | `AsyncSeriesWaterfallHook` | `change, compilation` | Starts issues checking for a compilation. It's an async waterfall hook, so you can modify the list of changed and removed files or delay the start of the service. |
|
176 | | `waiting` | `SyncHook` | `compilation` | Waiting for the issues checking. |
|
177 | | `canceled` | `SyncHook` | `compilation` | Issues checking for the compilation has been canceled. |
|
178 | | `error` | `SyncHook` | `compilation` | An error occurred during issues checking. |
|
179 | | `issues` | `SyncWaterfallHook` | `issues, compilation` | Issues have been received and will be reported. It's a waterfall hook, so you can modify the list of received issues. |
|
180 |
|
181 | To access plugin hooks and tap into the event, we need to use the `getCompilerHooks` static method.
|
182 | When we call this method with a [webpack compiler instance](https://webpack.js.org/api/node/), it returns the object with
|
183 | [tapable](https://github.com/webpack/tapable) hooks where you can pass in your callbacks.
|
184 |
|
185 | ```js
|
186 | // ./src/webpack/MyWebpackPlugin.js
|
187 | const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
188 |
|
189 | class MyWebpackPlugin {
|
190 | apply(compiler) {
|
191 | const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);
|
192 |
|
193 | // log some message on waiting
|
194 | hooks.waiting.tap('MyPlugin', () => {
|
195 | console.log('waiting for issues');
|
196 | });
|
197 | // don't show warnings
|
198 | hooks.issues.tap('MyPlugin', (issues) =>
|
199 | issues.filter((issue) => issue.severity === 'error')
|
200 | );
|
201 | }
|
202 | }
|
203 |
|
204 | module.exports = MyWebpackPlugin;
|
205 |
|
206 | // webpack.config.js
|
207 | const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
|
208 | const MyWebpackPlugin = require('./src/webpack/MyWebpackPlugin');
|
209 |
|
210 | module.exports = {
|
211 | /* ... */
|
212 | plugins: [
|
213 | new ForkTsCheckerWebpackPlugin(),
|
214 | new MyWebpackPlugin()
|
215 | ]
|
216 | };
|
217 | ```
|
218 |
|
219 | ## Profiling types resolution
|
220 |
|
221 | When using TypeScript 4.3.0 or newer you can profile long type checks by
|
222 | setting "generateTrace" compiler option. This is an instruction from [microsoft/TypeScript#40063](https://github.com/microsoft/TypeScript/pull/40063):
|
223 |
|
224 | 1. Set "generateTrace": "{folderName}" in your `tsconfig.json` (under `compilerOptions`)
|
225 | 2. Look in the resulting folder. If you used build mode, there will be a `legend.json` telling you what went where.
|
226 | Otherwise, there will be `trace.json` file and `types.json` files.
|
227 | 3. Navigate to [edge://tracing](edge://tracing) or [chrome://tracing](chrome://tracing) and load `trace.json`
|
228 | 4. Expand Process 1 with the little triangle in the left sidebar
|
229 | 5. Click on different blocks to see their payloads in the bottom pane
|
230 | 6. Open `types.json` in an editor
|
231 | 7. When you see a type ID in the tracing output, go-to-line {id} to find data about that type
|
232 |
|
233 | ## Enabling incremental mode
|
234 |
|
235 | You must both set "incremental": true in your `tsconfig.json` (under `compilerOptions`) and also specify mode: 'write-references' in `ForkTsCheckerWebpackPlugin` settings.
|
236 |
|
237 |
|
238 | ## Related projects
|
239 |
|
240 | * [`ts-loader`](https://github.com/TypeStrong/ts-loader) - TypeScript loader for webpack.
|
241 | * [`babel-loader`](https://github.com/babel/babel-loader) - Alternative TypeScript loader for webpack.
|
242 | * [`fork-ts-checker-notifier-webpack-plugin`](https://github.com/johnnyreilly/fork-ts-checker-notifier-webpack-plugin) - Notifies about build status using system notifications (similar to the [webpack-notifier](https://github.com/Turbo87/webpack-notifier)).
|
243 |
|
244 | ## Credits
|
245 |
|
246 | This plugin was created in [Realytics](https://www.realytics.io/) in 2017. Thank you for supporting Open Source.
|
247 |
|
248 | ## License
|
249 |
|
250 | MIT License
|
251 |
|