feat: #1000 ready to support client-side only

This commit is contained in:
Yidadaa
2023-06-14 00:37:42 +08:00
parent e6b49a60c0
commit 50cd33dbb2
13 changed files with 133 additions and 36 deletions

View File

@@ -1,16 +1,3 @@
const COMMIT_ID: string = (() => {
try {
const childProcess = require("child_process");
return childProcess
.execSync('git log -1 --format="%at000" --date=unix')
.toString()
.trim();
} catch (e) {
console.error("[Build Config] No git or not from git repo.");
return "unknown";
}
})();
export const getBuildConfig = () => {
if (typeof process === "undefined") {
throw Error(
@@ -18,7 +5,23 @@ export const getBuildConfig = () => {
);
}
const COMMIT_ID: string = (() => {
try {
const childProcess = require("child_process");
return childProcess
.execSync('git log -1 --format="%at000" --date=unix')
.toString()
.trim();
} catch (e) {
console.error("[Build Config] No git or not from git repo.");
return "unknown";
}
})();
return {
commitId: COMMIT_ID,
buildMode: process.env.BUILD_MODE ?? "standalone",
};
};
export type BuildConfig = ReturnType<typeof getBuildConfig>;

27
app/config/client.ts Normal file
View File

@@ -0,0 +1,27 @@
import { BuildConfig, getBuildConfig } from "./build";
export function getClientConfig() {
if (typeof document !== "undefined") {
// client side
return JSON.parse(queryMeta("config")) as BuildConfig;
}
if (typeof process !== "undefined") {
// server side
return getBuildConfig();
}
}
function queryMeta(key: string, defaultValue?: string): string {
let ret: string;
if (document) {
const meta = document.head.querySelector(
`meta[name='${key}']`,
) as HTMLMetaElement;
ret = meta?.content ?? "";
} else {
ret = defaultValue ?? "";
}
return ret;
}

View File

@@ -10,6 +10,7 @@ declare global {
VERCEL?: string;
HIDE_USER_API_KEY?: string; // disable user's api key input
DISABLE_GPT4?: string; // allow user to use gpt-4 or not
BUILD_MODE?: "standalone" | "export";
}
}
}