Fix API bundler. Use new esbuild context builder for it.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export default function apiRoutesBundler(): Promise<void>;
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import tailwindcss from "bun-plugin-tailwind";
|
||||
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
|
||||
export default async function apiRoutesBundler() {
|
||||
const api_routes = grabAllPages({ api_only: true });
|
||||
const dev = isDevelopment();
|
||||
try {
|
||||
const build = await Bun.build({
|
||||
entrypoints: api_routes.map((r) => r.local_path),
|
||||
target: "bun",
|
||||
format: "esm",
|
||||
jsx: {
|
||||
runtime: "automatic",
|
||||
development: dev,
|
||||
},
|
||||
minify: !dev,
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
},
|
||||
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||
plugins: [tailwindcss],
|
||||
naming: {
|
||||
entry: "api/[dir]/[name].[ext]",
|
||||
chunk: "api/[dir]/chunks/[hash].[ext]",
|
||||
},
|
||||
// external: [
|
||||
// "react",
|
||||
// "react-dom",
|
||||
// "react-dom/client",
|
||||
// "react/jsx-runtime",
|
||||
// ],
|
||||
splitting: true,
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`API paths build ERROR:`, error);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export default function apiRoutesContextBundler(): Promise<void>;
|
||||
@@ -0,0 +1,42 @@
|
||||
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 apiRoutesCTXArtifactTracker from "./plugins/api-routes-ctx-artifact-tracker";
|
||||
const { BUNX_CWD_MODULE_CACHE_DIR } = grabDirNames();
|
||||
export default async function apiRoutesContextBundler() {
|
||||
const pages = grabAllPages({ api_only: true });
|
||||
const dev = isDevelopment();
|
||||
if (global.API_ROUTES_BUNDLER_CTX) {
|
||||
await global.API_ROUTES_BUNDLER_CTX.dispose();
|
||||
global.API_ROUTES_BUNDLER_CTX = undefined;
|
||||
}
|
||||
global.API_ROUTES_BUNDLER_CTX = await esbuild.context({
|
||||
entryPoints: pages.map((p) => p.local_path),
|
||||
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||
bundle: true,
|
||||
minify: !dev,
|
||||
format: "esm",
|
||||
target: "esnext",
|
||||
platform: "node",
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
},
|
||||
entryNames: "api/[dir]/[hash]",
|
||||
metafile: true,
|
||||
plugins: [
|
||||
tailwindEsbuildPlugin,
|
||||
apiRoutesCTXArtifactTracker({ pages }),
|
||||
],
|
||||
jsx: "automatic",
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react/jsx-runtime",
|
||||
"react/jsx-dev-runtime",
|
||||
"bun:*",
|
||||
],
|
||||
});
|
||||
await global.API_ROUTES_BUNDLER_CTX.rebuild();
|
||||
}
|
||||
+2
-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();
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
const dev = isDevelopment();
|
||||
if (global.SSR_BUNDLER_CTX) {
|
||||
await global.SSR_BUNDLER_CTX.dispose();
|
||||
@@ -38,7 +38,7 @@ export default async function pagesSSRContextBundler(params) {
|
||||
bundle: true,
|
||||
minify: !dev,
|
||||
format: "esm",
|
||||
target: "es2020",
|
||||
target: "esnext",
|
||||
platform: "node",
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { type Plugin } from "esbuild";
|
||||
import type { PageFiles } from "../../../types";
|
||||
type Params = {
|
||||
pages: PageFiles[];
|
||||
};
|
||||
export default function apiRoutesCTXArtifactTracker({ pages }: Params): Plugin;
|
||||
export {};
|
||||
@@ -0,0 +1,62 @@
|
||||
import {} from "esbuild";
|
||||
import buildOnstartErrorHandler from "../build-on-start-error-handler";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import { log } from "../../../utils/log";
|
||||
let build_start = 0;
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 2;
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
export default function apiRoutesCTXArtifactTracker({ pages }) {
|
||||
const artifactTracker = {
|
||||
name: "ssr-artifact-tracker",
|
||||
setup(build) {
|
||||
build.onStart(async () => {
|
||||
build_starts++;
|
||||
build_start = performance.now();
|
||||
if (build_starts == MAX_BUILD_STARTS) {
|
||||
await buildOnstartErrorHandler();
|
||||
}
|
||||
});
|
||||
build.onEnd((result) => {
|
||||
if (result.errors.length > 0) {
|
||||
console.log("result.errors", result.errors);
|
||||
return;
|
||||
}
|
||||
const artifacts = Object.entries(result.metafile.outputs)
|
||||
.filter(([, meta]) => meta.entryPoint)
|
||||
.map(([outputPath, meta]) => {
|
||||
const entrypoint = meta.entryPoint
|
||||
? path.join(ROOT_DIR, meta.entryPoint)
|
||||
: undefined;
|
||||
const target_page = pages.find((p) => p.local_path == entrypoint);
|
||||
if (!target_page || !meta.entryPoint) {
|
||||
return undefined;
|
||||
}
|
||||
const { file_name, local_path, url_path } = target_page;
|
||||
return {
|
||||
path: outputPath,
|
||||
hash: path.basename(outputPath, path.extname(outputPath)),
|
||||
type: "text/javascript",
|
||||
entrypoint: meta.entryPoint,
|
||||
file_name,
|
||||
local_path,
|
||||
url_path,
|
||||
};
|
||||
});
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
for (let i = 0; i < artifacts.length; i++) {
|
||||
const artifact = artifacts[i];
|
||||
if (artifact?.local_path &&
|
||||
global.API_ROUTES_BUNDLER_CTX_MAP) {
|
||||
global.API_ROUTES_BUNDLER_CTX_MAP[artifact.local_path] = artifact;
|
||||
}
|
||||
}
|
||||
}
|
||||
// const elapsed = (performance.now() - build_start).toFixed(0);
|
||||
// log.success(`API Routes [Built] in ${elapsed}ms`);
|
||||
});
|
||||
},
|
||||
};
|
||||
return artifactTracker;
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import { log } from "../../../utils/log";
|
||||
import grabArtifactsFromBundledResults from "../grab-artifacts-from-bundled-result";
|
||||
import pagesSSRContextBundler from "../pages-ssr-context-bundler";
|
||||
import buildOnstartErrorHandler from "../build-on-start-error-handler";
|
||||
import apiRoutesContextBundler from "../api-routes-context-bundler";
|
||||
let build_start = 0;
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 2;
|
||||
@@ -46,6 +47,12 @@ export default function esbuildCTXArtifactTracker({ entryToPage, post_build_fn,
|
||||
else {
|
||||
pagesSSRContextBundler();
|
||||
}
|
||||
if (global.API_ROUTES_BUNDLER_CTX) {
|
||||
global.API_ROUTES_BUNDLER_CTX.rebuild();
|
||||
}
|
||||
else {
|
||||
apiRoutesContextBundler();
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user