9.0.2 • Published 5 months ago

fork-ts-checker-webpack-plugin v9.0.2

Weekly downloads
7,746,456
License
MIT
Repository
github
Last release
5 months ago

SWUbanner

npm version build status downloads commitizen friendly code style: prettier semantic-release

Features

Installation

This plugin requires Node.js >=14.0.0+, Webpack ^5.11.0, TypeScript ^3.6.0

  • If you depend on TypeScript 2.1 - 2.6.2, please use version 4 of the plugin.
  • If you depend on Webpack 4, TypeScript 2.7 - 3.5.3 or ESLint feature, please use version 6 of the plugin.
  • If you depend on Node.js 12, please use version 8 of the plugin.
  • If you need Vue.js support, please use version 6 of ths plugin
# with npm
npm install --save-dev fork-ts-checker-webpack-plugin

# with yarn
yarn add --dev fork-ts-checker-webpack-plugin

The minimal webpack config (with ts-loader)

// webpack.config.js
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

module.exports = {
  context: __dirname, // to automatically find tsconfig.json
  entry: './src/index.ts',
  resolve: {
    extensions: [".ts", ".tsx", ".js"],
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        // add transpileOnly option if you use ts-loader < 9.3.0 
        // options: {
        //   transpileOnly: true
        // }
      }
    ]
  },
  plugins: [new ForkTsCheckerWebpackPlugin()],
  watchOptions: {
    // for some systems, watching many files can result in a lot of CPU or memory usage
    // https://webpack.js.org/configuration/watch/#watchoptionsignored
    // don't use this pattern, if you have a monorepo with linked packages
    ignored: /node_modules/,
  },
};

Examples how to configure it with babel-loader, ts-loader and Visual Studio Code are in the examples directory.

Modules resolution

It's very important to be aware that this plugin uses TypeScript's, not webpack's modules resolution. It means that you have to setup tsconfig.json correctly.

It's because of the performance - with TypeScript's module resolution we don't have to wait for webpack to compile files.

To debug TypeScript's modules resolution, you can use tsc --traceResolution command.

Options

This plugin uses cosmiconfig. This means that besides the plugin constructor, you can place your configuration in the:

  • "fork-ts-checker" field in the package.json
  • .fork-ts-checkerrc file in JSON or YAML format
  • fork-ts-checker.config.js file exporting a JS object

Options passed to the plugin constructor will overwrite options from the cosmiconfig (using deepmerge).

NameTypeDefault valueDescription
asyncbooleancompiler.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.
typescriptobject{}See TypeScript options.
issueobject{}See Issues options.
formatterstring or object or functioncodeframeAvailable formatters are basic, codeframe and a custom function. To configure codeframe formatter, pass: { type: 'codeframe', options: { <coderame options> } }. To use absolute file path, pass: { type: 'codeframe', pathType: 'absolute' }.
logger{ log: function, error: function } or webpack-infrastructureconsoleConsole-like object to print issues in async mode.
devServerbooleantrueIf set to false, errors will not be reported to Webpack Dev Server.

TypeScript options

Options for the TypeScript checker (typescript option object).

NameTypeDefault valueDescription
memoryLimitnumber2048Memory limit for the checker process in MB. If the process exits with the allocation failed error, try to increase this number.
configFilestring'tsconfig.json'Path to the tsconfig.json file (path relative to the compiler.options.context or absolute path)
configOverwriteobject{ 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.
contextstringdirname(configuration.configFile)The base path for finding files specified in the tsconfig.json. Same as the context option from the ts-loader. 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.
buildbooleanfalseThe equivalent of the --build flag for the tsc command.
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).
diagnosticOptionsobject{ syntactic: false, semantic: true, declaration: false, global: false }Settings to select which diagnostics do we want to perform.
profilebooleanfalseMeasures and prints timings related to the TypeScript performance.
typescriptPathstringrequire.resolve('typescript')If supplied this is a custom path where TypeScript can be found.

Issues options

Options for the issues filtering (issue option object). I could write some plain text explanation of these options but I think code will explain it better:

interface Issue {
  severity: 'error' | 'warning';
  code: string;
  file?: string;
}

type IssueMatch = Partial<Issue>; // file field supports glob matching
type IssuePredicate = (issue: Issue) => boolean;
type IssueFilter = IssueMatch | IssuePredicate | (IssueMatch | IssuePredicate)[];
NameTypeDefault valueDescription
includeIssueFilterundefinedIf object, defines issue properties that should be matched. If function, acts as a predicate where issue is an argument.
excludeIssueFilterundefinedSame as include but issues that match this predicate will be excluded.

Include issues from the src directory, exclude issues from .spec.ts files:

module.exports = {
  // ...the webpack configuration
  plugins: [
    new ForkTsCheckerWebpackPlugin({
      issue: {
        include: [
          { file: '**/src/**/*' }
        ],
        exclude: [
          { file: '**/*.spec.ts' }
        ]
      }
    })
  ]
};

Plugin hooks

This plugin provides some custom webpack hooks:

Hook keyTypeParamsDescription
startAsyncSeriesWaterfallHookchange, compilationStarts 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.
waitingSyncHookcompilationWaiting for the issues checking.
canceledSyncHookcompilationIssues checking for the compilation has been canceled.
errorSyncHookcompilationAn error occurred during issues checking.
issuesSyncWaterfallHookissues, compilationIssues have been received and will be reported. It's a waterfall hook, so you can modify the list of received issues.

To access plugin hooks and tap into the event, we need to use the getCompilerHooks static method. When we call this method with a webpack compiler instance, it returns the object with tapable hooks where you can pass in your callbacks.

// ./src/webpack/MyWebpackPlugin.js
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

class MyWebpackPlugin {
  apply(compiler) {
    const hooks = ForkTsCheckerWebpackPlugin.getCompilerHooks(compiler);

    // log some message on waiting
    hooks.waiting.tap('MyPlugin', () => {
      console.log('waiting for issues');
    });
    // don't show warnings
    hooks.issues.tap('MyPlugin', (issues) =>
      issues.filter((issue) => issue.severity === 'error')
    );
  }
}

module.exports = MyWebpackPlugin;

// webpack.config.js
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const MyWebpackPlugin = require('./src/webpack/MyWebpackPlugin');

module.exports = {
  /* ... */
  plugins: [
    new ForkTsCheckerWebpackPlugin(),
    new MyWebpackPlugin()
  ]
};

Profiling types resolution

When using TypeScript 4.3.0 or newer you can profile long type checks by setting "generateTrace" compiler option. This is an instruction from microsoft/TypeScript#40063:

  1. Set "generateTrace": "{folderName}" in your tsconfig.json (under compilerOptions)
  2. Look in the resulting folder. If you used build mode, there will be a legend.json telling you what went where. Otherwise, there will be trace.json file and types.json files.
  3. Navigate to edge://tracing or chrome://tracing and load trace.json
  4. Expand Process 1 with the little triangle in the left sidebar
  5. Click on different blocks to see their payloads in the bottom pane
  6. Open types.json in an editor
  7. When you see a type ID in the tracing output, go-to-line {id} to find data about that type

Enabling incremental mode

You must both set "incremental": true in your tsconfig.json (under compilerOptions) and also specify mode: 'write-references' in ForkTsCheckerWebpackPlugin settings.

Related projects

Credits

This plugin was created in Realytics in 2017. Thank you for supporting Open Source.

License

MIT License

