Skip to content

Instantly share code, notes, and snippets.

@aztack
Created April 24, 2024 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aztack/8ca5183ac93cd288b08ba1e51357c042 to your computer and use it in GitHub Desktop.
Save aztack/8ca5183ac93cd288b08ba1e51357c042 to your computer and use it in GitHub Desktop.
Vite-watch-external-plugin.js
function watchExternal(paths: string[]): Plugin {
console.log('Watching external paths: ', paths);
return {
name: 'watch-external',
configureServer(server) {
// Server is the Vite dev server instance
// Start watching external files
paths.forEach(path => {
if (!fs.existsSync(path)) {
console.warn(redBright(`${path}`))
}
})
const watcher = chokidar.watch(paths, {
ignoreInitial: true,
ignored: ['**/node_modules/**'] // ignore changes in node_modules
});
watcher.on('change', (path) => {
console.log(`External file changed: ${path}`);
server.ws.send({ type: 'full-reload' });
});
watcher.on('add', (path) => {
console.log(`External file added: ${path}`);
server.ws.send({ type: 'full-reload' });
});
// Close the watcher when the server is closed
server.watcher.on('close', () => {
watcher.close();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment