Skip to content

Commit

Permalink
Merge pull request #23 from vclemenzi/fix-14
Browse files Browse the repository at this point in the history
fix(#14)!: full server restart for new pages
  • Loading branch information
vclemenzi authored Apr 27, 2024
2 parents 19f3d5d + 949037e commit 8d9937c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 18 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 13 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
});
},
};
};
11 changes: 9 additions & 2 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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");
Expand All @@ -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();
};

0 comments on commit 8d9937c

Please sign in to comment.