diff --git a/package.json b/package.json index 39cb292..886f200 100644 --- a/package.json +++ b/package.json @@ -28,12 +28,14 @@ "dependencies": { "html-minifier-terser": "^7.2.0", "markdown-it": "^14.1.0", + "micromatch": "^4.0.5", "rollup-plugin-copy": "^3.5.0", "tiny-glob": "^0.2.9", "vite": "^5.2.9" }, "devDependencies": { "@types/html-minifier-terser": "^7.0.2", + "@types/micromatch": "^4.0.7", "@types/node": "^20.12.7", "prettier": "3.2.5", "terser": "^5.30.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e00c661..a19b05a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: markdown-it: specifier: ^14.1.0 version: 14.1.0 + micromatch: + specifier: ^4.0.5 + version: 4.0.5 rollup-plugin-copy: specifier: ^3.5.0 version: 3.5.0 @@ -27,6 +30,9 @@ importers: '@types/html-minifier-terser': specifier: ^7.0.2 version: 7.0.2 + '@types/micromatch': + specifier: ^4.0.7 + version: 4.0.7 '@types/node': specifier: ^20.12.7 version: 20.12.7 @@ -442,6 +448,9 @@ packages: cpu: [x64] os: [win32] + '@types/braces@3.0.4': + resolution: {integrity: sha512-0WR3b8eaISjEW7RpZnclONaLFDf7buaowRHdqLp4vLj54AsSAYWfh3DRbfiYJY9XDxMgx1B4sE1Afw2PGpuHOA==} + '@types/estree@1.0.5': resolution: {integrity: sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==} @@ -454,6 +463,9 @@ packages: '@types/html-minifier-terser@7.0.2': resolution: {integrity: sha512-mm2HqV22l8lFQh4r2oSsOEVea+m0qqxEmwpc9kC1p/XzmjLWrReR9D/GRs8Pex2NX/imyEH9c5IU/7tMBQCHOA==} + '@types/micromatch@4.0.7': + resolution: {integrity: sha512-C/FMQ8HJAZhTsDpl4wDKZdMeeW5USjgzOczUwTGbRc1ZopPgOhIEnxY2ZgUrsuyy4DwK1JVOJZKFakv3TbCKiA==} + '@types/minimatch@5.1.2': resolution: {integrity: sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==} @@ -1325,6 +1337,8 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.14.3': optional: true + '@types/braces@3.0.4': {} + '@types/estree@1.0.5': {} '@types/fs-extra@8.1.5': @@ -1338,6 +1352,10 @@ snapshots: '@types/html-minifier-terser@7.0.2': {} + '@types/micromatch@4.0.7': + dependencies: + '@types/braces': 3.0.4 + '@types/minimatch@5.1.2': {} '@types/node@20.12.7': diff --git a/src/index.ts b/src/index.ts index 72e1512..bf3292f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,11 +4,12 @@ import { resolve } from "./utils/resolve"; import { getExternalDeps } from "./utils/config"; import { getInputs, type Frameworks } from "./utils/input"; import { getPageFromIndex } from "./utils/page"; -import { configureServer } from "./server"; +import { configureServerMiddlewares, handleRestart } from "./server"; import { load } from "./load"; import glob from "tiny-glob"; import copy from "rollup-plugin-copy"; import path from "path"; +import mm from "micromatch"; export const multip = (config?: Config): Plugin => { const root = config?.directory || "src/pages"; @@ -104,7 +105,17 @@ export const multip = (config?: Config): Plugin => { }, configureServer(server) { - configureServer(server, { root, frameworks, config: config || {} }) + configureServerMiddlewares(server, { root, frameworks, config: config || {} }) + + server.watcher.add("**/*.{svelte,vue,tsx,jsx,md,html,css,scss,sass,less,js,ts}"); + + server.watcher.on("add", (file) => { + handleRestart(server, file); + }); + + server.watcher.on("unlink", (file) => { + handleRestart(server, file); + }); }, }; }; diff --git a/src/server/index.ts b/src/server/index.ts index ce1cf83..e9bbaf0 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,5 +1,6 @@ -import type { ViteDevServer } from "vite"; import { scripts } from "./scripts"; +import mm from "micromatch"; +import type { ViteDevServer } from "vite"; import type { Frameworks } from "../utils/input"; import type { Config } from "../types"; @@ -9,7 +10,7 @@ export type Params = { config: Config; }; -export const configureServer = (server: ViteDevServer, params: Params) => { +export const configureServerMiddlewares = (server: ViteDevServer, params: Params) => { server.middlewares.use((req, res, next) => { if (req.url === "/@multip/refresh-runtime") { res.setHeader("Content-Type", "application/javascript"); @@ -23,3 +24,9 @@ export const configureServer = (server: ViteDevServer, params: Params) => { next(); }); }; + +export const handleRestart = (server: ViteDevServer, file: string) => { + if (!mm.isMatch(file, "**/*.{svelte,vue,tsx,jsx,md,html,css,scss,sass,less,js,ts}")) return; + + server.restart(); +};