react-dev-utilsyoyuo-scriptszhouhaifei-react-scripts@bsolution/bsolution-dev-utils@kuoruan/webpack-utils@lofty87/cli@modern-js/builder-shared@proteria/react-scripts@mrjmpl3/quasar-app-extension-typescript@obstinate/react-script@obstinate/vue-script@kraken.js/webpack@uukit/react-dev-kit@uukit/scripts@backstage/cli@toptal/davinci-engine@saasxx/eslintkanzhucai-nest-clitgcnx-electron-testaerolito-next@cubos/neutrino-preset-weblipemat-js-boilerplateoc-template-fiba-react-compiler@spotify-backstage/cli@kibalabs/react-static-plugin-typescript@ironbay/react-scripts-legosreact-microservice-scripts@firetask/builders@firetask/nodedotsbi-react-scripts-tsreact-scripts-jt@mohitsingh/react-scriptsextend-serviceecarx-buildcustom-react-dev-utils-anjaneslint-config-demingdemingreact-dev-utils-anjan@app-server/cli@citcfe/cicli-scriptsdeming-webpack@nodecorejs/preset-built-inp-react-dev-utils@built/react-scripts@willhayes/want-cli42dd8860-c724@psimk/porter-web-tools@nbarinov/commonwebpack-config-single-spa-tsjupcgdemouiufy-clikbc-ui-cli-v2@jkyu/emp-vue2-tswebpack-boxmomula@artel/arca-widget-cli@texttree/demo-bsa-reference-rcl@startdt/cli-plugin-typescript@launch/config@klizan/react-dev-utilsbamboo-react-cli@aotemanlei/clibamboosssstestesdfasdfsadfsdaf@mediamonks/porter-webpack-tools@startdt/plugin-typescriptfork-ts-checker-workeralr-react-dev-utilshexidave-react-scripts-tshzero-react-scriptshzero-webpack-scriptsbpuns-testhao-base@psimk/porter-webpack-tools@electron.land/electron-scripts@ez-fe/build@ez-fe/core@ez-fe/dev@ez-fe/ez@fect-ui/fect-cli@akijoey/react-scripts@akijoey/vue-scriptsts-component-scriptts-component-scriptscoral-cli6@zrf9018/marvel-clitest-bin-hereact-dev-utils-fork@kanzhucai/nest-clichancegraff-dev-utilsmassive-stack-webpoc-wc-tsworklio-clibee-react-serverdevops-react-serverleng-cliforce-storybook@eviljs/webpack-config-react@dite/bundler
9.0.2

5 months ago

9.0.1

5 months ago

9.0.0

6 months ago

8.0.0

1 year ago

6.5.3

1 year ago

7.3.0

1 year ago

7.2.14

1 year ago

7.2.13

2 years ago

7.2.12

2 years ago

7.2.11

2 years ago

7.2.10

2 years ago

7.2.9

2 years ago

7.2.8

2 years ago

7.2.7

2 years ago

6.5.2

2 years ago

7.2.6

2 years ago

7.2.5

2 years ago

7.2.4

2 years ago

7.2.3

2 years ago

7.2.2

2 years ago

6.5.1

2 years ago

6.5.0

2 years ago

7.0.0

2 years ago

7.1.1

2 years ago

7.1.0

2 years ago

7.0.0-alpha.11

2 years ago

7.0.0-alpha.12

2 years ago

7.0.0-alpha.15

2 years ago

7.0.0-alpha.16

2 years ago

7.0.0-alpha.13

2 years ago

7.0.0-alpha.14

2 years ago

7.2.1

2 years ago

7.2.0

2 years ago

6.4.2

2 years ago

6.4.1

2 years ago

7.0.0-alpha.10

2 years ago

7.0.0-alpha.9

2 years ago

6.3.6

2 years ago

6.3.5

2 years ago

6.4.0

2 years ago

6.3.4

2 years ago

6.3.3

3 years ago

7.0.0-alpha.8

3 years ago

7.0.0-alpha.7

3 years ago

7.0.0-alpha.6

3 years ago

6.3.2

3 years ago

7.0.0-alpha.5

3 years ago

7.0.0-alpha.4

3 years ago

6.3.0

3 years ago

6.3.1

3 years ago

6.2.13

3 years ago

6.2.11

3 years ago

6.2.12

3 years ago

7.0.0-alpha.3

3 years ago

7.0.0-alpha.2

3 years ago

6.2.10

3 years ago

6.2.5

3 years ago

6.2.4

3 years ago

6.2.7

3 years ago

6.2.6

3 years ago

6.2.9

3 years ago

6.2.8

3 years ago

6.2.3

3 years ago

6.2.2

3 years ago

6.2.1

3 years ago

6.2.0

3 years ago

7.0.0-alpha.1

3 years ago

6.1.1

3 years ago

6.1.0

3 years ago

6.0.8

3 years ago

6.0.7

3 years ago

6.0.6

3 years ago

6.0.5

3 years ago

6.0.4

3 years ago

6.0.3

3 years ago

6.0.2

3 years ago

6.0.1

3 years ago

6.0.0

3 years ago

6.0.0-alpha.3

3 years ago

5.2.1

3 years ago

6.0.0-alpha.2

3 years ago

6.0.0-alpha.1

3 years ago

5.2.0

4 years ago

5.1.0

4 years ago

5.0.14

4 years ago

5.0.13

4 years ago

5.0.12

4 years ago

5.0.11

4 years ago

5.0.10

4 years ago

5.0.9

4 years ago

5.0.8

4 years ago

5.0.7

4 years ago

5.0.6

4 years ago

5.0.5

4 years ago

5.0.4

4 years ago

5.0.3

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

4 years ago

5.0.0-beta.7

4 years ago

5.0.0-beta.6

4 years ago

5.0.0-beta.5

4 years ago

5.0.0-beta.4

4 years ago

5.0.0-beta.3

4 years ago

5.0.0-beta.2

4 years ago

5.0.0-alpha.17

4 years ago

5.0.0-beta.1

4 years ago

5.0.0-alpha.16

4 years ago

5.0.0-alpha.15

4 years ago

5.0.0-alpha.14

4 years ago

5.0.0-alpha.13

4 years ago

5.0.0-alpha.12

4 years ago

5.0.0-alpha.11

4 years ago

5.0.0-alpha.10

4 years ago

4.1.6

4 years ago

5.0.0-alpha.9

4 years ago

5.0.0-alpha.8

4 years ago

5.0.0-alpha.7

4 years ago

5.0.0-alpha.6

4 years ago

5.0.0-alpha.5

4 years ago

5.0.0-alpha.4

4 years ago

5.0.0-alpha.3

4 years ago

4.1.5

4 years ago

5.0.0-alpha.2

4 years ago

5.0.0-alpha.1

4 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.5

4 years ago

4.0.4

4 years ago

4.0.3

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-beta.5

4 years ago

4.0.0-beta.4

4 years ago

4.0.0-beta.3

4 years ago

4.0.0-beta.2

4 years ago

4.0.0-beta.1

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.6.0

4 years ago

1.5.1

4 years ago

1.5.0

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.1-beta.1

5 years ago

1.3.0

5 years ago

1.3.0-beta.1

5 years ago

1.2.0-beta.4

5 years ago

1.2.0-beta.3

5 years ago

1.2.0

5 years ago

1.2.0-beta.2

5 years ago

1.1.1

5 years ago

1.2.0-beta.1

5 years ago

1.1.0

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

1.0.0-alpha.10

5 years ago

1.0.0-alpha.9

5 years ago

1.0.0-alpha.8

5 years ago

1.0.0-alpha.7

5 years ago

1.0.0-alpha.6

5 years ago

1.0.0-alpha.5

5 years ago

1.0.0-alpha.4

5 years ago

1.0.0-alpha.3

5 years ago

1.0.0-alpha.2

5 years ago

1.0.0-alpha.1

5 years ago

1.0.0-alpha.0

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.15

5 years ago

0.4.14

5 years ago

0.4.13

5 years ago

0.4.12

5 years ago

0.4.11

5 years ago

0.4.10

5 years ago

0.4.9

6 years ago

0.4.8

6 years ago

0.4.7

6 years ago

0.4.6

6 years ago

0.4.5

6 years ago

0.4.4

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

7 years ago

0.2.7

7 years ago

0.2.6

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

7 years ago

0.2.2

7 years ago

0.2.1

7 years ago

0.2.0

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago