nitropage
Version:
A free and open source, extensible visual page builder based on SolidStart.
56 lines (49 loc) • 1.67 kB
text/typescript
import chokidar, { FSWatcher } from "chokidar";
import crypto from "crypto";
import { debounce } from "es-toolkit";
import { readFile, writeFile } from "fs/promises";
import { relative, resolve } from "path";
import { runtimePath } from "../lib/server/path";
export const resolveCwd = (...path: string[]) =>
resolve(process.cwd(), ...path);
let fsWatcher: FSWatcher;
export const initVinxiReload = () => {
if (fsWatcher) fsWatcher.close();
fsWatcher = chokidar.watch(
[
resolveCwd(runtimePath, "server.config.ts"),
resolveCwd(runtimePath, "plugin.ts"),
].filter(Boolean),
{
ignoreInitial: true,
persistent: true,
},
);
fsWatcher.on("all", reloadVinxi);
};
const reloadVinxi = debounce(async () => {
const appConfigPath = resolveCwd("app.config.ts");
const content = await readFile(appConfigPath, "utf-8");
await writeFile(appConfigPath, content);
}, 250);
const hashCache: Record<string, string> = {};
const cwd = process.cwd();
// TODO: Will need a rewrite for rolldown (https://vite.dev/guide/rolldown.html#manualchunks-to-advancedchunks)
/**
* Makes sure that css modules have consistent file names in server and client builds
*
* Vite / Rollup sometimes merge/rename js files and their assets differently for server and client builds.
* If the css file names are not consistent, this will result in css foucs.
*/
export const manualChunks = (id: string) => {
if (!id.endsWith("module.css")) {
return;
}
const relativeId = relative(cwd, id);
const hash = (hashCache[id] ??= crypto
.createHash("sha256")
.update(relativeId)
.digest("hex")
.slice(0, 10));
return hash;
};