UNPKG

@scalar/build-tooling

Version:

Build tooling and helpers

36 lines (33 loc) 1.22 kB
import pm from 'picomatch'; /** Reboots the vite server when workspace dependencies changes are detected */ function ViteWatchWorkspace(packageDir = 'scalar/packages') { const currentDir = process.cwd().split('/').at(-1); const timer = { instance: null, /** Run a function after a certain delay */ run: (callback, delay = 500) => { // Clear any existing callbacks to debounce timer.clear(); // Set the new delay timer.instance = setTimeout(callback, delay); }, /** Cancel any pending actions */ clear: () => timer.instance && clearTimeout(timer.instance), }; // We match any workspace package dist that are not the current one const matcher = pm(`**/${packageDir}/!(${currentDir})/dist/**`); return { name: 'vite-plugin-workspace-watch', apply: 'serve', handleHotUpdate({ file, server }) { if (matcher(file)) { timer.run(() => { console.log('Reloading the mothership...'); server.restart(); }); return []; } }, }; } export { ViteWatchWorkspace };