Switch back to esbuild
This commit is contained in:
+1
-3
@@ -7,8 +7,7 @@ import path from "path";
|
||||
import grabClientHydrationScript from "./grab-client-hydration-script";
|
||||
import { mkdirSync, rmSync } from "fs";
|
||||
import recordArtifacts from "./record-artifacts";
|
||||
import BunSkipNonBrowserPlugin from "./plugins/bun-skip-browser-plugin";
|
||||
const { HYDRATION_DST_DIR, BUNX_HYDRATION_SRC_DIR, BUNX_TMP_DIR } = grabDirNames();
|
||||
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 });
|
||||
@@ -64,7 +63,6 @@ export default async function allPagesBunBundler(params) {
|
||||
"react/jsx-runtime",
|
||||
],
|
||||
});
|
||||
await Bun.write(path.join(BUNX_TMP_DIR, "bundle.json"), JSON.stringify(result, null, 4), { createPath: true });
|
||||
if (!result.success) {
|
||||
for (const entry of result.logs) {
|
||||
log.error(`[Build] ${entry.message}`);
|
||||
|
||||
+6
-7
@@ -129,13 +129,12 @@ export default async function allPagesBundler(params) {
|
||||
}
|
||||
return;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
pages: target_pages,
|
||||
result,
|
||||
});
|
||||
if (artifacts?.[0]) {
|
||||
await recordArtifacts({ artifacts });
|
||||
}
|
||||
// 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;
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
type Params = {
|
||||
post_build_fn?: (params: {
|
||||
artifacts: any[];
|
||||
}) => Promise<void> | void;
|
||||
};
|
||||
type Params = {};
|
||||
export default function allPagesESBuildContextBundler(params?: Params): Promise<void>;
|
||||
export {};
|
||||
|
||||
+19
-28
@@ -7,40 +7,25 @@ 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";
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
import path from "path";
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE, BUNX_HYDRATION_SRC_DIR, } = grabDirNames();
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 10;
|
||||
export default async function allPagesESBuildContextBundler(params) {
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
global.PAGE_FILES = pages;
|
||||
const virtualEntries = {};
|
||||
const dev = isDevelopment();
|
||||
const entryToPage = new Map();
|
||||
for (const page of pages) {
|
||||
const key = page.transformed_path;
|
||||
const txt = await grabClientHydrationScript({
|
||||
page_local_path: page.local_path,
|
||||
});
|
||||
// if (page.url_path == "/index") {
|
||||
// console.log("txt", txt);
|
||||
// }
|
||||
if (!txt)
|
||||
continue;
|
||||
virtualEntries[key] = txt;
|
||||
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);
|
||||
}
|
||||
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",
|
||||
@@ -66,8 +51,8 @@ export default async function allPagesESBuildContextBundler(params) {
|
||||
return;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
pages,
|
||||
result,
|
||||
entryToPage,
|
||||
});
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
for (let i = 0; i < artifacts.length; i++) {
|
||||
@@ -77,8 +62,11 @@ export default async function allPagesESBuildContextBundler(params) {
|
||||
artifact;
|
||||
}
|
||||
}
|
||||
params?.post_build_fn?.({ artifacts });
|
||||
writeFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, JSON.stringify(artifacts, null, 4));
|
||||
// params?.post_build_fn?.({ artifacts });
|
||||
// writeFileSync(
|
||||
// HYDRATION_DST_DIR_MAP_JSON_FILE,
|
||||
// JSON.stringify(artifacts, null, 4),
|
||||
// );
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
@@ -87,12 +75,12 @@ export default async function allPagesESBuildContextBundler(params) {
|
||||
});
|
||||
},
|
||||
};
|
||||
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`);
|
||||
const entryPoints = [...entryToPage.keys()];
|
||||
const ctx = await esbuild.context({
|
||||
entryPoints,
|
||||
outdir: HYDRATION_DST_DIR,
|
||||
bundle: true,
|
||||
minify: true,
|
||||
minify: !dev,
|
||||
format: "esm",
|
||||
target: "es2020",
|
||||
platform: "browser",
|
||||
@@ -101,7 +89,7 @@ export default async function allPagesESBuildContextBundler(params) {
|
||||
},
|
||||
entryNames: "[dir]/[hash]",
|
||||
metafile: true,
|
||||
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker],
|
||||
plugins: [tailwindEsbuildPlugin, artifactTracker],
|
||||
jsx: "automatic",
|
||||
splitting: true,
|
||||
// logLevel: "silent",
|
||||
@@ -113,5 +101,8 @@ export default async function allPagesESBuildContextBundler(params) {
|
||||
],
|
||||
});
|
||||
await ctx.rebuild();
|
||||
// global.BUNDLER_CTX = ctx;
|
||||
// if (params?.watch) {
|
||||
// await ctx.watch();
|
||||
// }
|
||||
global.BUNDLER_CTX = ctx;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import * as esbuild from "esbuild";
|
||||
import type { BundlerCTXMap, PageFiles } from "../../types";
|
||||
type Params = {
|
||||
result: esbuild.BuildResult<esbuild.BuildOptions>;
|
||||
pages: PageFiles[];
|
||||
entryToPage: Map<string, PageFiles>;
|
||||
};
|
||||
export default function grabArtifactsFromBundledResults({ result, pages, }: Params): BundlerCTXMap[] | undefined;
|
||||
export default function grabArtifactsFromBundledResults({ result, entryToPage, }: Params): BundlerCTXMap[] | undefined;
|
||||
export {};
|
||||
|
||||
@@ -1,27 +1,27 @@
|
||||
import path from "path";
|
||||
import * as esbuild from "esbuild";
|
||||
export default function grabArtifactsFromBundledResults({ result, pages, }) {
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
const { ROOT_DIR } = grabDirNames();
|
||||
export default function grabArtifactsFromBundledResults({ result, entryToPage, }) {
|
||||
if (result.errors.length > 0)
|
||||
return;
|
||||
const artifacts = Object.entries(result.metafile.outputs)
|
||||
.filter(([, meta]) => meta.entryPoint)
|
||||
.map(([outputPath, meta]) => {
|
||||
const target_page = pages.find((p) => {
|
||||
return meta.entryPoint === `virtual:${p.transformed_path}`;
|
||||
});
|
||||
const entrypoint = path.join(ROOT_DIR, meta.entryPoint || "");
|
||||
const target_page = entryToPage.get(entrypoint);
|
||||
if (!target_page || !meta.entryPoint) {
|
||||
return undefined;
|
||||
}
|
||||
const { file_name, local_path, url_path, transformed_path } = target_page;
|
||||
const cssPath = meta.cssBundle || undefined;
|
||||
return {
|
||||
path: outputPath,
|
||||
hash: path.basename(outputPath, path.extname(outputPath)),
|
||||
type: outputPath.endsWith(".css")
|
||||
? "text/css"
|
||||
: "text/javascript",
|
||||
entrypoint: meta.entryPoint,
|
||||
css_path: cssPath,
|
||||
entrypoint,
|
||||
css_path: meta.cssBundle,
|
||||
file_name,
|
||||
local_path,
|
||||
url_path,
|
||||
|
||||
Reference in New Issue
Block a user