-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🧑💻 [#724] Handle env reading/processing in both Vite and CRA setups
We can detect the environment based on the presence of 'process' or not, and adapt the reading of environment variables based on the detected environment. This means that we currently have to duplicate the configuration, but as long as this is a temporary measure, we should be fine.
- Loading branch information
1 parent
ec850d5
commit f3f66d3
Showing
7 changed files
with
60 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/** | ||
* Handle the env in both (legacy) CRA and the new Vite-based setup. | ||
* | ||
* CRA is `process.env` based, using NodeJS, while ViteJS used import.meta from the | ||
* module system. | ||
*/ | ||
|
||
let env; | ||
let envVarPrefix = 'VITE'; | ||
|
||
let DEBUG = false; | ||
|
||
// Support both CRA and Vite for the time being. | ||
try { | ||
env = process.env; | ||
// it's legacy create-react-app | ||
DEBUG = env.NODE_ENV === 'development'; | ||
envVarPrefix = 'REACT_APP'; | ||
} catch (error) { | ||
if (error instanceof ReferenceError) { | ||
// it's ViteJS, which doesn't know `process` | ||
env = import.meta.env; | ||
DEBUG = env.MODE === 'development'; | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
const getEnv = name => { | ||
const fullName = `${envVarPrefix}_${name}`; | ||
return env[fullName]; | ||
}; | ||
|
||
export {DEBUG, env, getEnv}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
import {getEnv} from 'env'; | ||
|
||
import './index.scss'; | ||
import {OpenForm} from './sdk'; | ||
|
||
// import displayComponents from './custom-display-components'; | ||
|
||
const {REACT_APP_BASE_API_URL, REACT_APP_FORM_ID, REACT_APP_USE_HASH_ROUTING} = process.env; | ||
const BASE_API_URL = getEnv('BASE_API_URL'); | ||
const FORM_ID = getEnv('FORM_ID'); | ||
const USE_HASH_ROUTING = getEnv('USE_HASH_ROUTING'); | ||
|
||
window.onload = () => { | ||
const formId = new URLSearchParams(document.location.search).get('form'); | ||
const targetNode = document.getElementById('root'); | ||
const form = new OpenForm(targetNode, { | ||
baseUrl: REACT_APP_BASE_API_URL, | ||
formId: formId || REACT_APP_FORM_ID, | ||
baseUrl: BASE_API_URL, | ||
formId: formId || FORM_ID, | ||
basePath: '/', | ||
// added for testing purposes - adding a real CSP breaks *a lot* of things of Create | ||
// React App :( | ||
CSPNonce: 'RqgbALvp8D5b3+8NuhfuKg==', | ||
// displayComponents, | ||
useHashRouting: REACT_APP_USE_HASH_ROUTING === 'true' || false, | ||
useHashRouting: USE_HASH_ROUTING === 'true' || false, | ||
}); | ||
form.init(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters