index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. // src/index.ts
  2. import { relative, resolve } from "path";
  3. import colors from "picocolors";
  4. import picomatch from "picomatch";
  5. import { normalizePath } from "vite";
  6. function normalizePaths(root, path) {
  7. return (Array.isArray(path) ? path : [path]).map((path2) => resolve(root, path2)).map(normalizePath);
  8. }
  9. var src_default = (paths, config = {}) => ({
  10. name: "vite-plugin-full-reload",
  11. apply: "serve",
  12. // NOTE: Enable globbing so that Vite keeps track of the template files.
  13. config: () => ({ server: { watch: { disableGlobbing: false } } }),
  14. configureServer({ watcher, ws, config: { logger } }) {
  15. const { root = process.cwd(), log = true, always = true, delay = 0 } = config;
  16. const files = normalizePaths(root, paths);
  17. const shouldReload = picomatch(files);
  18. const checkReload = (path) => {
  19. if (shouldReload(path)) {
  20. setTimeout(() => ws.send({ type: "full-reload", path: always ? "*" : path }), delay);
  21. if (log)
  22. logger.info(`${colors.green("full reload")} ${colors.dim(relative(root, path))}`, { clear: true, timestamp: true });
  23. }
  24. };
  25. watcher.add(files);
  26. watcher.on("add", checkReload);
  27. watcher.on("change", checkReload);
  28. }
  29. });
  30. export {
  31. src_default as default,
  32. normalizePaths
  33. };