Created
April 24, 2024 09:32
-
-
Save aztack/8ca5183ac93cd288b08ba1e51357c042 to your computer and use it in GitHub Desktop.
Vite-watch-external-plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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