如何restart Vite项目

721 阅读1分钟

目前vite的项目已经实现了项目内文件改变,可restart项目的能力。
但是如果项目外的文件更改,如何能导致vite项目的restart?

- 使用nodemon命令

vite项目的package.json文件的script配置如下命令:

"scripts": {
    "dev": "vite --force",
    "build": "vite build",
    "preview": "vite preview",
    "runWatchVite": "nodemon -w ../babelPlugin/index.js -x 'vite'"
},

-w (指定watch监控文件)
-x (指定excute执行命令)

- 使用vite-plugin-restart

vite项目中的vite.config.js 使用vite-plugin-restart

配置如下:

export default defineConfig({
  plugins: [
    ...
    ViteRestart({
      cache: false,
      restart: [
        '../babelPlugin/**/*.js',
      ]
    })
  ],

以上方法均实现restart vite项目,
但是利用到的相关文件仍然是restart之前的,跟退出vite,然后npm run vite项目不同