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

@rsbuild/plugin-type-check

v1.0.1

Published

An Rsbuild plugin to run TypeScript type checker in a separate process.

Downloads

132,957

Readme

@rsbuild/plugin-type-check

An Rsbuild plugin to run TypeScript type checker in a separate process.

Introduction

This plugin internally integrates with fork-ts-checker-webpack-plugin.

The type checking logic of fork-ts-checker-webpack-plugin is similar to the native tsc command of TypeScript. It automatically reads the configuration options from tsconfig.json and can also be modified via the configuration options provided by the Type Check plugin.

The behavior of the plugin differs in the development and production builds:

  • In development mode, type errors do not block the build process. They are only logged in the terminal.
  • In production mode, type errors cause the build to fail in order to ensure the stability of the production code.

Usage

Install:

npm add @rsbuild/plugin-type-check -D

Add plugin to your rsbuild.config.ts:

// rsbuild.config.ts
import { pluginTypeCheck } from "@rsbuild/plugin-type-check";

export default {
  plugins: [pluginTypeCheck()],
};

Configuring tsconfig.json

The Type Check plugin by default performs checks based on the tsconfig.json file in the root directory of the current project. Below is an example of a tsconfig.json file, which you can also adjust according to the needs of your project.

{
  "compilerOptions": {
    "target": "ES2020",
    "lib": ["DOM", "ES2020"],
    "module": "ESNext",
    "strict": true,
    "skipLibCheck": true,
    "isolatedModules": true,
    "resolveJsonModule": true,
    "moduleResolution": "bundler"
  },
  "include": ["src"]
}

Please note that the fields in tsconfig.json will not affect the compilation behavior and output of Rsbuild, but will only affect the results of type checking.

Options

enable

Whether to enable TypeScript type checking.

  • Type: boolean
  • Default: true
  • Example:

Disable TypeScript type checking:

pluginTypeCheck({
  enable: false,
});

Enable type checking only in production mode:

pluginTypeCheck({
  enable: process.env.NODE_ENV === "production",
});

Enable type checking only in development mode (it is not recommended to disable type checking in production mode, as it may reduce the stability of the production code):

pluginTypeCheck({
  enable: process.env.NODE_ENV === "development",
});

forkTsCheckerOptions

To modify the options of fork-ts-checker-webpack-plugin, please refer to fork-ts-checker-webpack-plugin - README to learn about available options.

  • Type: Object | Function
  • Default:
const defaultOptions = {
  typescript: {
    // set 'readonly' to avoid emitting tsbuildinfo,
    // as the generated tsbuildinfo will break fork-ts-checker
    mode: "readonly",
    // enable build when using project reference
    build: useReference,
    // avoid OOM issue
    memoryLimit: 8192,
    // use tsconfig of user project
    configFile: tsconfigPath,
    // use typescript of user project
    typescriptPath: require.resolve("typescript"),
  },
  issue: {
    // ignore types errors from node_modules
    exclude: [({ file = "" }) => /[\\/]node_modules[\\/]/.test(file)],
  },
  logger: {
    log() {
      // do nothing
      // we only want to display error messages
    },
    error(message: string) {
      console.error(message.replace(/ERROR/g, "Type Error"));
    },
  },
};

Object Type

When the value of forkTsCheckerOptions is an object, it will be deeply merged with the default configuration.

pluginTypeCheck({
  forkTsCheckerOptions: {
    issue: {
      exclude: [({ file = "" }) => /[\\/]some-folder[\\/]/.test(file)],
    },
  },
});

Function Type

When the value of forkTsCheckerOptions is a function, the default configuration will be passed as the first argument. You can directly modify the configuration object or return an object as the final configuration.

pluginTypeCheck({
  forkTsCheckerOptions(options) {
    options.async = false;
    return options;
  },
});

exclude Example

The exclude option can filter based on the code, message, or file from TS errors.

For example, the type mismatch error can be excluded using code: 'TS2345':

pluginTypeCheck({
  forkTsCheckerOptions: {
    issue: {
      // Ignore "Argument of type 'string' is not assignable to parameter of type 'number'.ts(2345)"
      exclude: [{ code: "TS2345" }],
    },
  },
});

Or exclude files under /some-folder/ using file:

pluginTypeCheck({
  forkTsCheckerOptions: {
    issue: {
      exclude: [({ file = "" }) => /[\\/]some-folder[\\/]/.test(file)],
    },
  },
});

Notes

  • If you have enabled ts-loader in your project and manually configured compileOnly: false, please disable the Type Check plugin to avoid duplicate type checking.
  • Some errors will be displayed as warnings in IDEs such as VS Code, but they will still be displayed as errors in the fork-ts-checker-webpack-plugin check. For details, please refer to: Why are some errors reported as warnings?.

Performance Optimization

Type checking has a significant performance overhead. You can refer to the Performance Guide in the official TypeScript documentation for performance optimization.

For example, properly configuring the include and exclude scopes in tsconfig.json can significantly reduce unnecessary type checking and improve TypeScript performance:

{
  "include": ["src"],
  "exclude": ["**/node_modules", "**/.*/"]
}

Vue Components

fork-ts-checker-webpack-plugin does not support checking TypeScript code in .vue components. You can check for type issues in .vue files in the following ways:

  1. Install the vue-tsc package, which provides the ability to check types in .vue files.
  1. Add the vue-tsc --noEmit command to the build script in package.json:
{
  "scripts": {
-   "build": "rsbuild build"
+   "build": "vue-tsc --noEmit && rsbuild build"
  }
}
  1. Since the production build uses vue-tsc for type checking, you can disable the Type Check plugin in production mode to avoid redundant checks.
pluginTypeCheck({
  enable: process.env.NODE_ENV === "development",
});

License

MIT.