Skip to content

Commit

Permalink
fix: fix BUILD_CONFIG_DEFINE
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed May 9, 2023
1 parent 87c6f86 commit c5037c3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions app/utils/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as process from "process";
import process from "process";
import { once } from "@hiogawa/utils";
import { z } from "zod";
import { uninitialized } from "./misc";
Expand Down Expand Up @@ -31,7 +31,6 @@ const Z_PUBLIC_CONFIG = z.object({

// prettier-ignore
const Z_BUILD_CONFIG = z.object({
// build time constants injected via esbuild "define" options
GIT_COMMIT_REF: z.string().optional(),
});

Expand All @@ -41,7 +40,8 @@ export let publicConfig = uninitialized as z.infer<typeof Z_PUBLIC_CONFIG>;

export let buildConfig = uninitialized as z.infer<typeof Z_BUILD_CONFIG>;

export const CONFIG_SCRIPT_PLACEHOLDER = "/*@@INJECT_CONFIG_SCRIPT@@*/";
// esbuild injects BUILD_CONFIG_DEFINE on server release build
declare let BUILD_CONFIG_DEFINE: unknown;

//
// server
Expand All @@ -50,21 +50,26 @@ export const CONFIG_SCRIPT_PLACEHOLDER = "/*@@INJECT_CONFIG_SCRIPT@@*/";
export const initializeConfigServer = once(() => {
serverConfig = Z_SERVER_CONFIG.parse(process.env);
publicConfig = Z_PUBLIC_CONFIG.parse(process.env);
// esbuild injects BUILD_CONFIG_DEFINE on release build
buildConfig = Z_BUILD_CONFIG.parse(process.env.BUILD_CONFIG_DEFINE ?? {});
buildConfig = Z_BUILD_CONFIG.parse(
typeof BUILD_CONFIG_DEFINE !== "undefined" ? BUILD_CONFIG_DEFINE : {}
);
});

// pass data to client via global script
declare let __publicConfig: any;
declare let __buildConfig: any;

export const CONFIG_SCRIPT_PLACEHOLDER = "/*@@INJECT_CONFIG_SCRIPT@@*/";

export function injectConfigScript(markup: string): string {
// TODO: need to escape?
const code = `
// TODO: should escape?
return markup.replace(
CONFIG_SCRIPT_PLACEHOLDER,
`
globalThis.__publicConfig = ${JSON.stringify(publicConfig)};
globalThis.__buildConfig = ${JSON.stringify(buildConfig)};
`;
return markup.replace(CONFIG_SCRIPT_PLACEHOLDER, code);
`
);
}

//
Expand Down
2 changes: 1 addition & 1 deletion misc/build/bundle-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ esbuild.build({
],
plugins: [noSourceMapNodeModulesPlugin()],
define: {
"process.env.BUILD_CONFIG_DEFINE": JSON.stringify(BUILD_CONFIG_DEFINE),
BUILD_CONFIG_DEFINE: JSON.stringify(BUILD_CONFIG_DEFINE),
},
});

Expand Down

0 comments on commit c5037c3

Please sign in to comment.