Bugfix watcher function. Refresh bundler after adding and removing pages
This commit is contained in:
parent
d56fe0ee55
commit
c6166f5fd2
@ -1,66 +1,19 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import type { RouterTypes, ServeOptions } from "bun";
|
import type { ServeOptions } from "bun";
|
||||||
import grabAppPort from "../../utils/grab-app-port";
|
import grabAppPort from "../../utils/grab-app-port";
|
||||||
import grabDirNames from "../../utils/grab-dir-names";
|
import grabDirNames from "../../utils/grab-dir-names";
|
||||||
import handleWebPages from "./web-pages/handle-web-pages";
|
import handleWebPages from "./web-pages/handle-web-pages";
|
||||||
import handleRoutes from "./handle-routes";
|
import handleRoutes from "./handle-routes";
|
||||||
import isDevelopment from "../../utils/is-development";
|
import isDevelopment from "../../utils/is-development";
|
||||||
import type { Server } from "http";
|
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
dev?: boolean;
|
dev?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
// type ServerOptions = Omit<ServeOptions, "fetch"> & {
|
|
||||||
// routes: { [K: string]: RouterTypes.RouteValue<string> };
|
|
||||||
// fetch?: (
|
|
||||||
// this: Server,
|
|
||||||
// request: Request,
|
|
||||||
// server: Server,
|
|
||||||
// ) => Response | Promise<Response>;
|
|
||||||
// };
|
|
||||||
|
|
||||||
export default async function (params?: Params): Promise<ServeOptions> {
|
export default async function (params?: Params): Promise<ServeOptions> {
|
||||||
const port = grabAppPort();
|
const port = grabAppPort();
|
||||||
const { PUBLIC_DIR } = grabDirNames();
|
const { PUBLIC_DIR } = grabDirNames();
|
||||||
|
|
||||||
// const opts: ServerOptions = {
|
|
||||||
// routes: {
|
|
||||||
// "/__hmr": {
|
|
||||||
// async GET(req) {
|
|
||||||
// if (!isDevelopment()) {
|
|
||||||
// return new Response(`Production Environment`);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// let controller: ReadableStreamDefaultController<string>;
|
|
||||||
// const stream = new ReadableStream<string>({
|
|
||||||
// start(c) {
|
|
||||||
// controller = c;
|
|
||||||
// global.HMR_CONTROLLERS.add(c);
|
|
||||||
// },
|
|
||||||
// cancel() {
|
|
||||||
// global.HMR_CONTROLLERS.delete(controller);
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
|
|
||||||
// return new Response(stream, {
|
|
||||||
// headers: {
|
|
||||||
// "Content-Type": "text/event-stream",
|
|
||||||
// "Cache-Control": "no-cache",
|
|
||||||
// Connection: "keep-alive",
|
|
||||||
// },
|
|
||||||
// });
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// "/api/*": {},
|
|
||||||
// "/*": {
|
|
||||||
// async GET(req) {
|
|
||||||
// return await handleWebPages({ req });
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// };
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
async fetch(req, server) {
|
async fetch(req, server) {
|
||||||
try {
|
try {
|
||||||
@ -72,17 +25,11 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
|||||||
);
|
);
|
||||||
const match = global.ROUTER.match(referer_url.pathname);
|
const match = global.ROUTER.match(referer_url.pathname);
|
||||||
|
|
||||||
if (!match?.filePath) {
|
const target_map = match?.filePath
|
||||||
return new Response(`Unhandled Path.`);
|
? global.BUNDLER_CTX_MAP?.find(
|
||||||
}
|
(m) => m.local_path == match.filePath,
|
||||||
|
)
|
||||||
const target_map = global.BUNDLER_CTX_MAP?.find(
|
: undefined;
|
||||||
(m) => m.local_path == match.filePath,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!target_map?.entrypoint) {
|
|
||||||
return new Response(`Target Path has no map`);
|
|
||||||
}
|
|
||||||
|
|
||||||
let controller: ReadableStreamDefaultController<string>;
|
let controller: ReadableStreamDefaultController<string>;
|
||||||
const stream = new ReadableStream<string>({
|
const stream = new ReadableStream<string>({
|
||||||
|
|||||||
@ -16,17 +16,20 @@ export default async function serverPostBuildFn({ artifacts }: Params) {
|
|||||||
|
|
||||||
for (let i = 0; i < global.HMR_CONTROLLERS.length; i++) {
|
for (let i = 0; i < global.HMR_CONTROLLERS.length; i++) {
|
||||||
const controller = global.HMR_CONTROLLERS[i];
|
const controller = global.HMR_CONTROLLERS[i];
|
||||||
const target_artifact = artifacts.find(
|
|
||||||
(a) => controller.target_map.local_path == a.local_path,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!target_artifact?.local_path) continue;
|
const target_artifact = artifacts.find(
|
||||||
|
(a) => controller.target_map?.local_path == a.local_path,
|
||||||
|
);
|
||||||
|
|
||||||
const final_artifact: Omit<GlobalHMRControllerObject, "controller"> = {
|
const final_artifact: Omit<GlobalHMRControllerObject, "controller"> = {
|
||||||
..._.omit(controller, ["controller"]),
|
..._.omit(controller, ["controller"]),
|
||||||
target_map: target_artifact,
|
target_map: target_artifact,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (!target_artifact) {
|
||||||
|
delete final_artifact.target_map;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
controller.controller.enqueue(
|
controller.controller.enqueue(
|
||||||
`event: update\ndata: ${JSON.stringify(final_artifact)}\n\n`,
|
`event: update\ndata: ${JSON.stringify(final_artifact)}\n\n`,
|
||||||
|
|||||||
@ -1,10 +1,15 @@
|
|||||||
import { watch } from "fs";
|
import { watch, existsSync } from "fs";
|
||||||
|
import path from "path";
|
||||||
import grabDirNames from "../../utils/grab-dir-names";
|
import grabDirNames from "../../utils/grab-dir-names";
|
||||||
import serverParamsGen from "./server-params-gen";
|
import serverParamsGen from "./server-params-gen";
|
||||||
// import allPagesBundler from "../bundler/all-pages-bundler";
|
import allPagesBundler from "../bundler/all-pages-bundler";
|
||||||
|
import serverPostBuildFn from "./server-post-build-fn";
|
||||||
|
import refreshRouter from "../../utils/refresh-router";
|
||||||
|
|
||||||
const { PAGES_DIR } = grabDirNames();
|
const { PAGES_DIR } = grabDirNames();
|
||||||
|
|
||||||
|
const PAGE_FILE_RE = /\.(tsx?|jsx?)$/;
|
||||||
|
|
||||||
export default function watcher() {
|
export default function watcher() {
|
||||||
watch(
|
watch(
|
||||||
PAGES_DIR,
|
PAGES_DIR,
|
||||||
@ -13,48 +18,39 @@ export default function watcher() {
|
|||||||
persistent: true,
|
persistent: true,
|
||||||
},
|
},
|
||||||
(event, filename) => {
|
(event, filename) => {
|
||||||
// if (!filename) return;
|
if (!filename) return;
|
||||||
// if (filename.match(/ /)) return;
|
if (!PAGE_FILE_RE.test(filename)) return;
|
||||||
// if (filename.match(/^node_modules\//)) return;
|
|
||||||
// if (filename.match(/\.bunext|\/?public\//)) return;
|
|
||||||
// if (!filename.match(/\.(tsx|ts|css|js|jsx)$/)) return;
|
|
||||||
|
|
||||||
console.log("event", event);
|
// "change" events (file content modified) are already handled by
|
||||||
|
// esbuild's internal ctx.watch(). Only "rename" (create or delete)
|
||||||
|
// requires a full rebuild because entry points have changed.
|
||||||
|
if (event !== "rename") return;
|
||||||
|
|
||||||
if (global.RECOMPILING) return;
|
if (global.RECOMPILING) return;
|
||||||
|
|
||||||
|
const fullPath = path.join(PAGES_DIR, filename);
|
||||||
|
const action = existsSync(fullPath) ? "created" : "deleted";
|
||||||
|
|
||||||
clearTimeout(global.WATCHER_TIMEOUT);
|
clearTimeout(global.WATCHER_TIMEOUT);
|
||||||
global.WATCHER_TIMEOUT = setTimeout(async () => {
|
global.WATCHER_TIMEOUT = setTimeout(async () => {
|
||||||
try {
|
try {
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
|
|
||||||
console.log(`File Changed. Rebuilding ...`);
|
console.log(`Page ${action}: ${filename}. Rebuilding ...`);
|
||||||
|
|
||||||
// await allPagesBundler();
|
await global.BUNDLER_CTX?.dispose();
|
||||||
|
global.BUNDLER_CTX = undefined;
|
||||||
|
|
||||||
// global.LAST_BUILD_TIME = Date.now();
|
await allPagesBundler({
|
||||||
|
watch: true,
|
||||||
// for (const controller of global.HMR_CONTROLLERS) {
|
post_build_fn: serverPostBuildFn,
|
||||||
// try {
|
});
|
||||||
// controller.enqueue(
|
|
||||||
// `event: update\ndata: ${global.LAST_BUILD_TIME}\n\n`,
|
|
||||||
// );
|
|
||||||
// } catch {
|
|
||||||
// global.HMR_CONTROLLERS.delete(controller);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(error);
|
console.error(error);
|
||||||
} finally {
|
} finally {
|
||||||
global.RECOMPILING = false;
|
global.RECOMPILING = false;
|
||||||
}
|
}
|
||||||
}, 150);
|
}, 150);
|
||||||
|
|
||||||
// if (filename.match(/\/pages\//)) {
|
|
||||||
// } else if (filename.match(/\.(js|ts|tsx|jsx)$/)) {
|
|
||||||
// clearTimeout(global.WATCHER_TIMEOUT);
|
|
||||||
// global.WATCHER_TIMEOUT = setTimeout(() => reloadServer(), 150);
|
|
||||||
// }
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -25,13 +25,19 @@ export default async function grabPageComponent({
|
|||||||
const url = req?.url ? new URL(req.url) : undefined;
|
const url = req?.url ? new URL(req.url) : undefined;
|
||||||
const router = grabRouter();
|
const router = grabRouter();
|
||||||
|
|
||||||
const { BUNX_ROOT_500_PRESET_COMPONENT, BUNX_ROOT_404_PRESET_COMPONENT, PAGES_DIR } = grabDirNames();
|
const {
|
||||||
|
BUNX_ROOT_500_PRESET_COMPONENT,
|
||||||
|
BUNX_ROOT_404_PRESET_COMPONENT,
|
||||||
|
PAGES_DIR,
|
||||||
|
} = grabDirNames();
|
||||||
|
|
||||||
const routeParams = req ? await grabRouteParams({ req }) : undefined;
|
const routeParams = req ? await grabRouteParams({ req }) : undefined;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const match = url ? router.match(url.pathname) : undefined;
|
const match = url ? router.match(url.pathname) : undefined;
|
||||||
|
|
||||||
|
console.log("match", match);
|
||||||
|
|
||||||
if (!match?.filePath && url?.pathname) {
|
if (!match?.filePath && url?.pathname) {
|
||||||
throw new NotFoundError(`Page ${url.pathname} not found`);
|
throw new NotFoundError(`Page ${url.pathname} not found`);
|
||||||
}
|
}
|
||||||
@ -163,7 +169,9 @@ export default async function grabPageComponent({
|
|||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<span>{is404 ? "404 Not Found" : "500 Internal Server Error"}</span>
|
<span>
|
||||||
|
{is404 ? "404 Not Found" : "500 Internal Server Error"}
|
||||||
|
</span>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
import grabDirNames from "../../../utils/grab-dir-names";
|
import grabDirNames from "../../../utils/grab-dir-names";
|
||||||
import type { BundlerCTXMap, PageDistGenParams } from "../../../types";
|
import type { BundlerCTXMap, PageDistGenParams } from "../../../types";
|
||||||
import grabConstants from "../../../utils/grab-constants";
|
|
||||||
|
|
||||||
const { BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
const { BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
||||||
|
|
||||||
@ -9,9 +8,6 @@ type Params = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export default async function ({ bundledMap }: Params) {
|
export default async function ({ bundledMap }: Params) {
|
||||||
const { ClientRootElementIDName, ClientRootComponentWindowName } =
|
|
||||||
await grabConstants();
|
|
||||||
|
|
||||||
let script = "";
|
let script = "";
|
||||||
|
|
||||||
// script += `import React from "react";\n`;
|
// script += `import React from "react";\n`;
|
||||||
|
|||||||
@ -215,5 +215,5 @@ export type BundlerCTXMap = {
|
|||||||
export type GlobalHMRControllerObject = {
|
export type GlobalHMRControllerObject = {
|
||||||
controller: ReadableStreamDefaultController<string>;
|
controller: ReadableStreamDefaultController<string>;
|
||||||
page_url: string;
|
page_url: string;
|
||||||
target_map: BundlerCTXMap;
|
target_map?: BundlerCTXMap;
|
||||||
};
|
};
|
||||||
|
|||||||
12
src/utils/refresh-router.ts
Normal file
12
src/utils/refresh-router.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import grabDirNames from "./grab-dir-names";
|
||||||
|
|
||||||
|
export default function refreshRouter() {
|
||||||
|
const { PAGES_DIR } = grabDirNames();
|
||||||
|
|
||||||
|
const router = new Bun.FileSystemRouter({
|
||||||
|
style: "nextjs",
|
||||||
|
dir: PAGES_DIR,
|
||||||
|
});
|
||||||
|
|
||||||
|
global.ROUTER = router;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user