Skip to content
项目

antfu/vite-plugin-restart

在 GitHub 查看

关于

Custom files/globs to restart Vite server

开发工具

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
-1
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

vite-plugin-restart

Custom files/globs to restart Vite server



Usage

Install

npm i vite-plugin-restart -D # yarn add vite-plugin-restart -D

Add it to vite.config.js

// vite.config.js
import ViteRestart from 'vite-plugin-restart'

export default {
  plugins: [
    ViteRestart({
      restart: [
        'my.config.[jt]s',
      ]
    })
  ],
}

Changes to my.config.js or my.config.ts will now restart the server automatically.

Options

export interface VitePluginRestartOptions {
  /**
   * Enable glob support for watcher (it's disabled by Vite, but add this plugin will turn it on by default)
   * @default true
   */
  glob?: boolean
  /**
   * Delay in ms before restarting the server
   * @default 500
   */
  delay?: number
  /**
   * Array of files to watch, changes to those file will trigger a server restart
   */
  restart?: string | string[]
  /**
   * Array of files to watch, changes to those file will trigger a client full page reload
   */
  reload?: string | string[]
  /**
   * Enable content-based change detection to prevent unnecessary restarts
   * when file metadata changes but content remains the same
   * @default true
   */
  contentCheck?: boolean
}

Content-Based Change Detection

By default, the plugin now uses content-based change detection (contentCheck: true). This prevents infinite restart loops when watching auto-generated files that may be rewritten with the same content.

For example, when watching .eslintrc-auto-import.json:

export default {
  plugins: [
    ViteRestart({
      restart: [
        '.eslintrc-auto-import.json',
      ],
      // Content check is enabled by default
      // Only restarts if file content actually changes
      contentCheck: true,
    })
  ],
}

The plugin calculates a SHA-256 hash of the file content and only triggers a restart if the hash changes. This solves the problem of:

  • Auto-generated files being rewritten during the build process
  • File modification timestamps changing without actual content changes
  • Infinite restart loops caused by files being regenerated on every server restart

You can disable this feature if needed:

export default {
  plugins: [
    ViteRestart({
      restart: ['my.config.js'],
      contentCheck: false, // Disable content-based detection
    })
  ],
}

Motivation

Byebye nodemon -w my.config.js -x 'vite'

License

MIT License © 2021 Anthony Fu

探索相关项目

加载中…