Compare commits
2 Commits
95fcee36b2
...
cb5126c947
| Author | SHA1 | Date | |
|---|---|---|---|
| cb5126c947 | |||
| f018b228a8 |
6
dist/commands/dev/index.js
vendored
6
dist/commands/dev/index.js
vendored
@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names";
|
||||
import writeErrorFile from "../../functions/write-error-file";
|
||||
let retries = 0;
|
||||
let timeout;
|
||||
const MAX_RETRIES = 5;
|
||||
export default function () {
|
||||
return new Command("dev")
|
||||
.description("Run development server")
|
||||
@ -13,7 +14,8 @@ export default function () {
|
||||
}
|
||||
async function dev() {
|
||||
clearTimeout(timeout);
|
||||
if (retries == 1) {
|
||||
if (retries >= MAX_RETRIES) {
|
||||
console.error(`Dev server crashed ${MAX_RETRIES} times. Exiting.`);
|
||||
process.exit(1);
|
||||
}
|
||||
const dev_spawn_file = path.resolve(__dirname, "dev-spawn.ts");
|
||||
@ -32,7 +34,7 @@ async function dev() {
|
||||
retries++;
|
||||
timeout = setTimeout(() => {
|
||||
retries = 0;
|
||||
}, 5000);
|
||||
}, 10000);
|
||||
const exited = await dev_process.exited;
|
||||
if (exited) {
|
||||
return await dev();
|
||||
|
||||
@ -21,14 +21,29 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
build_starts++;
|
||||
build_start = performance.now();
|
||||
const does_error_file_exist = existsSync(BUNX_BUNDLER_ERROR_EXIT_FILE);
|
||||
if (build_starts == MAX_BUILD_STARTS &&
|
||||
if (build_starts >= MAX_BUILD_STARTS &&
|
||||
!does_error_file_exist) {
|
||||
await buildOnstartErrorHandler();
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
build_starts = 0;
|
||||
log.error(`Build errors:`);
|
||||
for (const err of result.errors) {
|
||||
log.error(` ${err.text}${err.location ? ` (${err.location.file}:${err.location.line}:${err.location.column})` : ""}`);
|
||||
}
|
||||
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`);
|
||||
}
|
||||
catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
@ -59,7 +74,12 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
fullRebuild();
|
||||
}
|
||||
else {
|
||||
pagesSSRBundler();
|
||||
try {
|
||||
pagesSSRBundler();
|
||||
}
|
||||
catch (error) {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
}
|
||||
}
|
||||
// if (global.SSR_BUNDLER_CTX) {
|
||||
// global.SSR_BUNDLER_CTX.rebuild();
|
||||
|
||||
@ -22,7 +22,9 @@ export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
||||
});
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
console.log("result.errors", result.errors);
|
||||
global.SSR_BUNDLER_CTX_DISPOSED = false;
|
||||
build_starts = 0;
|
||||
console.log("SSR Build errors:", result.errors);
|
||||
return;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
|
||||
2
dist/functions/server/full-rebuild.js
vendored
2
dist/functions/server/full-rebuild.js
vendored
@ -23,6 +23,8 @@ export default async function fullRebuild(params) {
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
log.error(error);
|
||||
}
|
||||
if (global.PAGES_SRC_WATCHER) {
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/bunext",
|
||||
"version": "1.0.85",
|
||||
"version": "1.0.86",
|
||||
"main": "dist/index.js",
|
||||
"module": "index.ts",
|
||||
"dependencies": {
|
||||
|
||||
@ -12,6 +12,7 @@ import {
|
||||
import { FileSystemRouter } from "bun";
|
||||
import { BuildContext } from "esbuild";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabConstants from "../../utils/grab-constants";
|
||||
|
||||
// Fixture lives under test/ so the framework's directory guard allows it
|
||||
const fixtureDir = path.resolve(__dirname, "../../../test/e2e-fixture");
|
||||
@ -58,6 +59,11 @@ describe("E2E Integration", () => {
|
||||
global.HMR_CONTROLLERS = [];
|
||||
global.BUNDLER_REBUILDS = 0;
|
||||
global.PAGE_FILES = [];
|
||||
global.CONSTANTS = grabConstants();
|
||||
global.SSR_BUNDLER_CTX_MAP = {};
|
||||
global.BUNDLER_CTX_MAP = {};
|
||||
global.REACT_DOM_MODULE_CACHE = new Map<string, any>();
|
||||
global.REBUILD_RETRIES = 0;
|
||||
|
||||
// Set up router pointing at the fixture's pages directory
|
||||
global.ROUTER = new Bun.FileSystemRouter({
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { describe, expect, test, mock, afterAll } from "bun:test";
|
||||
import { describe, expect, test, mock, afterAll, beforeEach } from "bun:test";
|
||||
import bunextRequestHandler from "../../../../src/functions/server/bunext-req-handler";
|
||||
|
||||
mock.module("../../../../src/utils/is-development", () => ({
|
||||
@ -34,11 +34,21 @@ mock.module("../../../../src/functions/server/web-pages/handle-web-pages", () =>
|
||||
default: async () => new Response("web-pages")
|
||||
}));
|
||||
|
||||
/**
|
||||
* Tests for the `bunext-req-handler` module.
|
||||
* Ensures that requests are correctly routed to the proper subsystem.
|
||||
*/
|
||||
describe("bunext-req-handler", () => {
|
||||
beforeEach(() => {
|
||||
global.CONSTANTS = {
|
||||
config: {
|
||||
middleware: async ({ url }: any) => {
|
||||
if (url.pathname === "/blocked") {
|
||||
return new Response("Blocked by middleware", { status: 403 });
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
RouteIgnorePatterns: [],
|
||||
} as any;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
@ -1,24 +1,26 @@
|
||||
import { afterAll, afterEach, describe, expect, test } from "bun:test";
|
||||
import fs from "fs";
|
||||
import { afterAll, afterEach, describe, expect, test, mock } from "bun:test";
|
||||
import path from "path";
|
||||
import { renderToString } from "react-dom/server";
|
||||
import React, { createContext, useContext } from "react";
|
||||
import grabPageBundledReactComponent from "../../../../src/functions/server/web-pages/grab-page-bundled-react-component";
|
||||
import grabDirNames from "../../../../src/utils/grab-dir-names";
|
||||
|
||||
const { BUNX_CWD_MODULE_CACHE_DIR, BUNX_TMP_DIR } = grabDirNames();
|
||||
const { BUNX_TMP_DIR } = grabDirNames();
|
||||
|
||||
describe("grabPageBundledReactComponent", () => {
|
||||
const fixtureDirs: string[] = [];
|
||||
const originalConfig = global.CONFIG;
|
||||
const fixtureDirs: string[] = [];
|
||||
|
||||
global.CONFIG = { development: true } as any;
|
||||
|
||||
afterEach(() => {
|
||||
for (const fixtureDir of fixtureDirs.splice(0)) {
|
||||
const fs = require("fs");
|
||||
fs.rmSync(fixtureDir, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
global.CONFIG = { development: true } as any;
|
||||
mock.restore();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
@ -26,50 +28,36 @@ describe("grabPageBundledReactComponent", () => {
|
||||
});
|
||||
|
||||
test("keeps __root context connected during SSR", async () => {
|
||||
fs.mkdirSync(BUNX_CWD_MODULE_CACHE_DIR, { recursive: true });
|
||||
fs.mkdirSync(BUNX_TMP_DIR, { recursive: true });
|
||||
|
||||
const fixtureDir = path.join(BUNX_TMP_DIR, `ssr-context-${Date.now()}`);
|
||||
fixtureDirs.push(fixtureDir);
|
||||
fs.mkdirSync(fixtureDir, { recursive: true });
|
||||
|
||||
const rootFilePath = path.join(fixtureDir, "__root.tsx");
|
||||
const pageFilePath = path.join(fixtureDir, "page.tsx");
|
||||
|
||||
fs.writeFileSync(
|
||||
rootFilePath,
|
||||
`import { createContext } from "react";
|
||||
export const AppContext = createContext("missing-context");
|
||||
mock.module("../../../../src/functions/server/web-pages/grab-root-file-path", () => ({
|
||||
default: () => ({ root_file_path: rootFilePath })
|
||||
}));
|
||||
|
||||
export default function Root({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<AppContext.Provider value="server-context">
|
||||
{children}
|
||||
</AppContext.Provider>
|
||||
);
|
||||
}
|
||||
`,
|
||||
);
|
||||
|
||||
fs.writeFileSync(
|
||||
pageFilePath,
|
||||
`import { useContext } from "react";
|
||||
import { AppContext } from "./__root";
|
||||
|
||||
export default function Page() {
|
||||
const value = useContext(AppContext);
|
||||
|
||||
return <div>{value}</div>;
|
||||
}
|
||||
`,
|
||||
);
|
||||
mock.module("../../../../src/functions/server/web-pages/grab-tsx-string-module", () => ({
|
||||
default: async () => {
|
||||
const AppContext = createContext("missing-context");
|
||||
const Root = ({ children }: { children: React.ReactNode }) =>
|
||||
React.createElement(AppContext.Provider, { value: "server-context" }, children);
|
||||
const Page = () => {
|
||||
const value = useContext(AppContext);
|
||||
return React.createElement("div", null, value);
|
||||
};
|
||||
const Main = (props: any) =>
|
||||
React.createElement(Root, props, React.createElement(Page, props));
|
||||
return { default: Main };
|
||||
}
|
||||
}));
|
||||
|
||||
const result = await grabPageBundledReactComponent({
|
||||
file_path: pageFilePath,
|
||||
root_file_path: rootFilePath,
|
||||
});
|
||||
|
||||
expect(result?.component).toBeDefined();
|
||||
expect(renderToString(result!.component)).toContain("server-context");
|
||||
const html = renderToString(React.createElement(result!.component));
|
||||
expect(html).toContain("server-context");
|
||||
});
|
||||
});
|
||||
|
||||
@ -8,6 +8,18 @@ mock.module("../../../../src/utils/log", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
mock.module("../../../../src/utils/is-development", () => ({
|
||||
default: () => false
|
||||
}));
|
||||
|
||||
mock.module("../../../../src/functions/server/full-rebuild", () => ({
|
||||
default: async () => {}
|
||||
}));
|
||||
|
||||
mock.module("../../../../src/functions/server/server-post-build-fn", () => ({
|
||||
default: async () => {}
|
||||
}));
|
||||
|
||||
import { log } from "../../../../src/utils/log";
|
||||
import grabPageComponent from "../../../../src/functions/server/web-pages/grab-page-component";
|
||||
|
||||
@ -29,7 +41,7 @@ describe("grabPageComponent", () => {
|
||||
req: new Request("http://localhost:3000/unknown-foo-bar123"),
|
||||
});
|
||||
|
||||
expect(res.serverRes?.responseOptions?.status).toBe(404);
|
||||
expect(res.serverRes?.response_options?.status).toBe(404);
|
||||
expect(log.error).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@ -29,7 +29,7 @@ mock.module("/test-path", () => ({
|
||||
|
||||
mock.module("/large-path", () => ({
|
||||
default: async () => new Response("Large OK", { status: 200 }),
|
||||
config: { maxRequestBodyMB: 1 }
|
||||
config: { max_request_body_mb: 1 }
|
||||
}));
|
||||
|
||||
/**
|
||||
|
||||
@ -6,6 +6,7 @@ import writeErrorFile from "../../functions/write-error-file";
|
||||
|
||||
let retries = 0;
|
||||
let timeout: any;
|
||||
const MAX_RETRIES = 5;
|
||||
|
||||
export default function () {
|
||||
return new Command("dev")
|
||||
@ -18,7 +19,8 @@ export default function () {
|
||||
async function dev() {
|
||||
clearTimeout(timeout);
|
||||
|
||||
if (retries == 1) {
|
||||
if (retries >= MAX_RETRIES) {
|
||||
console.error(`Dev server crashed ${MAX_RETRIES} times. Exiting.`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
@ -42,7 +44,7 @@ async function dev() {
|
||||
|
||||
timeout = setTimeout(() => {
|
||||
retries = 0;
|
||||
}, 5000);
|
||||
}, 10000);
|
||||
|
||||
const exited = await dev_process.exited;
|
||||
if (exited) {
|
||||
|
||||
@ -43,16 +43,35 @@ export default function esbuildCTXArtifactTracker({
|
||||
);
|
||||
|
||||
if (
|
||||
build_starts == MAX_BUILD_STARTS &&
|
||||
build_starts >= MAX_BUILD_STARTS &&
|
||||
!does_error_file_exist
|
||||
) {
|
||||
await buildOnstartErrorHandler();
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
build_starts = 0;
|
||||
|
||||
log.error(`Build errors:`);
|
||||
for (const err of result.errors) {
|
||||
log.error(` ${err.text}${err.location ? ` (${err.location.file}:${err.location.line}:${err.location.column})` : ""}`);
|
||||
}
|
||||
|
||||
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`,
|
||||
);
|
||||
} catch {
|
||||
global.HMR_CONTROLLERS.splice(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -98,7 +117,11 @@ export default function esbuildCTXArtifactTracker({
|
||||
cleanupLogsDirs();
|
||||
fullRebuild();
|
||||
} else {
|
||||
pagesSSRBundler();
|
||||
try {
|
||||
pagesSSRBundler();
|
||||
} catch (error) {
|
||||
log.error(`SSR Bundler Error: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
// if (global.SSR_BUNDLER_CTX) {
|
||||
|
||||
@ -40,7 +40,9 @@ export default function ssrCTXArtifactTracker({
|
||||
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
console.log("result.errors", result.errors);
|
||||
global.SSR_BUNDLER_CTX_DISPOSED = false;
|
||||
build_starts = 0;
|
||||
console.log("SSR Build errors:", result.errors);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@ -30,6 +30,8 @@ export default async function fullRebuild(params?: { msg?: string }) {
|
||||
},
|
||||
});
|
||||
} catch (error: any) {
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
log.error(error);
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user