From f28a0773872bbf9ff6874b5199b78ca66b01e1aa Mon Sep 17 00:00:00 2001
From: Stanislav Lashmanov <slashmanov@gitlab.com>
Date: Tue, 28 Nov 2023 19:14:46 +0400
Subject: [PATCH] Watch for node_modules changes in Vite dev server

---
 package.json   |  1 +
 vite.config.js | 20 ++++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/package.json b/package.json
index e5d76331d421..999ea22ac48b 100644
--- a/package.json
+++ b/package.json
@@ -252,6 +252,7 @@
     "axios-mock-adapter": "^1.15.0",
     "babel-jest": "^28.1.3",
     "chalk": "^2.4.1",
+    "chokidar": "^3.5.3",
     "commander": "^2.20.3",
     "custom-jquery-matchers": "^2.1.0",
     "eslint": "8.54.0",
diff --git a/vite.config.js b/vite.config.js
index b4b35e10fda2..88d264531a63 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -3,6 +3,7 @@ import { defineConfig } from 'vite';
 import vue from '@vitejs/plugin-vue2';
 import graphql from '@rollup/plugin-graphql';
 import RubyPlugin from 'vite-plugin-ruby';
+import chokidar from 'chokidar';
 import { viteCommonjs } from '@originjs/vite-plugin-commonjs';
 import webpackConfig from './config/webpack.config';
 import {
@@ -58,6 +59,23 @@ const JH_ALIAS_FALLBACK = [
   },
 ];
 
+const autoRestartPlugin = {
+  configureServer(server) {
+    const watcher = chokidar.watch(['node_modules/.yarn-integrity'], {
+      ignoreInitial: true,
+    });
+
+    // GDK will restart Vite server for us
+    const stop = () => server.stop();
+
+    watcher.on('add', stop);
+    watcher.on('change', stop);
+    watcher.on('unlink', stop);
+
+    server.httpServer?.addListener?.('close', () => watcher.close());
+  },
+};
+
 export default defineConfig({
   cacheDir: path.resolve(__dirname, 'tmp/cache/vite'),
   resolve: {
@@ -76,6 +94,8 @@ export default defineConfig({
     ],
   },
   plugins: [
+    // VITE_ENABLED is present when running with GDK
+    process.env.VITE_ENABLED ? autoRestartPlugin : null,
     fixedRubyPlugin,
     vue({
       template: {
-- 
GitLab