Error handling bugfix in dev server
This commit is contained in:
parent
1d0ac4aa80
commit
e3a0f5fbeb
11
dist/commands/dev/dev-spawn.js
vendored
11
dist/commands/dev/dev-spawn.js
vendored
@ -4,11 +4,22 @@ import bunextInit from "../../functions/bunext-init";
|
|||||||
import grabDirNames from "../../utils/grab-dir-names";
|
import grabDirNames from "../../utils/grab-dir-names";
|
||||||
import { rmSync } from "fs";
|
import { rmSync } from "fs";
|
||||||
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
|
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
|
||||||
|
process.on("uncaughtException", (error) => {
|
||||||
|
log.error(`Uncaught exception: ${error}`);
|
||||||
|
});
|
||||||
|
process.on("unhandledRejection", (reason) => {
|
||||||
|
log.error(`Unhandled rejection: ${reason}`);
|
||||||
|
});
|
||||||
log.info("Running development server ...");
|
log.info("Running development server ...");
|
||||||
try {
|
try {
|
||||||
rmSync(HYDRATION_DST_DIR, { recursive: true });
|
rmSync(HYDRATION_DST_DIR, { recursive: true });
|
||||||
rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
|
rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
|
||||||
}
|
}
|
||||||
catch (error) { }
|
catch (error) { }
|
||||||
|
try {
|
||||||
await bunextInit();
|
await bunextInit();
|
||||||
await startServer();
|
await startServer();
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
log.error(`Failed to start development server: ${error}`);
|
||||||
|
}
|
||||||
|
|||||||
7
dist/functions/bundler/pages-ssr-bundler.js
vendored
7
dist/functions/bundler/pages-ssr-bundler.js
vendored
@ -48,6 +48,7 @@ export default async function pagesSSRBundler(params) {
|
|||||||
writeFileSync(path.join(BUNX_TMP_DIR, "ssr-entrypoints.json"), JSON.stringify(entryPoints, null, 4));
|
writeFileSync(path.join(BUNX_TMP_DIR, "ssr-entrypoints.json"), JSON.stringify(entryPoints, null, 4));
|
||||||
}
|
}
|
||||||
catch (error) { }
|
catch (error) { }
|
||||||
|
try {
|
||||||
await esbuild.build({
|
await esbuild.build({
|
||||||
entryPoints,
|
entryPoints,
|
||||||
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||||
@ -83,6 +84,10 @@ export default async function pagesSSRBundler(params) {
|
|||||||
...(config.ssr_compiler_excludes || []),
|
...(config.ssr_compiler_excludes || []),
|
||||||
],
|
],
|
||||||
splitting: true,
|
splitting: true,
|
||||||
// logLevel: "silent",
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
catch (error) {
|
||||||
|
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||||
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -56,7 +56,12 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
|||||||
_.merge(global.BUNDLER_CTX_MAP[artifact.local_path], artifact);
|
_.merge(global.BUNDLER_CTX_MAP[artifact.local_path], artifact);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
post_build_fn?.({ artifacts });
|
try {
|
||||||
|
await post_build_fn?.({ artifacts });
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
log.error(`Post-build Error: ${error}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const elapsed = (performance.now() - build_start).toFixed(0);
|
const elapsed = (performance.now() - build_start).toFixed(0);
|
||||||
log.success(`[Built] in ${elapsed}ms`);
|
log.success(`[Built] in ${elapsed}ms`);
|
||||||
@ -82,7 +87,7 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
try {
|
try {
|
||||||
pagesSSRBundler();
|
await pagesSSRBundler();
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
log.error(`SSR Bundler Error: ${error}`);
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
|||||||
@ -23,9 +23,15 @@ export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
|||||||
build.onEnd(async (result) => {
|
build.onEnd(async (result) => {
|
||||||
if (result.errors.length > 0) {
|
if (result.errors.length > 0) {
|
||||||
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||||
|
try {
|
||||||
await global.SSR_BUNDLER_CTX?.dispose();
|
await global.SSR_BUNDLER_CTX?.dispose();
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
global.SSR_BUNDLER_CTX = undefined;
|
||||||
build_starts = 0;
|
build_starts = 0;
|
||||||
console.log("SSR Build errors:", result.errors);
|
for (const err of result.errors) {
|
||||||
|
console.error(`SSR Build error: ${err.text}${err.location ? ` (${err.location.file}:${err.location.line}:${err.location.column})` : ""}`);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const artifacts = grabArtifactsFromBundledResults({
|
const artifacts = grabArtifactsFromBundledResults({
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
import chokidar from "chokidar";
|
import chokidar from "chokidar";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { existsSync, statSync } from "fs";
|
import { existsSync } from "fs";
|
||||||
import grabDirNames from "../../utils/grab-dir-names";
|
import grabDirNames from "../../utils/grab-dir-names";
|
||||||
import fullRebuild from "./full-rebuild";
|
import fullRebuild from "./full-rebuild";
|
||||||
import { AppData } from "../../data/app-data";
|
import { AppData } from "../../data/app-data";
|
||||||
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
||||||
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
||||||
|
import { log } from "../../utils/log";
|
||||||
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
||||||
export default async function chokadirWatcherEsbuildCTX() {
|
export default async function chokadirWatcherEsbuildCTX() {
|
||||||
// Define ignored patterns directly in Chokidar for better performance
|
|
||||||
const watcher = chokidar.watch(ROOT_DIR, {
|
const watcher = chokidar.watch(ROOT_DIR, {
|
||||||
ignored: [
|
ignored: [
|
||||||
/(^|[\/\\])\../, // ignore dotfiles
|
/(^|[\/\\])\../,
|
||||||
/node_modules/,
|
/node_modules/,
|
||||||
/public/,
|
/public/,
|
||||||
/\.bunext/,
|
/\.bunext/,
|
||||||
@ -25,6 +25,8 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
depth: 99,
|
depth: 99,
|
||||||
});
|
});
|
||||||
const handleEvent = async (event, filePath) => {
|
const handleEvent = async (event, filePath) => {
|
||||||
|
let owns_recompile = false;
|
||||||
|
try {
|
||||||
const filename = path.relative(ROOT_DIR, filePath);
|
const filename = path.relative(ROOT_DIR, filePath);
|
||||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||||
await fullRebuild();
|
await fullRebuild();
|
||||||
@ -34,9 +36,12 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
await fullRebuild({ msg: `Restarting Bundler ...` });
|
await fullRebuild({ msg: `Restarting Bundler ...` });
|
||||||
}
|
}
|
||||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||||
pagesSSRBundler();
|
await pagesSSRBundler().catch((error) => {
|
||||||
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (filename.match(/\/styles$/) || filename === "styles") {
|
if (filename.match(/\/styles$/) || filename === "styles") {
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
await Bun.sleep(1000);
|
await Bun.sleep(1000);
|
||||||
await fullRebuild({
|
await fullRebuild({
|
||||||
@ -55,6 +60,7 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
if (filename.match(target_files_match)) {
|
if (filename.match(target_files_match)) {
|
||||||
if (global.RECOMPILING)
|
if (global.RECOMPILING)
|
||||||
return;
|
return;
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
if (filename.match(/.*\.server\.tsx?/)) {
|
if (filename.match(/.*\.server\.tsx?/)) {
|
||||||
global.IS_SERVER_COMPONENT = true;
|
global.IS_SERVER_COMPONENT = true;
|
||||||
@ -62,30 +68,33 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
if (global.BUNDLER_CTX) {
|
if (global.BUNDLER_CTX) {
|
||||||
await global.BUNDLER_CTX.rebuild();
|
await global.BUNDLER_CTX.rebuild();
|
||||||
}
|
}
|
||||||
// HMR for error pages
|
|
||||||
if (filename.match(/(404|500)\.tsx?/)) {
|
if (filename.match(/(404|500)\.tsx?/)) {
|
||||||
global.HMR_CONTROLLERS.forEach((controller) => {
|
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
|
||||||
|
const controller = global.HMR_CONTROLLERS[i];
|
||||||
|
try {
|
||||||
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
|
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
|
||||||
});
|
}
|
||||||
|
catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Handle Structural Changes (Add/Delete)
|
|
||||||
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
|
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
|
||||||
const is_file_of_interest = !!filename.match(target_files_match) || event.includes("Dir");
|
const is_file_of_interest = !!filename.match(target_files_match) ||
|
||||||
|
event.includes("Dir");
|
||||||
if (!is_file_of_interest)
|
if (!is_file_of_interest)
|
||||||
return;
|
return;
|
||||||
// Validation logic
|
|
||||||
if (!filename.match(/^src\/pages\/|\.css$/) ||
|
if (!filename.match(/^src\/pages\/|\.css$/) ||
|
||||||
checkExcludedPatterns({ path: filename }) ||
|
checkExcludedPatterns({ path: filename }) ||
|
||||||
filename.includes(" ")) {
|
filename.includes(" ")) {
|
||||||
// With chokidar, you rarely need to "reload" the whole watcher.
|
|
||||||
// But we keep the logic for consistency.
|
|
||||||
return reloadWatcher();
|
return reloadWatcher();
|
||||||
}
|
}
|
||||||
if (global.RECOMPILING)
|
if (global.RECOMPILING)
|
||||||
return;
|
return;
|
||||||
|
owns_recompile = true;
|
||||||
const action = event.startsWith("add") ? "created" : "deleted";
|
const action = event.startsWith("add") ? "created" : "deleted";
|
||||||
const type = filename.match(/\.css$/)
|
const type = filename.match(/\.css$/)
|
||||||
? "Stylesheet"
|
? "Stylesheet"
|
||||||
@ -98,6 +107,16 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
log.error(`Watcher rebuild failed: ${error}`);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if (owns_recompile) {
|
||||||
|
global.RECOMPILING = false;
|
||||||
|
global.IS_SERVER_COMPONENT = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
watcher
|
watcher
|
||||||
.on("add", (path) => handleEvent("add", path))
|
.on("add", (path) => handleEvent("add", path))
|
||||||
@ -105,7 +124,6 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
.on("unlink", (path) => handleEvent("unlink", path))
|
.on("unlink", (path) => handleEvent("unlink", path))
|
||||||
.on("addDir", (path) => handleEvent("addDir", path))
|
.on("addDir", (path) => handleEvent("addDir", path))
|
||||||
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
|
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
|
||||||
// global.PAGES_SRC_WATCHER = watcher;
|
|
||||||
}
|
}
|
||||||
function reloadWatcher() {
|
function reloadWatcher() {
|
||||||
if (global.PAGES_SRC_WATCHER) {
|
if (global.PAGES_SRC_WATCHER) {
|
||||||
|
|||||||
5
dist/functions/server/full-rebuild.js
vendored
5
dist/functions/server/full-rebuild.js
vendored
@ -17,7 +17,6 @@ export default async function fullRebuild(params) {
|
|||||||
global.SSR_BUNDLER_CTX = undefined;
|
global.SSR_BUNDLER_CTX = undefined;
|
||||||
}
|
}
|
||||||
catch (error) { }
|
catch (error) { }
|
||||||
// await pagesSSRBundler();
|
|
||||||
await allPagesESBuildContextBundler({
|
await allPagesESBuildContextBundler({
|
||||||
post_build_fn: () => {
|
post_build_fn: () => {
|
||||||
serverPostBuildFn();
|
serverPostBuildFn();
|
||||||
@ -25,9 +24,11 @@ export default async function fullRebuild(params) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
|
log.error(error);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
global.RECOMPILING = false;
|
global.RECOMPILING = false;
|
||||||
global.IS_SERVER_COMPONENT = false;
|
global.IS_SERVER_COMPONENT = false;
|
||||||
log.error(error);
|
|
||||||
}
|
}
|
||||||
if (global.PAGES_SRC_WATCHER) {
|
if (global.PAGES_SRC_WATCHER) {
|
||||||
global.PAGES_SRC_WATCHER.close();
|
global.PAGES_SRC_WATCHER.close();
|
||||||
|
|||||||
12
dist/functions/server/server-post-build-fn.js
vendored
12
dist/functions/server/server-post-build-fn.js
vendored
@ -22,12 +22,22 @@ export default async function serverPostBuildFn(params) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (params?.reload_all_controllers) {
|
if (params?.reload_all_controllers) {
|
||||||
|
try {
|
||||||
controller.controller.enqueue(reload_enqueue);
|
controller.controller.enqueue(reload_enqueue);
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
const target_artifact = global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||||
if (!target_artifact.local_path) {
|
if (!target_artifact?.local_path) {
|
||||||
|
try {
|
||||||
controller.controller.enqueue(reload_enqueue);
|
controller.controller.enqueue(reload_enqueue);
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
const mock_req = target_artifact.req_url
|
const mock_req = target_artifact.req_url
|
||||||
|
|||||||
26
dist/functions/server/watcher-esbuild-ctx.js
vendored
26
dist/functions/server/watcher-esbuild-ctx.js
vendored
@ -5,12 +5,15 @@ import fullRebuild from "./full-rebuild";
|
|||||||
import { AppData } from "../../data/app-data";
|
import { AppData } from "../../data/app-data";
|
||||||
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
||||||
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
||||||
|
import { log } from "../../utils/log";
|
||||||
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
||||||
export default async function watcherEsbuildCTX() {
|
export default async function watcherEsbuildCTX() {
|
||||||
const pages_src_watcher = watch(ROOT_DIR, {
|
const pages_src_watcher = watch(ROOT_DIR, {
|
||||||
recursive: true,
|
recursive: true,
|
||||||
persistent: true,
|
persistent: true,
|
||||||
}, async (event, filename) => {
|
}, async (event, filename) => {
|
||||||
|
let owns_recompile = false;
|
||||||
|
try {
|
||||||
if (!filename)
|
if (!filename)
|
||||||
return;
|
return;
|
||||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||||
@ -25,7 +28,9 @@ export default async function watcherEsbuildCTX() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||||
pagesSSRBundler();
|
await pagesSSRBundler().catch((error) => {
|
||||||
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
|
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
|
||||||
return;
|
return;
|
||||||
@ -36,6 +41,7 @@ export default async function watcherEsbuildCTX() {
|
|||||||
? statSync(full_file_path)
|
? statSync(full_file_path)
|
||||||
: undefined;
|
: undefined;
|
||||||
if (full_file_path.match(/\/styles$/)) {
|
if (full_file_path.match(/\/styles$/)) {
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
await Bun.sleep(1000);
|
await Bun.sleep(1000);
|
||||||
await fullRebuild({
|
await fullRebuild({
|
||||||
@ -53,11 +59,11 @@ export default async function watcherEsbuildCTX() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||||
// const rebuild_skip_paths = /\/pages\/api\//;
|
|
||||||
if (event !== "rename") {
|
if (event !== "rename") {
|
||||||
if (filename.match(target_files_match)) {
|
if (filename.match(target_files_match)) {
|
||||||
if (global.RECOMPILING)
|
if (global.RECOMPILING)
|
||||||
return;
|
return;
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
if (filename.match(/.*\.server\.tsx?/)) {
|
if (filename.match(/.*\.server\.tsx?/)) {
|
||||||
global.IS_SERVER_COMPONENT = true;
|
global.IS_SERVER_COMPONENT = true;
|
||||||
@ -68,8 +74,13 @@ export default async function watcherEsbuildCTX() {
|
|||||||
if (filename.match(/(404|500)\.tsx?/)) {
|
if (filename.match(/(404|500)\.tsx?/)) {
|
||||||
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
|
for (let i = global.HMR_CONTROLLERS.length - 1; i >= 0; i--) {
|
||||||
const controller = global.HMR_CONTROLLERS[i];
|
const controller = global.HMR_CONTROLLERS[i];
|
||||||
|
try {
|
||||||
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
|
controller?.controller?.enqueue(`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`);
|
||||||
}
|
}
|
||||||
|
catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@ -87,6 +98,7 @@ export default async function watcherEsbuildCTX() {
|
|||||||
return reloadWatcher();
|
return reloadWatcher();
|
||||||
if (global.RECOMPILING)
|
if (global.RECOMPILING)
|
||||||
return;
|
return;
|
||||||
|
owns_recompile = true;
|
||||||
const action = does_file_exist ? "created" : "deleted";
|
const action = does_file_exist ? "created" : "deleted";
|
||||||
const type = filename.match(/\.css$/)
|
const type = filename.match(/\.css$/)
|
||||||
? "Sylesheet"
|
? "Sylesheet"
|
||||||
@ -98,6 +110,16 @@ export default async function watcherEsbuildCTX() {
|
|||||||
await fullRebuild({
|
await fullRebuild({
|
||||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
log.error(`Watcher rebuild failed: ${error}`);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
if (owns_recompile) {
|
||||||
|
global.RECOMPILING = false;
|
||||||
|
global.IS_SERVER_COMPONENT = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
global.PAGES_SRC_WATCHER = pages_src_watcher;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,14 @@ import { rmSync } from "fs";
|
|||||||
|
|
||||||
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
|
const { HYDRATION_DST_DIR, BUNX_CWD_PAGES_REWRITE_DIR } = grabDirNames();
|
||||||
|
|
||||||
|
process.on("uncaughtException", (error) => {
|
||||||
|
log.error(`Uncaught exception: ${error}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on("unhandledRejection", (reason) => {
|
||||||
|
log.error(`Unhandled rejection: ${reason}`);
|
||||||
|
});
|
||||||
|
|
||||||
log.info("Running development server ...");
|
log.info("Running development server ...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -13,6 +21,9 @@ try {
|
|||||||
rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
|
rmSync(BUNX_CWD_PAGES_REWRITE_DIR, { recursive: true });
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
|
|
||||||
|
try {
|
||||||
await bunextInit();
|
await bunextInit();
|
||||||
|
|
||||||
await startServer();
|
await startServer();
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`Failed to start development server: ${error}`);
|
||||||
|
}
|
||||||
|
|||||||
@ -75,6 +75,7 @@ export default async function pagesSSRBundler(params?: Params) {
|
|||||||
);
|
);
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
|
|
||||||
|
try {
|
||||||
await esbuild.build({
|
await esbuild.build({
|
||||||
entryPoints,
|
entryPoints,
|
||||||
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||||
@ -112,6 +113,9 @@ export default async function pagesSSRBundler(params?: Params) {
|
|||||||
...(config.ssr_compiler_excludes || []),
|
...(config.ssr_compiler_excludes || []),
|
||||||
],
|
],
|
||||||
splitting: true,
|
splitting: true,
|
||||||
// logLevel: "silent",
|
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||||
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -98,7 +98,11 @@ export default function esbuildCTXArtifactTracker({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post_build_fn?.({ artifacts });
|
try {
|
||||||
|
await post_build_fn?.({ artifacts });
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`Post-build Error: ${error}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const elapsed = (performance.now() - build_start).toFixed(0);
|
const elapsed = (performance.now() - build_start).toFixed(0);
|
||||||
@ -131,7 +135,7 @@ export default function esbuildCTXArtifactTracker({
|
|||||||
await fullRebuild();
|
await fullRebuild();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
pagesSSRBundler();
|
await pagesSSRBundler();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.error(`SSR Bundler Error: ${error}`);
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,9 +41,16 @@ export default function ssrCTXArtifactTracker({
|
|||||||
build.onEnd(async (result) => {
|
build.onEnd(async (result) => {
|
||||||
if (result.errors.length > 0) {
|
if (result.errors.length > 0) {
|
||||||
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
global.SSR_BUNDLER_CTX_DISPOSED = true;
|
||||||
|
try {
|
||||||
await global.SSR_BUNDLER_CTX?.dispose();
|
await global.SSR_BUNDLER_CTX?.dispose();
|
||||||
|
} catch {}
|
||||||
|
global.SSR_BUNDLER_CTX = undefined;
|
||||||
build_starts = 0;
|
build_starts = 0;
|
||||||
console.log("SSR Build errors:", result.errors);
|
for (const err of result.errors) {
|
||||||
|
console.error(
|
||||||
|
`SSR Build error: ${err.text}${err.location ? ` (${err.location.file}:${err.location.line}:${err.location.column})` : ""}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,19 +1,19 @@
|
|||||||
import chokidar from "chokidar";
|
import chokidar from "chokidar";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { existsSync, statSync } from "fs";
|
import { existsSync } from "fs";
|
||||||
import grabDirNames from "../../utils/grab-dir-names";
|
import grabDirNames from "../../utils/grab-dir-names";
|
||||||
import fullRebuild from "./full-rebuild";
|
import fullRebuild from "./full-rebuild";
|
||||||
import { AppData } from "../../data/app-data";
|
import { AppData } from "../../data/app-data";
|
||||||
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
||||||
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
||||||
|
import { log } from "../../utils/log";
|
||||||
|
|
||||||
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
||||||
|
|
||||||
export default async function chokadirWatcherEsbuildCTX() {
|
export default async function chokadirWatcherEsbuildCTX() {
|
||||||
// Define ignored patterns directly in Chokidar for better performance
|
|
||||||
const watcher = chokidar.watch(ROOT_DIR, {
|
const watcher = chokidar.watch(ROOT_DIR, {
|
||||||
ignored: [
|
ignored: [
|
||||||
/(^|[\/\\])\../, // ignore dotfiles
|
/(^|[\/\\])\../,
|
||||||
/node_modules/,
|
/node_modules/,
|
||||||
/public/,
|
/public/,
|
||||||
/\.bunext/,
|
/\.bunext/,
|
||||||
@ -31,6 +31,9 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
event: "add" | "change" | "unlink" | "addDir" | "unlinkDir",
|
event: "add" | "change" | "unlink" | "addDir" | "unlinkDir",
|
||||||
filePath: string,
|
filePath: string,
|
||||||
) => {
|
) => {
|
||||||
|
let owns_recompile = false;
|
||||||
|
|
||||||
|
try {
|
||||||
const filename = path.relative(ROOT_DIR, filePath);
|
const filename = path.relative(ROOT_DIR, filePath);
|
||||||
|
|
||||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||||
@ -43,10 +46,13 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||||
pagesSSRBundler();
|
await pagesSSRBundler().catch((error) => {
|
||||||
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename.match(/\/styles$/) || filename === "styles") {
|
if (filename.match(/\/styles$/) || filename === "styles") {
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
await Bun.sleep(1000);
|
await Bun.sleep(1000);
|
||||||
await fullRebuild({
|
await fullRebuild({
|
||||||
@ -67,6 +73,7 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
if (event === "change") {
|
if (event === "change") {
|
||||||
if (filename.match(target_files_match)) {
|
if (filename.match(target_files_match)) {
|
||||||
if (global.RECOMPILING) return;
|
if (global.RECOMPILING) return;
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
|
|
||||||
if (filename.match(/.*\.server\.tsx?/)) {
|
if (filename.match(/.*\.server\.tsx?/)) {
|
||||||
@ -77,38 +84,44 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
await global.BUNDLER_CTX.rebuild();
|
await global.BUNDLER_CTX.rebuild();
|
||||||
}
|
}
|
||||||
|
|
||||||
// HMR for error pages
|
|
||||||
if (filename.match(/(404|500)\.tsx?/)) {
|
if (filename.match(/(404|500)\.tsx?/)) {
|
||||||
global.HMR_CONTROLLERS.forEach((controller) => {
|
for (
|
||||||
|
let i = global.HMR_CONTROLLERS.length - 1;
|
||||||
|
i >= 0;
|
||||||
|
i--
|
||||||
|
) {
|
||||||
|
const controller = global.HMR_CONTROLLERS[i];
|
||||||
|
try {
|
||||||
controller?.controller?.enqueue(
|
controller?.controller?.enqueue(
|
||||||
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
||||||
);
|
);
|
||||||
});
|
} catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Structural Changes (Add/Delete)
|
|
||||||
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
|
if (["add", "unlink", "addDir", "unlinkDir"].includes(event)) {
|
||||||
const is_file_of_interest =
|
const is_file_of_interest =
|
||||||
!!filename.match(target_files_match) || event.includes("Dir");
|
!!filename.match(target_files_match) ||
|
||||||
|
event.includes("Dir");
|
||||||
|
|
||||||
if (!is_file_of_interest) return;
|
if (!is_file_of_interest) return;
|
||||||
|
|
||||||
// Validation logic
|
|
||||||
if (
|
if (
|
||||||
!filename.match(/^src\/pages\/|\.css$/) ||
|
!filename.match(/^src\/pages\/|\.css$/) ||
|
||||||
checkExcludedPatterns({ path: filename }) ||
|
checkExcludedPatterns({ path: filename }) ||
|
||||||
filename.includes(" ")
|
filename.includes(" ")
|
||||||
) {
|
) {
|
||||||
// With chokidar, you rarely need to "reload" the whole watcher.
|
|
||||||
// But we keep the logic for consistency.
|
|
||||||
return reloadWatcher();
|
return reloadWatcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (global.RECOMPILING) return;
|
if (global.RECOMPILING) return;
|
||||||
|
|
||||||
|
owns_recompile = true;
|
||||||
const action = event.startsWith("add") ? "created" : "deleted";
|
const action = event.startsWith("add") ? "created" : "deleted";
|
||||||
const type = filename.match(/\.css$/)
|
const type = filename.match(/\.css$/)
|
||||||
? "Stylesheet"
|
? "Stylesheet"
|
||||||
@ -122,6 +135,14 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`Watcher rebuild failed: ${error}`);
|
||||||
|
} finally {
|
||||||
|
if (owns_recompile) {
|
||||||
|
global.RECOMPILING = false;
|
||||||
|
global.IS_SERVER_COMPONENT = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
watcher
|
watcher
|
||||||
@ -130,8 +151,6 @@ export default async function chokadirWatcherEsbuildCTX() {
|
|||||||
.on("unlink", (path) => handleEvent("unlink", path))
|
.on("unlink", (path) => handleEvent("unlink", path))
|
||||||
.on("addDir", (path) => handleEvent("addDir", path))
|
.on("addDir", (path) => handleEvent("addDir", path))
|
||||||
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
|
.on("unlinkDir", (path) => handleEvent("unlinkDir", path));
|
||||||
|
|
||||||
// global.PAGES_SRC_WATCHER = watcher;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function reloadWatcher() {
|
function reloadWatcher() {
|
||||||
|
|||||||
@ -23,17 +23,16 @@ export default async function fullRebuild(params?: { msg?: string }) {
|
|||||||
global.SSR_BUNDLER_CTX = undefined;
|
global.SSR_BUNDLER_CTX = undefined;
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
|
|
||||||
// await pagesSSRBundler();
|
|
||||||
|
|
||||||
await allPagesESBuildContextBundler({
|
await allPagesESBuildContextBundler({
|
||||||
post_build_fn: () => {
|
post_build_fn: () => {
|
||||||
serverPostBuildFn();
|
serverPostBuildFn();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
log.error(error);
|
||||||
|
} finally {
|
||||||
global.RECOMPILING = false;
|
global.RECOMPILING = false;
|
||||||
global.IS_SERVER_COMPONENT = false;
|
global.IS_SERVER_COMPONENT = false;
|
||||||
log.error(error);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (global.PAGES_SRC_WATCHER) {
|
if (global.PAGES_SRC_WATCHER) {
|
||||||
|
|||||||
@ -33,15 +33,23 @@ export default async function serverPostBuildFn(params?: Params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (params?.reload_all_controllers) {
|
if (params?.reload_all_controllers) {
|
||||||
|
try {
|
||||||
controller.controller.enqueue(reload_enqueue);
|
controller.controller.enqueue(reload_enqueue);
|
||||||
|
} catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const target_artifact =
|
const target_artifact =
|
||||||
global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
global.BUNDLER_CTX_MAP[controller.target_map.local_path];
|
||||||
|
|
||||||
if (!target_artifact.local_path) {
|
if (!target_artifact?.local_path) {
|
||||||
|
try {
|
||||||
controller.controller.enqueue(reload_enqueue);
|
controller.controller.enqueue(reload_enqueue);
|
||||||
|
} catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import fullRebuild from "./full-rebuild";
|
|||||||
import { AppData } from "../../data/app-data";
|
import { AppData } from "../../data/app-data";
|
||||||
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
import checkExcludedPatterns from "../../utils/check-excluded-patterns";
|
||||||
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
import pagesSSRBundler from "../bundler/pages-ssr-bundler";
|
||||||
|
import { log } from "../../utils/log";
|
||||||
|
|
||||||
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
const { ROOT_DIR, BUNX_BUNDLER_ERROR_EXIT_FILE } = grabDirNames();
|
||||||
|
|
||||||
@ -16,6 +17,9 @@ export default async function watcherEsbuildCTX() {
|
|||||||
persistent: true,
|
persistent: true,
|
||||||
},
|
},
|
||||||
async (event, filename) => {
|
async (event, filename) => {
|
||||||
|
let owns_recompile = false;
|
||||||
|
|
||||||
|
try {
|
||||||
if (!filename) return;
|
if (!filename) return;
|
||||||
|
|
||||||
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
if (existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE)) {
|
||||||
@ -33,7 +37,9 @@ export default async function watcherEsbuildCTX() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
if (global.SSR_BUNDLER_CTX_DISPOSED) {
|
||||||
pagesSSRBundler();
|
await pagesSSRBundler().catch((error) => {
|
||||||
|
log.error(`SSR Bundler Error: ${error}`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
|
if (filename.endsWith(AppData["BunextTmpFileExt"])) {
|
||||||
@ -47,6 +53,7 @@ export default async function watcherEsbuildCTX() {
|
|||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
if (full_file_path.match(/\/styles$/)) {
|
if (full_file_path.match(/\/styles$/)) {
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
await Bun.sleep(1000);
|
await Bun.sleep(1000);
|
||||||
await fullRebuild({
|
await fullRebuild({
|
||||||
@ -68,11 +75,11 @@ export default async function watcherEsbuildCTX() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
const target_files_match = /\.(tsx?|jsx?|css)$/;
|
||||||
// const rebuild_skip_paths = /\/pages\/api\//;
|
|
||||||
|
|
||||||
if (event !== "rename") {
|
if (event !== "rename") {
|
||||||
if (filename.match(target_files_match)) {
|
if (filename.match(target_files_match)) {
|
||||||
if (global.RECOMPILING) return;
|
if (global.RECOMPILING) return;
|
||||||
|
owns_recompile = true;
|
||||||
global.RECOMPILING = true;
|
global.RECOMPILING = true;
|
||||||
|
|
||||||
if (filename.match(/.*\.server\.tsx?/)) {
|
if (filename.match(/.*\.server\.tsx?/)) {
|
||||||
@ -90,9 +97,13 @@ export default async function watcherEsbuildCTX() {
|
|||||||
i--
|
i--
|
||||||
) {
|
) {
|
||||||
const controller = global.HMR_CONTROLLERS[i];
|
const controller = global.HMR_CONTROLLERS[i];
|
||||||
|
try {
|
||||||
controller?.controller?.enqueue(
|
controller?.controller?.enqueue(
|
||||||
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
`event: update\ndata: ${JSON.stringify({ reload: true })}\n\n`,
|
||||||
);
|
);
|
||||||
|
} catch {
|
||||||
|
global.HMR_CONTROLLERS.splice(i, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,13 +118,15 @@ export default async function watcherEsbuildCTX() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!filename.match(/^src\/pages\/|\.css$/)) return reloadWatcher();
|
if (!filename.match(/^src\/pages\/|\.css$/))
|
||||||
|
return reloadWatcher();
|
||||||
if (checkExcludedPatterns({ path: filename }))
|
if (checkExcludedPatterns({ path: filename }))
|
||||||
return reloadWatcher();
|
return reloadWatcher();
|
||||||
if (filename.match(/ /)) return reloadWatcher();
|
if (filename.match(/ /)) return reloadWatcher();
|
||||||
|
|
||||||
if (global.RECOMPILING) return;
|
if (global.RECOMPILING) return;
|
||||||
|
|
||||||
|
owns_recompile = true;
|
||||||
const action = does_file_exist ? "created" : "deleted";
|
const action = does_file_exist ? "created" : "deleted";
|
||||||
const type = filename.match(/\.css$/)
|
const type = filename.match(/\.css$/)
|
||||||
? "Sylesheet"
|
? "Sylesheet"
|
||||||
@ -126,6 +139,14 @@ export default async function watcherEsbuildCTX() {
|
|||||||
await fullRebuild({
|
await fullRebuild({
|
||||||
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
msg: `${type} ${action}: ${filename}. Rebuilding ...`,
|
||||||
});
|
});
|
||||||
|
} catch (error) {
|
||||||
|
log.error(`Watcher rebuild failed: ${error}`);
|
||||||
|
} finally {
|
||||||
|
if (owns_recompile) {
|
||||||
|
global.RECOMPILING = false;
|
||||||
|
global.IS_SERVER_COMPONENT = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user