Skip to content

Commit

Permalink
Use REACT_APP_ENDPOINT for BASE_URL env in vite
Browse files Browse the repository at this point in the history
  • Loading branch information
tynanford committed Nov 8, 2023
1 parent 3d7294b commit 8747317
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ REACT_APP_WEBSITE_NAME=PV Info
REACT_APP_WEBSITE_DESC=EPICS Controls PV Info
REACT_APP_VERSION=$npm_package_version

# In PVInfo version 2.0.0, this env variable is used instead of the full PUBLIC_URL env
# It is no longer required to change the package.json homepage variable or pass PUBLIC_URL during npm run build
# See the definition of "base" in vite.config.js for more details
REACT_APP_ENDPOINT=/pvinfo
REACT_APP_DOMAIN=localhost
REACT_APP_HTTP_PROTOCOL=http
Expand Down
6 changes: 4 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineConfig } from "vite";
import { defineConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import envCompatible from "vite-plugin-env-compatible";
import svgrPlugin from "vite-plugin-svgr";
Expand All @@ -8,7 +8,7 @@ import * as child from "child_process";
// https://vitejs.dev/config/
// https://stackoverflow.com/questions/71162040/how-to-insert-git-info-in-environment-variables-using-vite
// https://stackoverflow.com/questions/70436753/how-to-add-commit-hash-into-reactjs-vite-config-js
export default defineConfig(() => {
export default defineConfig(({ command, mode }) => {
const commitHash = child.execSync("git rev-parse --short HEAD").toString().trimEnd();
const commitDate = child.execSync("git log -1 --format='%ad' --date=short --date=format:'%m/%d/%Y'").toString().trimEnd();
const errString = "fatal: not a git repository (or any of the parent directories): .git"
Expand All @@ -21,6 +21,7 @@ export default defineConfig(() => {
process.env.REACT_APP_GIT_SHORT_HASH = errStringShort;
process.env.REACT_APP_GIT_COMMIT_DATE = errStringShort;
}
const env = loadEnv(mode, process.cwd(), '')

return {
envPrefix: 'REACT_APP_',
Expand All @@ -29,6 +30,7 @@ export default defineConfig(() => {
build: {
outDir: "build",
},
base: env.REACT_APP_ENDPOINT,
plugins: [
react(),
envCompatible(),
Expand Down

0 comments on commit 8747317

Please sign in to comment.