Skip to content

Commit

Permalink
(wip) build time config
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed May 9, 2023
1 parent 544716c commit a2655d5
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
9 changes: 6 additions & 3 deletions app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import type { UserTable } from "./db/models";
import { $R, R } from "./misc/routes";
import { HideRecaptchaBadge } from "./routes/users/register";
import { trpc } from "./trpc/client";
import { publicConfig } from "./utils/config";
import { CONFIG_SCRIPT_PLACEHOLDER_V2, publicConfig } from "./utils/config";
import { ConfigPlaceholder } from "./utils/config-placeholder";
import { useFlashMessages } from "./utils/flash-message-hook";
import { RootLoaderData, useRootLoaderData } from "./utils/loader-utils";
Expand Down Expand Up @@ -73,10 +73,13 @@ export default function DefaultComponent() {
</title>
<Meta />
<Links />
<ConfigPlaceholder />
{/* <ConfigPlaceholder /> */}
<ThemeScript />
<HideRecaptchaBadge />
<script dangerouslySetInnerHTML={{ __html: `` }} />
<script
suppressHydrationWarning
dangerouslySetInnerHTML={{ __html: CONFIG_SCRIPT_PLACEHOLDER_V2 }}
/>
</head>
<body className="h-full">
{/* TODO: default position="top" is fine? */}
Expand Down
31 changes: 24 additions & 7 deletions app/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,22 @@ const Z_PUBLIC_CONFIG = z.object({
VERCEL_ENV: z.string().optional(),
});

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

export let serverConfig = uninitialized as z.infer<typeof Z_SERVER_CONFIG>;

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_ID = "__configScript";

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

//
// server
Expand All @@ -44,21 +53,29 @@ export const CONFIG_SCRIPT_PLACEHOLDER = "@@__configScriptPlaceholder@@";
export const initializeConfigServer = once(() => {
serverConfig = Z_SERVER_CONFIG.parse(process.env);
publicConfig = Z_PUBLIC_CONFIG.parse(process.env);
buildConfig = Z_BUILD_CONFIG.parse(process.env.__DEFINE_BUILD_CONFIG ?? {});
});

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

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

//
// client
//

export const initializeConfigClient = once(() => {
const el = document.querySelector("#" + CONFIG_SCRIPT_ID);
tinyassert(el);
publicConfig = JSON.parse(el.innerHTML);
// const el = document.querySelector("#" + CONFIG_SCRIPT_ID);
// tinyassert(el);
publicConfig = __publicConfig;
buildConfig = __buildConfig;
});
47 changes: 28 additions & 19 deletions misc/build/bundle-vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,32 @@ import esbuild from "esbuild";
// - https://esbuild.github.io/plugins/
// - https://github.com/evanw/esbuild/issues/1685#issuecomment-944916409

esbuild.build({
logLevel: "info",
entryPoints: ["./build/remix/production/server/index.js"],
outfile: "./build/remix/production/server/index-bundled.js",
bundle: true,
sourcemap: "inline",
platform: "node",
external: [
// exclude knex drivers except "mysql2"
"mysql",
"sqlite3",
"better-sqlite3",
"tedious",
"pg",
"oracledb",
"pg-query-stream",
],
plugins: [noSourceMapNodeModulesPlugin()],
});
async function main() {
await esbuild.build({
logLevel: "info",
entryPoints: ["./build/remix/production/server/index.js"],
outfile: "./build/remix/production/server/index-bundled.js",
bundle: true,
sourcemap: "inline",
platform: "node",
external: [
// exclude knex drivers except "mysql2"
"mysql",
"sqlite3",
"better-sqlite3",
"tedious",
"pg",
"oracledb",
"pg-query-stream",
],
plugins: [noSourceMapNodeModulesPlugin()],
define: {
"process.env.__DEFINE_BUILD_CONFIG": JSON.stringify({
GIT_COMMIT_REF: `xxx`,
}),
},
});
}

// https://github.com/evanw/esbuild/issues/1685#issuecomment-944916409
function noSourceMapNodeModulesPlugin(): esbuild.Plugin {
Expand All @@ -47,3 +54,5 @@ function noSourceMapNodeModulesPlugin(): esbuild.Plugin {
},
};
}

main();

0 comments on commit a2655d5

Please sign in to comment.