Skip to content

Instantly share code, notes, and snippets.

@igrek8
Last active November 23, 2019 08:36
Show Gist options
  • Save igrek8/39cb5db80ef069271b23256090168448 to your computer and use it in GitHub Desktop.
Save igrek8/39cb5db80ef069271b23256090168448 to your computer and use it in GitHub Desktop.
Inject docker envrionments in create-react-app

Run the script in docker runtime

chmod +x webenv.sh
# load global envrionments with filter
(env | grep ^REACT_APP_ | webenv.sh) > env.js

Include <script> in index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script scr="./env.js"></script>
</head>
<body>
  
</body>
</html>
#!/usr/bin/env sh
var=${1:-env}
printf "!function(e){"
DONE=false
until $DONE; do
read || DONE=true
if [ ! -z "$REPLY" ]; then
if printf '%s\n' "$REPLY" | grep -q -e '='; then
key=$(printf '%s\n' "$REPLY" | sed -e 's/=.*//')
val=$(printf '%s\n' "$REPLY" | sed -e 's/^[^=]*=//')
fi
val=$(printf '%s' "$val" | sed 's/"/\\\\/g')
printf "e.%s=\"%s\"," "$key" "$val"
fi
done < /dev/stdin
printf "0}(window.%s=window.%s||{});" "$var" "$var"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment