Skip to content

Commit

Permalink
Manually define process.env vars needed on client
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsoniv committed Dec 18, 2023
1 parent 773e476 commit 526e235
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basic Application</title>
<title>Application Name</title>
</head>
<body>
<div id="root"></div>
Expand Down
10 changes: 6 additions & 4 deletions src/features/domains/ordercloud/constants.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
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
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,
Expand Down
46 changes: 30 additions & 16 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -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,
}
};
});
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,
};
});

0 comments on commit 526e235

Please sign in to comment.