Advertisement
Guest User

vite-plugin-restart.js

a guest
Sep 3rd, 2021
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Reloads the server when a vue file under a certain folder gets deleted or created
  2.  
  3. import fs from "fs";
  4.  
  5. function touch(path) {
  6.     const time = new Date();
  7.  
  8.     try {
  9.         fs.utimesSync(path, time, time);
  10.     } catch (err) {
  11.         fs.closeSync(fs.openSync(path, "w"));
  12.     }
  13. }
  14.  
  15. // for "options" here I just pass "pages" in the vite config file when I init the plugin, feel free to fix this
  16. export default function FullRestart(options = "") {
  17.     let configFile = "vite.config.js";
  18.  
  19.     return {
  20.         name: "vite-plugin-restart",
  21.         configureServer(server) {
  22.             server.watcher.on("add", (path) => {
  23.                 if (options != "") {
  24.                     if (path.includes(options) && path.endsWith(".vue")) {
  25.                         touch(configFile);
  26.                         server.ws.send({ type: "full-reload" });
  27.                     }
  28.                 }
  29.             });
  30.             server.watcher.on("unlink", (path) => {
  31.                 if (options != "") {
  32.                     if (path.includes(options) && path.endsWith(".vue")) {
  33.                         touch(configFile);
  34.                         server.ws.send({ type: "full-reload" });
  35.                     }
  36.                 }
  37.             });
  38.         },
  39.     };
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement