Fix api route caching issue
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
import type { BundlerCTXMap } from "../../types";
|
||||
type Params = {
|
||||
target?: "bun" | "browser";
|
||||
page_file_paths?: string[];
|
||||
};
|
||||
export default function allPagesBunBundler(params?: Params): Promise<BundlerCTXMap[] | undefined>;
|
||||
export {};
|
||||
-107
@@ -1,107 +0,0 @@
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import { log } from "../../utils/log";
|
||||
import tailwindcss from "bun-plugin-tailwind";
|
||||
import path from "path";
|
||||
import grabClientHydrationScript from "./grab-client-hydration-script";
|
||||
import { mkdirSync, rmSync } from "fs";
|
||||
import recordArtifacts from "./record-artifacts";
|
||||
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
||||
export default async function allPagesBunBundler(params) {
|
||||
const { target = "browser", page_file_paths } = params || {};
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
const target_pages = page_file_paths?.[0]
|
||||
? pages.filter((p) => page_file_paths.includes(p.local_path))
|
||||
: pages;
|
||||
if (!page_file_paths) {
|
||||
global.PAGE_FILES = pages;
|
||||
try {
|
||||
rmSync(BUNX_HYDRATION_SRC_DIR, { recursive: true });
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
mkdirSync(BUNX_HYDRATION_SRC_DIR, { recursive: true });
|
||||
const dev = isDevelopment();
|
||||
const entryToPage = new Map();
|
||||
for (const page of target_pages) {
|
||||
const txt = await grabClientHydrationScript({
|
||||
page_local_path: page.local_path,
|
||||
});
|
||||
if (!txt)
|
||||
continue;
|
||||
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
|
||||
await Bun.write(entryFile, txt, { createPath: true });
|
||||
entryToPage.set(path.resolve(entryFile), page);
|
||||
}
|
||||
if (entryToPage.size === 0)
|
||||
return;
|
||||
const buildStart = performance.now();
|
||||
const define = {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
};
|
||||
const result = await Bun.build({
|
||||
entrypoints: [...entryToPage.keys()],
|
||||
outdir: HYDRATION_DST_DIR,
|
||||
root: BUNX_HYDRATION_SRC_DIR,
|
||||
minify: !dev,
|
||||
format: "esm",
|
||||
define,
|
||||
naming: {
|
||||
entry: "[dir]/[hash].[ext]",
|
||||
chunk: "chunks/[hash].[ext]",
|
||||
},
|
||||
plugins: [tailwindcss],
|
||||
// plugins: [tailwindcss, BunSkipNonBrowserPlugin],
|
||||
splitting: true,
|
||||
target,
|
||||
metafile: true,
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react-dom/client",
|
||||
"react/jsx-runtime",
|
||||
],
|
||||
});
|
||||
if (!result.success) {
|
||||
for (const entry of result.logs) {
|
||||
log.error(`[Build] ${entry.message}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
const artifacts = [];
|
||||
for (const [outputPath, outputInfo] of Object.entries(result.metafile.outputs)) {
|
||||
const entryPoint = outputInfo.entryPoint;
|
||||
const cssBundle = outputInfo.cssBundle;
|
||||
if (!entryPoint)
|
||||
continue;
|
||||
if (outputPath.match(/\.css$/))
|
||||
continue;
|
||||
const page = entryToPage.get(path.resolve(entryPoint));
|
||||
if (!page)
|
||||
continue;
|
||||
artifacts.push({
|
||||
path: path.join(".bunext/public/pages", outputPath),
|
||||
hash: path.basename(outputPath, path.extname(outputPath)),
|
||||
type: outputPath.endsWith(".css") ? "text/css" : "text/javascript",
|
||||
entrypoint: entryPoint,
|
||||
css_path: cssBundle
|
||||
? path.join(".bunext/public/pages", cssBundle)
|
||||
: undefined,
|
||||
file_name: page.file_name,
|
||||
local_path: page.local_path,
|
||||
url_path: page.url_path,
|
||||
});
|
||||
}
|
||||
if (artifacts?.[0]) {
|
||||
await recordArtifacts({
|
||||
artifacts,
|
||||
page_file_paths,
|
||||
});
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
return artifacts;
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
type Params = {
|
||||
/**
|
||||
* Locations of the pages Files.
|
||||
*/
|
||||
page_file_paths?: string[];
|
||||
};
|
||||
export default function allPagesBundler(params?: Params): Promise<void>;
|
||||
export {};
|
||||
-142
@@ -1,142 +0,0 @@
|
||||
import * as esbuild from "esbuild";
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import { log } from "../../utils/log";
|
||||
import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
|
||||
import grabClientHydrationScript from "./grab-client-hydration-script";
|
||||
import grabArtifactsFromBundledResults from "./grab-artifacts-from-bundled-result";
|
||||
import { writeFileSync } from "fs";
|
||||
import recordArtifacts from "./record-artifacts";
|
||||
import stripServerSideLogic from "./strip-server-side-logic";
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 10;
|
||||
export default async function allPagesBundler(params) {
|
||||
const { page_file_paths } = params || {};
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
const target_pages = page_file_paths?.[0]
|
||||
? pages.filter((p) => page_file_paths.includes(p.local_path))
|
||||
: pages;
|
||||
if (!page_file_paths) {
|
||||
global.PAGE_FILES = pages;
|
||||
}
|
||||
const virtualEntries = {};
|
||||
const dev = isDevelopment();
|
||||
for (const page of target_pages) {
|
||||
const key = page.local_path;
|
||||
const txt = await grabClientHydrationScript({
|
||||
page_local_path: page.local_path,
|
||||
});
|
||||
// if (page.url_path == "/index") {
|
||||
// console.log("txt", txt);
|
||||
// }
|
||||
if (!txt)
|
||||
continue;
|
||||
// const final_tsx = stripServerSideLogic({
|
||||
// txt_code: txt,
|
||||
// file_path: key,
|
||||
// });
|
||||
// console.log("final_tsx", final_tsx);
|
||||
virtualEntries[key] = txt;
|
||||
}
|
||||
const virtualPlugin = {
|
||||
name: "virtual-entrypoints",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /^virtual:/ }, (args) => ({
|
||||
path: args.path.replace("virtual:", ""),
|
||||
namespace: "virtual",
|
||||
}));
|
||||
build.onLoad({ filter: /.*/, namespace: "virtual" }, (args) => ({
|
||||
contents: virtualEntries[args.path],
|
||||
loader: "tsx",
|
||||
resolveDir: process.cwd(),
|
||||
}));
|
||||
},
|
||||
};
|
||||
let buildStart = 0;
|
||||
const artifactTracker = {
|
||||
name: "artifact-tracker",
|
||||
setup(build) {
|
||||
build.onStart(() => {
|
||||
build_starts++;
|
||||
buildStart = performance.now();
|
||||
if (build_starts == MAX_BUILD_STARTS) {
|
||||
const error_msg = `Build Failed. Please check all your components and imports.`;
|
||||
log.error(error_msg);
|
||||
}
|
||||
});
|
||||
// build.onEnd((result) => {
|
||||
// });
|
||||
},
|
||||
};
|
||||
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`);
|
||||
// let alias: any = {};
|
||||
// const excludes = [
|
||||
// "bun:sqlite",
|
||||
// "path",
|
||||
// "url",
|
||||
// "events",
|
||||
// "util",
|
||||
// "crypto",
|
||||
// "net",
|
||||
// "tls",
|
||||
// "fs",
|
||||
// "node:path",
|
||||
// "node:url",
|
||||
// "node:process",
|
||||
// "node:fs",
|
||||
// "node:timers/promises",
|
||||
// ];
|
||||
// for (let i = 0; i < excludes.length; i++) {
|
||||
// const exclude = excludes[i];
|
||||
// alias[exclude] = "./empty.js";
|
||||
// }
|
||||
// console.log("alias", alias);
|
||||
const result = await esbuild.build({
|
||||
entryPoints,
|
||||
outdir: HYDRATION_DST_DIR,
|
||||
bundle: true,
|
||||
minify: true,
|
||||
format: "esm",
|
||||
target: "es2020",
|
||||
platform: "browser",
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
},
|
||||
entryNames: "[dir]/[hash]",
|
||||
metafile: true,
|
||||
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker],
|
||||
jsx: "automatic",
|
||||
// splitting: true,
|
||||
// logLevel: "silent",
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react-dom/client",
|
||||
"react/jsx-runtime",
|
||||
],
|
||||
// alias,
|
||||
});
|
||||
if (result.errors.length > 0) {
|
||||
for (const error of result.errors) {
|
||||
const loc = error.location;
|
||||
const location = loc
|
||||
? ` ${loc.file}:${loc.line}:${loc.column}`
|
||||
: "";
|
||||
log.error(`[Build]${location} ${error.text}`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
// const artifacts = grabArtifactsFromBundledResults({
|
||||
// result,
|
||||
// });
|
||||
// if (artifacts?.[0]) {
|
||||
// await recordArtifacts({ artifacts });
|
||||
// }
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
build_starts = 0;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
type Params = {
|
||||
post_build_fn?: (params: {
|
||||
artifacts: any[];
|
||||
}) => Promise<void> | void;
|
||||
};
|
||||
export default function allPagesESBuildContextBundlerFiles(params?: Params): Promise<void>;
|
||||
export {};
|
||||
@@ -1,58 +0,0 @@
|
||||
import * as esbuild from "esbuild";
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import tailwindEsbuildPlugin from "../server/web-pages/tailwind-esbuild-plugin";
|
||||
import grabClientHydrationScript from "./grab-client-hydration-script";
|
||||
import path from "path";
|
||||
import esbuildCTXArtifactTracker from "./plugins/esbuild-ctx-artifact-tracker";
|
||||
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
||||
export default async function allPagesESBuildContextBundlerFiles(params) {
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
global.PAGE_FILES = pages;
|
||||
const dev = isDevelopment();
|
||||
const entryToPage = new Map();
|
||||
for (const page of pages) {
|
||||
const tsx = await grabClientHydrationScript({
|
||||
page_local_path: page.local_path,
|
||||
});
|
||||
if (!tsx)
|
||||
continue;
|
||||
const entryFile = path.join(BUNX_HYDRATION_SRC_DIR, `${page.url_path}.tsx`);
|
||||
await Bun.write(entryFile, tsx, { createPath: true });
|
||||
entryToPage.set(entryFile, { ...page, tsx });
|
||||
}
|
||||
const entryPoints = [...entryToPage.keys()];
|
||||
const ctx = await esbuild.context({
|
||||
entryPoints,
|
||||
outdir: HYDRATION_DST_DIR,
|
||||
bundle: true,
|
||||
minify: !dev,
|
||||
format: "esm",
|
||||
target: "es2020",
|
||||
platform: "browser",
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
},
|
||||
entryNames: "[dir]/[hash]",
|
||||
metafile: true,
|
||||
plugins: [
|
||||
tailwindEsbuildPlugin,
|
||||
esbuildCTXArtifactTracker({
|
||||
entryToPage,
|
||||
post_build_fn: params?.post_build_fn,
|
||||
}),
|
||||
],
|
||||
jsx: "automatic",
|
||||
splitting: true,
|
||||
logLevel: "silent",
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react-dom/client",
|
||||
"react/jsx-runtime",
|
||||
],
|
||||
});
|
||||
await ctx.rebuild();
|
||||
global.BUNDLER_CTX = ctx;
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
type Params = {};
|
||||
export default function buildOnstartErrorHandler(params?: Params): Promise<void>;
|
||||
export {};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { log } from "../../utils/log";
|
||||
export default async function buildOnstartErrorHandler(params) {
|
||||
// const error_msg = `Build Failed. Please check all your components and imports.`;
|
||||
// log.error(error_msg);
|
||||
global.BUNDLER_CTX_DISPOSED = true;
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
await Promise.all([
|
||||
global.SSR_BUNDLER_CTX?.dispose(),
|
||||
global.BUNDLER_CTX?.dispose(),
|
||||
]);
|
||||
global.SSR_BUNDLER_CTX = undefined;
|
||||
global.BUNDLER_CTX = undefined;
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
type Params = {
|
||||
log_time?: boolean;
|
||||
debug?: boolean;
|
||||
target_page_file?: string;
|
||||
};
|
||||
export default function initPages(params?: Params): Promise<void>;
|
||||
export {};
|
||||
Vendored
-44
@@ -1,44 +0,0 @@
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import { log } from "../../utils/log";
|
||||
import grabPageBundledReactComponent from "../server/web-pages/grab-page-bundled-react-component";
|
||||
import grabTsxStringModule from "../server/web-pages/grab-tsx-string-module";
|
||||
const {} = grabDirNames();
|
||||
export default async function initPages(params) {
|
||||
const buildStart = performance.now();
|
||||
const dev = isDevelopment();
|
||||
const pages = grabAllPages({
|
||||
exclude_api: true,
|
||||
});
|
||||
if (params?.log_time) {
|
||||
log.build(`Compiling SSR for ${pages.length} pages ...`);
|
||||
}
|
||||
const tsx_map = [];
|
||||
try {
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const page = pages[i];
|
||||
if (params?.target_page_file &&
|
||||
page.local_path !== params.target_page_file) {
|
||||
continue;
|
||||
}
|
||||
const { tsx } = (await grabPageBundledReactComponent({
|
||||
file_path: page.local_path,
|
||||
return_tsx_only: true,
|
||||
})) || {};
|
||||
if (!tsx) {
|
||||
continue;
|
||||
}
|
||||
tsx_map.push({
|
||||
tsx,
|
||||
page_file_path: page.local_path,
|
||||
});
|
||||
}
|
||||
await grabTsxStringModule({ tsx_map });
|
||||
}
|
||||
catch (error) { }
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
if (params?.log_time) {
|
||||
log.success(`[SSR Compiled] in ${elapsed}ms`);
|
||||
}
|
||||
}
|
||||
+7
-2
@@ -9,7 +9,7 @@ import ssrVirtualFilesPlugin from "./plugins/ssr-virtual-files-plugin";
|
||||
import ssrCTXArtifactTracker from "./plugins/ssr-ctx-artifact-tracker";
|
||||
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
|
||||
export default async function pagesSSRContextBundler(params) {
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
const pages = grabAllPages();
|
||||
const dev = isDevelopment();
|
||||
if (global.SSR_BUNDLER_CTX) {
|
||||
await global.SSR_BUNDLER_CTX.dispose();
|
||||
@@ -18,6 +18,11 @@ export default async function pagesSSRContextBundler(params) {
|
||||
const entryToPage = new Map();
|
||||
const { root_file_path } = grabRootFilePath();
|
||||
for (const page of pages) {
|
||||
if (page.local_path.match(/\/pages\/api\//)) {
|
||||
const ts = await Bun.file(page.local_path).text();
|
||||
entryToPage.set(page.local_path, { ...page, tsx: ts });
|
||||
continue;
|
||||
}
|
||||
const tsx = grabPageReactComponentString({
|
||||
file_path: page.local_path,
|
||||
root_file_path,
|
||||
@@ -57,7 +62,7 @@ export default async function pagesSSRContextBundler(params) {
|
||||
"react/jsx-runtime",
|
||||
"react/jsx-dev-runtime",
|
||||
],
|
||||
logLevel: "silent",
|
||||
// logLevel: "silent",
|
||||
});
|
||||
await global.SSR_BUNDLER_CTX.rebuild();
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@ import {} from "esbuild";
|
||||
import { log } from "../../../utils/log";
|
||||
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
|
||||
import pagesSSRContextBundler from "../pages-ssr-context-bundler";
|
||||
let buildStart = 0;
|
||||
import buildOnstartErrorHandler from "../build-on-start-error-handler";
|
||||
let build_start = 0;
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 2;
|
||||
export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
||||
@@ -11,49 +12,19 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
setup(build) {
|
||||
build.onStart(async () => {
|
||||
build_starts++;
|
||||
buildStart = performance.now();
|
||||
build_start = performance.now();
|
||||
if (build_starts == MAX_BUILD_STARTS) {
|
||||
const error_msg = `Build Failed. Please check all your components and imports.`;
|
||||
log.error(error_msg);
|
||||
global.BUNDLER_CTX_DISPOSED = true;
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
await global.SSR_BUNDLER_CTX?.dispose();
|
||||
global.SSR_BUNDLER_CTX = undefined;
|
||||
await global.BUNDLER_CTX?.dispose();
|
||||
global.BUNDLER_CTX = undefined;
|
||||
await buildOnstartErrorHandler();
|
||||
}
|
||||
});
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
// for (const error of result.errors) {
|
||||
// const loc = error.location;
|
||||
// const location = loc
|
||||
// ? ` ${loc.file}:${loc.line}:${loc.column}`
|
||||
// : "";
|
||||
// log.error(`[Build]${location} ${error.text}`);
|
||||
// }
|
||||
return;
|
||||
}
|
||||
// if (result.errors.length) {
|
||||
// console.error(
|
||||
// esbuild.formatMessagesSync(result.errors, {
|
||||
// kind: "error",
|
||||
// }),
|
||||
// );
|
||||
// }
|
||||
// if (result.warnings.length) {
|
||||
// console.warn(
|
||||
// esbuild.formatMessagesSync(result.warnings, {
|
||||
// kind: "warning",
|
||||
// }),
|
||||
// );
|
||||
// }
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
result,
|
||||
entryToPage,
|
||||
});
|
||||
// console.log("artifacts", artifacts);
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
for (let i = 0; i < artifacts.length; i++) {
|
||||
const artifact = artifacts[i];
|
||||
@@ -64,7 +35,7 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
}
|
||||
post_build_fn?.({ artifacts });
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
const elapsed = (performance.now() - build_start).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
global.IS_SERVER_COMPONENT = false;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {} from "esbuild";
|
||||
import { log } from "../../../utils/log";
|
||||
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
|
||||
let buildStart = 0;
|
||||
import buildOnstartErrorHandler from "../build-on-start-error-handler";
|
||||
let build_start = 0;
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 2;
|
||||
export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
||||
@@ -10,14 +10,14 @@ export default function ssrCTXArtifactTracker({ entryToPage, post_build_fn, }) {
|
||||
setup(build) {
|
||||
build.onStart(async () => {
|
||||
build_starts++;
|
||||
buildStart = performance.now();
|
||||
build_start = performance.now();
|
||||
if (build_starts == MAX_BUILD_STARTS) {
|
||||
// const error_msg = `SSR Build Failed. Please check all your components and imports.`;
|
||||
// log.error(error_msg);
|
||||
await buildOnstartErrorHandler();
|
||||
}
|
||||
});
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
console.log("result.errors", result.errors);
|
||||
return;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
|
||||
Reference in New Issue
Block a user