diff --git a/commands/build/index.ts b/commands/build/index.ts index f4eac22..5d49cb4 100644 --- a/commands/build/index.ts +++ b/commands/build/index.ts @@ -23,6 +23,12 @@ export default function () { allPagesBundler({ exit_after_first_build: true, + // async post_build_fn({ artifacts }) { + // writeFileSync( + // HYDRATION_DST_DIR_MAP_JSON_FILE, + // JSON.stringify(artifacts), + // ); + // }, }); }); } diff --git a/src/functions/bundler/all-pages-bundler.ts b/src/functions/bundler/all-pages-bundler.ts index f8cb9d5..1c0ee12 100644 --- a/src/functions/bundler/all-pages-bundler.ts +++ b/src/functions/bundler/all-pages-bundler.ts @@ -12,7 +12,8 @@ import type { BundlerCTXMap } from "../../types"; import { execSync } from "child_process"; import grabConstants from "../../utils/grab-constants"; -const { HYDRATION_DST_DIR, PAGES_DIR } = grabDirNames(); +const { HYDRATION_DST_DIR, PAGES_DIR, HYDRATION_DST_DIR_MAP_JSON_FILE } = + grabDirNames(); const tailwindPlugin: esbuild.Plugin = { name: "tailwindcss", @@ -151,11 +152,16 @@ export default async function allPagesBundler(params?: Params) { console.timeEnd("build"); + writeFileSync( + HYDRATION_DST_DIR_MAP_JSON_FILE, + JSON.stringify(artifacts), + ); + if (params?.exit_after_first_build) { - console.log( - "global.BUNDLER_CTX_MAP", - global.BUNDLER_CTX_MAP, - ); + // console.log( + // "global.BUNDLER_CTX_MAP", + // global.BUNDLER_CTX_MAP, + // ); process.exit(); } }); diff --git a/src/functions/server/start-server.ts b/src/functions/server/start-server.ts index 4049564..7d0e82e 100644 --- a/src/functions/server/start-server.ts +++ b/src/functions/server/start-server.ts @@ -4,6 +4,12 @@ import allPagesBundler from "../bundler/all-pages-bundler"; import serverParamsGen from "./server-params-gen"; import watcher from "./watcher"; import serverPostBuildFn from "./server-post-build-fn"; +import grabDirNames from "../../utils/grab-dir-names"; +import EJSON from "../../utils/ejson"; +import { readFileSync } from "fs"; +import type { BundlerCTXMap } from "../../types"; + +const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); type Params = { dev?: boolean; @@ -21,6 +27,14 @@ export default async function startServer(params?: Params) { }); watcher(); } else { + const artifacts = EJSON.parse( + readFileSync(HYDRATION_DST_DIR_MAP_JSON_FILE, "utf-8"), + ) as BundlerCTXMap[] | undefined; + if (!artifacts?.[0]) { + console.error(`Please build first.`); + process.exit(1); + } + global.BUNDLER_CTX_MAP = artifacts; global.IS_FIRST_BUNDLE_READY = true; }