vite-plugin-checker

repository·main·Indexed 22 days ago

https://github.com/fi3ework/vite-plugin-checker

A Vite plugin that offloads type-checking and linting to a worker thread to maintain dev server responsiveness. It supports TypeScript, vue-tsc, ESLint, Biome, Stylelint, and oxlint. The plugin provides a browser UI overlay for displaying diagnostics and allows for advanced configuration of each checker, including custom lint commands and watch paths to optimize performance.

Tokens
8K
Snippets
17
Records
45
Agent score
78%

What's inside vite-plugin-checker

  1. Overview of vite-plugin-checker

    main

    vite-plugin-checker is a Vite plugin designed to run type-checking and linting tools in a separate worker thread. This prevents the main Vite development server thread from being blocked by heavy processes.

    Supported tools include:

    • TypeScript
    • vue-tsc
    • ESLint
    • Biome
    • Stylelint
  2. What is vite-plugin-checker

    main
    vite-plugin-checker is a Vite plugin designed to add type checking and linting support to your Vite development environment. It runs tools like TypeScript, vue-tsc, ESLint, Stylelint, and oxlint in a separate worker thread. This approach ensures that the heavy lifting of type checking and linting does not block the main Vite development server, maintaining a fast and responsive development experience.
  3. Supported checkers in vite-plugin-checker

    main

    The plugin provides built-in support for several type-checking and linting tools. Currently supported checkers include:

    • TypeScript
    • ESLint
    • Biome
    • vue-tsc
    • Stylelint
    • oxlint

    Note that each checker may require specific peer dependencies to function correctly. Refer to the individual checker documentation pages for installation requirements.

  4. Understand @vite-plugin-checker/runtime

    main

    The @vite-plugin-checker/runtime package contains the runtime code required for vite-plugin-checker to function.

    Note: This package is not released to NPM independently. It is automatically bundled into the main vite-plugin-checker package during the build process. You should not attempt to install this package directly via a package manager.

  5. Integrate with Nuxt3 as a Vite plugin

    main

    To use vite-plugin-checker with Nuxt3 and enable the global error overlay, follow these steps (requires version 0.5.5 or higher):

    1. Install dependencies: Add vite-plugin-checker, typescript, vue-tsc, and @types/node to your project's devDependencies.
    2. Create a runtime component: Create a file named vite-plugin-checker.vue that imports the runtime entry point:
      <script setup>
      import('/@vite-plugin-checker-runtime-entry')
      </script>
    3. Configure Nuxt: Add the checker to the vite.plugins array in your nuxt.config.ts.
    4. Mount the component: Import and use the component in your root component, wrapped in <DevOnly> and <ClientOnly> tags to ensure it only runs in the browser during development.
    // 1. vite-plugin-checker.vue
    <script setup>
    import('/@vite-plugin-checker-runtime-entry')
    </script>
    
    // 2. nuxt.config.ts
    import { checker } from 'vite-plugin-checker'
    export default defineNuxtConfig({
      vite: {
        plugins: [
          checker({
            vueTsc: true,
          }),
        ],
      },
    })
    
    // 3. Root component (e.g., app.vue)
    <script setup lang="ts">
    import Vpc from './vite-plugin-checker.vue'
    </script>
    
    <template>
      <DevOnly>
        <ClientOnly>
          <Vpc />
        </ClientOnly>
      </DevOnly>
    </template>
  6. Install and enable oxlint

    main

    To use oxlint with vite-plugin-checker, you must first ensure that oxlint is installed as a dependency in your project. Once installed, enable it by adding the oxlint field to your checker plugin configuration. You can either set it to true to use the default settings or provide a configuration object for advanced control.

    // e.g.
    export default defineConfig({
      plugins: [
        checker({
          oxlint: true,
          // or
          oxlint: {
            lintCommand: "oxlint -D correctness",
          },
        }),
      ],
    });
  7. Configure vite-plugin-checker in Vite

    main

    Add the checker plugin to your vite.config.js (or .ts) file. You must specify which checker you want to run (e.g., typescript: true).

    To avoid running checkers during unit testing with Vitest, you can conditionally include the plugin using an environment variable check.

    // Standard configuration with TypeScript enabled
    import checker from 'vite-plugin-checker'
    export default {
      plugins: [
        checker({
          // e.g. use TypeScript check
          typescript: true,
        }),
      ],
    }
  8. Optimize ESLint performance with watchPath

    main

    If you encounter EMFILE: too many open files errors or want to improve performance during development, use the watchPath option. This limits the file watching scope to specific directories instead of the entire project root. This option is only applicable in dev mode.

    export default {
      plugins: [
        checker({
          eslint: {
            lintCommand: 'eslint "./src/**/*.{ts,tsx}"',
            // Single directory
            watchPath: './src',
            
            // Multiple directories
            // watchPath: ['./src', './lib'],
          },
        }),
      ],
    }
  9. Optimize Stylelint performance with watchPath

    main

    If you encounter EMFILE: too many open files errors or want to improve performance, use the watchPath option. This limits file watching to specific directories instead of watching the entire project root. This option is only active in dev mode.

    export default {
      plugins: [
        checker({
          stylelint: {
            lintCommand: 'stylelint ./src/**/*.{css,vue}',
            // Single directory
            watchPath: './src',
            
            // Multiple directories
            // watchPath: ['./src', './components'],
          },
        }),
      ],
    }
  10. How to add a checker to your configuration

    main

    To enable a checker in your Vite configuration, use the corresponding property in the vite-plugin-checker plugin options. You have three ways to configure them:

    1. Default Configuration: Set the checker property to true. This uses the checker with its default settings (Note: this does not apply to eslint and stylelint, which require specific configuration).
    2. Advanced Configuration: Pass an object instead of a boolean to provide custom settings for that specific checker.
    3. Disable a Checker: Set the property to false or leave the field blank to ensure the checker is not used.

    Important: You must manually install the peer dependencies required by the checker you are enabling.