diff --git a/config/helpers/vite_plugin_auto_stop.mjs b/config/helpers/vite_plugin_auto_stop.mjs new file mode 100644 index 0000000000000000000000000000000000000000..1c5a82b8e3b3b51c302585606348645d089901c2 --- /dev/null +++ b/config/helpers/vite_plugin_auto_stop.mjs @@ -0,0 +1,43 @@ +import chokidar from 'chokidar'; + +/** + * This vite plugin automatically stops vite if + * + * 1. a new dependency install happened, because it could affect vite itself + * 2. a new entry point is created, because right now our entry points are + * statically looked at during start up + */ +export function AutoStopPlugin() { + return { + name: 'vite-plugin-auto-stop', + configureServer(server) { + const nodeModulesWatcher = chokidar.watch(['node_modules/.yarn-integrity'], { + ignoreInitial: true, + }); + const pageEntrypointsWatcher = chokidar.watch( + [ + 'app/assets/javascripts/pages/**/*.js', + 'ee/app/assets/javascripts/pages/**/*.js', + 'jh/app/assets/javascripts/pages/**/*.js', + ], + { + ignoreInitial: true, + }, + ); + + // GDK will restart Vite server for us + const stop = () => process.kill(process.pid); + + pageEntrypointsWatcher.on('add', stop); + pageEntrypointsWatcher.on('unlink', stop); + nodeModulesWatcher.on('add', stop); + nodeModulesWatcher.on('change', stop); + nodeModulesWatcher.on('unlink', stop); + + server.httpServer?.addListener?.('close', () => { + pageEntrypointsWatcher.close(); + nodeModulesWatcher.close(); + }); + }, + }; +} diff --git a/vite.config.js b/vite.config.js index d9e8e99d781f949fcef707da69d27728e0d1f2b7..0dcfa63b250a74efdcfe1b6a79d1ba023e0d1f51 100644 --- a/vite.config.js +++ b/vite.config.js @@ -5,7 +5,6 @@ import path from 'node:path'; import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue2'; import graphql from '@rollup/plugin-graphql'; -import chokidar from 'chokidar'; import globby from 'globby'; import { viteCommonjs } from '@originjs/vite-plugin-commonjs'; import webpackConfig from './config/webpack.config'; @@ -20,6 +19,7 @@ import { /* eslint-disable import/extensions */ import { viteCSSCompilerPlugin } from './scripts/frontend/lib/compile_css.mjs'; import { viteTailwindCompilerPlugin } from './scripts/frontend/tailwindcss.mjs'; +import { AutoStopPlugin } from './config/helpers/vite_plugin_auto_stop.mjs'; import { FixedRubyPlugin } from './config/helpers/vite_plugin_ruby_fixed.mjs'; /* eslint-enable import/extensions */ @@ -65,38 +65,6 @@ const JH_ALIAS_FALLBACK = [ }, ]; -const autoRestartPlugin = { - configureServer(server) { - const nodeModulesWatcher = chokidar.watch(['node_modules/.yarn-integrity'], { - ignoreInitial: true, - }); - const pageEntrypointsWatcher = chokidar.watch( - [ - 'app/assets/javascripts/pages/**/*.js', - 'ee/app/assets/javascripts/pages/**/*.js', - 'jh/app/assets/javascripts/pages/**/*.js', - ], - { - ignoreInitial: true, - }, - ); - - // GDK will restart Vite server for us - const stop = () => process.kill(process.pid); - - pageEntrypointsWatcher.on('add', stop); - pageEntrypointsWatcher.on('unlink', stop); - nodeModulesWatcher.on('add', stop); - nodeModulesWatcher.on('change', stop); - nodeModulesWatcher.on('unlink', stop); - - server.httpServer?.addListener?.('close', () => { - pageEntrypointsWatcher.close(); - nodeModulesWatcher.close(); - }); - }, -}; - /** * This is a simple-reimplementation of the copy-webpack-plugin * @@ -207,7 +175,7 @@ export default defineConfig({ viteCopyPlugin({ patterns: copyFilesPatterns, }), - viteGDKConfig.enabled ? autoRestartPlugin : null, + viteGDKConfig.enabled ? AutoStopPlugin() : null, FixedRubyPlugin(), vue({ template: {