npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-checker-rspack-plugin

v1.0.3

Published

Runs typescript type checker and linter on separate process.

Downloads

32,078

Readme

ts-checker-rspack-plugin

Rspack plugin that runs TypeScript type checker on a separate process.

Credits

This plugin is forked from TypeStrong/fork-ts-checker-webpack-plugin, which is created by Piotr Oleś. See the original project's LICENSE.

Big thanks to fork-ts-checker-webpack-plugin creators and contributors for their great work. ❤️

Features

💡 For Rsbuild projects, use @rsbuild/plugin-type-check to get out-of-the-box experience.

Installation

This plugin requires Node.js >=16.0.0+, Rspack ^1.0.0, TypeScript ^3.8.0

# with npm
npm install -D ts-checker-rspack-plugin

# with yarn
yarn add -D ts-checker-rspack-plugin

# with pnpm
pnpm add -D ts-checker-rspack-plugin

The minimal Rspack config with builtin:swc-loader.

// rspack.config.js
const { TsCheckerRspackPlugin } = require('ts-checker-rspack-plugin');

module.exports = {
  context: __dirname, // to automatically find tsconfig.json
  entry: './src/index.ts',
  resolve: {
    extensions: ['.ts', '.tsx', '.js'],
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'builtin:swc-loader',
        options: {
          jsc: {
            parser: {
              syntax: 'typescript',
            },
          },
        },
      },
    ],
  },
  plugins: [new TsCheckerRspackPlugin()],
  watchOptions: {
    // for some systems, watching many files can result in a lot of CPU or memory usage
    // https://rspack.dev/config/watch#watchoptionsignored
    // don't use this pattern, if you have a monorepo with linked packages
    ignored: /node_modules/,
  },
};

Modules resolution

It's very important to be aware that this plugin uses TypeScript's, not Rspack'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 Rspack to compile files.

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

Options

| Name | Type | Default value | Description | | ------------ | ---------------------------------------------------------------- | ----------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | async | boolean | compiler.options.mode === 'development' | If true, reports issues after Rspack's compilation is done. Thanks to that it doesn't block the compilation. Used only in the watch mode. | | typescript | object | {} | See TypeScript options. | | issue | object | {} | See Issues options. | | formatter | string or object or function | codeframe | Available 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-infrastructure | console | Console-like object to print issues in async mode. | | devServer | boolean | true | If set to false, errors will not be reported to Dev Server. |

TypeScript options

Options for the TypeScript checker (typescript option object).

| Name | Type | Default value | Description | | ------------------- | ------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | memoryLimit | number | 8192 | Memory limit for the checker process in MB. If the process exits with the allocation failed error, try to increase this number. | | configFile | string | 'tsconfig.json' | Path to the tsconfig.json file (path relative to the compiler.options.context or absolute path) | | 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. | | 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. 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 ts-checker-rspack-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. | | build | boolean | false | The 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). | | diagnosticOptions | object | { syntactic: false, semantic: true, declaration: false, global: false } | Settings to select which diagnostics do we want to perform. | | profile | boolean | false | Measures and prints timings related to the TypeScript performance. | | typescriptPath | string | require.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)[];

| Name | Type | Default value | Description | | --------- | ------------- | ------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- | | include | IssueFilter | undefined | If object, defines issue properties that should be matched. If function, acts as a predicate where issue is an argument. | | exclude | IssueFilter | undefined | Same 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 Rspack configuration
  plugins: [
    new TsCheckerRspackPlugin({
      issue: {
        include: [{ file: '**/src/**/*' }],
        exclude: [{ file: '**/*.spec.ts' }],
      },
    }),
  ],
};

Plugin hooks

This plugin provides some custom Rspack hooks:

| Hook key | Type | Params | Description | | ---------- | -------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | 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. | | waiting | SyncHook | compilation | Waiting for the issues checking. | | canceled | SyncHook | compilation | Issues checking for the compilation has been canceled. | | error | SyncHook | compilation | An error occurred during issues checking. | | 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. |

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

// ./src/rspack/MyRspackPlugin.js
const { TsCheckerRspackPlugin } = require('ts-checker-rspack-plugin');

class MyRspackPlugin {
  apply(compiler) {
    const hooks = TsCheckerRspackPlugin.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 = MyRspackPlugin;

// rspack.config.js
const { TsCheckerRspackPlugin } = require('ts-checker-rspack-plugin');
const MyRspackPlugin = require('./src/rspack/MyRspackPlugin');

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

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 TsCheckerRspackPlugin settings.

License

MIT License