From 526e23598776d73ee042ba4528949a5cee185363 Mon Sep 17 00:00:00 2001 From: Robert Watt Date: Mon, 18 Dec 2023 15:48:46 -0600 Subject: [PATCH] Manually define process.env vars needed on client --- index.html | 2 +- src/features/domains/ordercloud/constants.ts | 10 +++-- vite.config.ts | 46 +++++++++++++------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/index.html b/index.html index de441d3..ecc98ed 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ - Basic Application + Application Name
diff --git a/src/features/domains/ordercloud/constants.ts b/src/features/domains/ordercloud/constants.ts index 86b012b..8e81e40 100644 --- a/src/features/domains/ordercloud/constants.ts +++ b/src/features/domains/ordercloud/constants.ts @@ -1,12 +1,13 @@ import { ApiRole } from "ordercloud-javascript-sdk"; //Basic auth configuration +const CONFIG_BASE = process.env.VITE_APP_CONFIG_BASE; const BASE_API_URL = process.env.VITE_APP_ORDERCLOUD_BASE_API_URL || "https://api.ordercloud.io/v1"; -const CLIENT_ID = process.env.VITE_APP_ORDERCLOUD_CLIENT_ID; -const SCOPE_STRING = process.env.VITE_APP_ORDERCLOUD_SCOPE; +const CLIENT_ID = process.env.VITE_APP_ORDERCLOUD_CLIENT_ID || ""; +const SCOPE_STRING = process.env.VITE_APP_ORDERCLOUD_SCOPE || ""; const CUSTOM_SCOPE_STRING = process.env.VITE_APP_ORDERCLOUD_CUSTOM_SCOPE; -const SCOPE:ApiRole[] = SCOPE_STRING?.split(',') as ApiRole[] || []; +const SCOPE:ApiRole[] = SCOPE_STRING.split(',') as ApiRole[]; const CUSTOM_SCOPE:string[] = CUSTOM_SCOPE_STRING?.split(',') || []; //Anonymous auth configuration @@ -14,12 +15,13 @@ const ALLOW_ANONYMOUS_STRING = process.env.VITE_APP_ORDERCLOUD_ALLOW_ANONYMOUS; const ALLOW_ANONYMOUS = ALLOW_ANONYMOUS_STRING ? ALLOW_ANONYMOUS_STRING === "true" : false; console.log('consts', { + CONFIG_BASE, BASE_API_URL, CLIENT_ID, SCOPE, CUSTOM_SCOPE, ALLOW_ANONYMOUS, -}) +}, ) export { BASE_API_URL, diff --git a/vite.config.ts b/vite.config.ts index fdbf9c6..fbb7824 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,18 +1,32 @@ -import { defineConfig, loadEnv } from 'vite'; -import react from '@vitejs/plugin-react' +import { defineConfig, loadEnv } from "vite"; +import react from "@vitejs/plugin-react"; export default defineConfig(({ mode }) => { - const env = loadEnv(mode, process.cwd(), ''); - console.log('ENV', env); - return { - base: env.VITE_APP_CONFIG_BASE, - plugins: [react()], - define: { - 'process.env.VITE_APP_CONFIG_BASE': env.VITE_APP_CONFIG_BASE, - 'process.env.VITE_APP_ORDERCLOUD_CLIENT_ID': env.VITE_APP_ORDERCLOUD_CLIENT_ID, - 'process.env.VITE_APP_ORDERCLOUD_SCOPE': env.VITE_APP_ORDERCLOUD_SCOPE, - 'process.env.VITE_APP_ORDERCLOUD_CUSTOM_SCOPE': env.VITE_APP_ORDERCLOUD_CUSTOM_SCOPE, - 'process.env.VITE_APP_ORDERCLOUD_ALLOW_ANONYMOUS': env.VITE_APP_ORDERCLOUD_ALLOW_ANONYMOUS, - } - }; -}); \ No newline at end of file + const env = loadEnv(mode, process.cwd(), ""); + console.log("ENV", env); + const definitions = { + "process.env.VITE_APP_CONFIG_BASE": JSON.stringify( + env.VITE_APP_CONFIG_BASE + ), + "process.env.VITE_APP_ORDERCLOUD_BASE_API_URL": JSON.stringify( + env.VITE_APP_ORDERCLOUD_BASE_API_URL + ), + "process.env.VITE_APP_ORDERCLOUD_CLIENT_ID": JSON.stringify( + env.VITE_APP_ORDERCLOUD_CLIENT_ID + ), + "process.env.VITE_APP_ORDERCLOUD_SCOPE": JSON.stringify( + env.VITE_APP_ORDERCLOUD_SCOPE + ), + "process.env.VITE_APP_ORDERCLOUD_CUSTOM_SCOPE": JSON.stringify( + env.VITE_APP_ORDERCLOUD_CUSTOM_SCOPE + ), + "process.env.VITE_APP_ORDERCLOUD_ALLOW_ANONYMOUS": JSON.stringify( + env.VITE_APP_ORDERCLOUD_ALLOW_ANONYMOUS + ), + }; + return { + base: env.VITE_APP_CONFIG_BASE, + plugins: [react()], + define: definitions, + }; +});