Updates
This commit is contained in:
+3
-1
@@ -1,5 +1,7 @@
|
||||
import type { BundlerCTXMap } from "../../types";
|
||||
type Params = {
|
||||
target?: "bun" | "browser";
|
||||
page_file_paths?: string[];
|
||||
};
|
||||
export default function allPagesBunBundler(params?: Params): Promise<void>;
|
||||
export default function allPagesBunBundler(params?: Params): Promise<BundlerCTXMap[] | undefined>;
|
||||
export {};
|
||||
|
||||
+82
-26
@@ -3,45 +3,101 @@ import grabDirNames from "../../utils/grab-dir-names";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
import { log } from "../../utils/log";
|
||||
import tailwindcss from "bun-plugin-tailwind";
|
||||
const { HYDRATION_DST_DIR } = grabDirNames();
|
||||
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, BUNX_TMP_DIR } = grabDirNames();
|
||||
export default async function allPagesBunBundler(params) {
|
||||
const { target = "browser" } = 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();
|
||||
let buildStart = 0;
|
||||
buildStart = performance.now();
|
||||
const build = await Bun.build({
|
||||
entrypoints: pages.map((p) => p.transformed_path),
|
||||
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 result = await Bun.build({
|
||||
entrypoints: [...entryToPage.keys()],
|
||||
outdir: HYDRATION_DST_DIR,
|
||||
root: BUNX_HYDRATION_SRC_DIR,
|
||||
minify: true,
|
||||
format: "esm",
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
},
|
||||
naming: {
|
||||
entry: "[name]/[hash].[ext]",
|
||||
chunk: "chunks/[name]-[hash].[ext]",
|
||||
entry: "[dir]/[hash].[ext]",
|
||||
chunk: "chunks/[hash].[ext]",
|
||||
},
|
||||
plugins: [
|
||||
tailwindcss,
|
||||
{
|
||||
name: "post-build",
|
||||
setup(build) {
|
||||
build.onEnd((result) => {
|
||||
console.log("result", result);
|
||||
});
|
||||
},
|
||||
},
|
||||
],
|
||||
// plugins: [
|
||||
// ],
|
||||
plugins: [tailwindcss],
|
||||
// plugins: [tailwindcss, BunSkipNonBrowserPlugin],
|
||||
splitting: true,
|
||||
target,
|
||||
external: ["bun"],
|
||||
metafile: true,
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react-dom/client",
|
||||
"react/jsx-runtime",
|
||||
],
|
||||
});
|
||||
console.log("build", build);
|
||||
if (build.success) {
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
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}`);
|
||||
}
|
||||
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 });
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
return artifacts;
|
||||
}
|
||||
|
||||
+37
-32
@@ -7,6 +7,7 @@ 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";
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
let build_starts = 0;
|
||||
const MAX_BUILD_STARTS = 10;
|
||||
@@ -26,6 +27,9 @@ export default async function allPagesBundler(params) {
|
||||
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;
|
||||
@@ -44,10 +48,10 @@ export default async function allPagesBundler(params) {
|
||||
}));
|
||||
},
|
||||
};
|
||||
let buildStart = 0;
|
||||
const artifactTracker = {
|
||||
name: "artifact-tracker",
|
||||
setup(build) {
|
||||
let buildStart = 0;
|
||||
build.onStart(() => {
|
||||
build_starts++;
|
||||
buildStart = performance.now();
|
||||
@@ -57,38 +61,12 @@ export default async function allPagesBundler(params) {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
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;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
pages: target_pages,
|
||||
result,
|
||||
});
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
for (let i = 0; i < artifacts.length; i++) {
|
||||
const artifact = artifacts[i];
|
||||
global.BUNDLER_CTX_MAP[artifact.local_path] = artifact;
|
||||
}
|
||||
// params?.post_build_fn?.({ artifacts });
|
||||
writeFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, JSON.stringify(artifacts));
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
build_starts = 0;
|
||||
});
|
||||
// build.onEnd((result) => {
|
||||
// });
|
||||
},
|
||||
};
|
||||
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`);
|
||||
await esbuild.build({
|
||||
const result = await esbuild.build({
|
||||
entryPoints,
|
||||
outdir: HYDRATION_DST_DIR,
|
||||
bundle: true,
|
||||
@@ -99,11 +77,38 @@ export default async function allPagesBundler(params) {
|
||||
define: {
|
||||
"process.env.NODE_ENV": JSON.stringify(dev ? "development" : "production"),
|
||||
},
|
||||
entryNames: "[dir]/[name]/[hash]",
|
||||
entryNames: "[dir]/[hash]",
|
||||
metafile: true,
|
||||
plugins: [tailwindEsbuildPlugin, virtualPlugin, artifactTracker],
|
||||
jsx: "automatic",
|
||||
splitting: true,
|
||||
// splitting: true,
|
||||
// logLevel: "silent",
|
||||
external: [
|
||||
"react",
|
||||
"react-dom",
|
||||
"react-dom/client",
|
||||
"react/jsx-runtime",
|
||||
],
|
||||
});
|
||||
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({
|
||||
pages: target_pages,
|
||||
result,
|
||||
});
|
||||
if (artifacts?.[0]) {
|
||||
await recordArtifacts({ artifacts });
|
||||
}
|
||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||
log.success(`[Built] in ${elapsed}ms`);
|
||||
global.RECOMPILING = false;
|
||||
build_starts = 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
type Params = {
|
||||
post_build_fn?: (params: {
|
||||
artifacts: any[];
|
||||
}) => Promise<void> | void;
|
||||
};
|
||||
export default function allPagesESBuildContextBundler(params?: Params): Promise<void>;
|
||||
export {};
|
||||
@@ -0,0 +1,117 @@
|
||||
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";
|
||||
const { HYDRATION_DST_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = 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();
|
||||
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 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);
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
||||
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;
|
||||
}
|
||||
const artifacts = grabArtifactsFromBundledResults({
|
||||
pages,
|
||||
result,
|
||||
});
|
||||
if (artifacts?.[0] && artifacts.length > 0) {
|
||||
for (let i = 0; i < artifacts.length; i++) {
|
||||
const artifact = artifacts[i];
|
||||
if (artifact?.local_path && global.BUNDLER_CTX_MAP) {
|
||||
global.BUNDLER_CTX_MAP[artifact.local_path] =
|
||||
artifact;
|
||||
}
|
||||
}
|
||||
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`);
|
||||
global.RECOMPILING = false;
|
||||
build_starts = 0;
|
||||
});
|
||||
},
|
||||
};
|
||||
const entryPoints = Object.keys(virtualEntries).map((k) => `virtual:${k}`);
|
||||
const ctx = await esbuild.context({
|
||||
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",
|
||||
],
|
||||
});
|
||||
await ctx.rebuild();
|
||||
// global.BUNDLER_CTX = ctx;
|
||||
}
|
||||
+1
-1
@@ -11,7 +11,7 @@ export default async function grabClientHydrationScript({ page_local_path, }) {
|
||||
const root_component_path = path.join(PAGES_DIR, `${AppNames["RootPagesComponentName"]}.tsx`);
|
||||
const does_root_exist = existsSync(root_component_path);
|
||||
let txt = ``;
|
||||
txt += `import { hydrateRoot, createElement } from "react-dom/client";\n`;
|
||||
txt += `import { hydrateRoot } from "react-dom/client";\n`;
|
||||
if (does_root_exist) {
|
||||
txt += `import Root from "${root_component_path}";\n`;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
declare const BunSkipNonBrowserPlugin: Bun.BunPlugin;
|
||||
export default BunSkipNonBrowserPlugin;
|
||||
@@ -0,0 +1,32 @@
|
||||
const BunSkipNonBrowserPlugin = {
|
||||
name: "skip-non-browser",
|
||||
setup(build) {
|
||||
build.onResolve({ filter: /^(bun:|node:)/ }, (args) => {
|
||||
return { path: args.path, external: true };
|
||||
});
|
||||
build.onResolve({ filter: /^[^./]/ }, (args) => {
|
||||
// If it's a built-in like 'fs' or 'path', skip it immediately
|
||||
const excludes = [
|
||||
"fs",
|
||||
"path",
|
||||
"os",
|
||||
"crypto",
|
||||
"net",
|
||||
"events",
|
||||
"util",
|
||||
];
|
||||
if (excludes.includes(args.path) || args.path.startsWith("node:")) {
|
||||
return { path: args.path, external: true };
|
||||
}
|
||||
try {
|
||||
Bun.resolveSync(args.path, args.importer || process.cwd());
|
||||
return null;
|
||||
}
|
||||
catch (e) {
|
||||
console.warn(`[Skip] Mark as external: ${args.path}`);
|
||||
return { path: args.path, external: true };
|
||||
}
|
||||
});
|
||||
},
|
||||
};
|
||||
export default BunSkipNonBrowserPlugin;
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { BundlerCTXMap } from "../../types";
|
||||
type Params = {
|
||||
artifacts: BundlerCTXMap[];
|
||||
};
|
||||
export default function recordArtifacts({ artifacts }: Params): Promise<void>;
|
||||
export {};
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||
export default async function recordArtifacts({ artifacts }) {
|
||||
const artifacts_map = {};
|
||||
for (const artifact of artifacts) {
|
||||
if (artifact?.local_path) {
|
||||
artifacts_map[artifact.local_path] = artifact;
|
||||
}
|
||||
}
|
||||
if (global.BUNDLER_CTX_MAP) {
|
||||
global.BUNDLER_CTX_MAP = artifacts_map;
|
||||
}
|
||||
await Bun.write(HYDRATION_DST_DIR_MAP_JSON_FILE, JSON.stringify(artifacts_map, null, 4));
|
||||
}
|
||||
Reference in New Issue
Block a user