Open webbushka opened 5 years ago
How do You send environment variable using rescripts?
just create a .env
file and they should get picked up, that's how it works with react-scripts.
I love the OS community –– thanks for covering this issue @webbushka 👍
Indeed! I believe this issue may be closed. 😄
has something changed to pick up that var?
Yes, the var does get picked up. 👍
I believe this issue can be closed. Looking back at your first message, is it possible you had accidentally defined the URL to PUBLIC_ENV
instead of PUBLIC_URL
?
Here are steps to demonstrate proper behavior:
# create an initial build from create-react-app
npx create-react-app my-app
cd my-app
echo "PUBLIC_URL=https://example.com" > .env
sed -i '' 's/\/build/# \/build/g' .gitignore
npm run build
git add .
git commit -a -m "npm run build"
# create a second build using rescripts
sed -i '' 's/ }/ },"rescripts": []/' package.json
sed -i '' 's/react-scripts /rescripts /g' package.json
npm install @rescripts/cli
npm run build
git add .
git commit -a -m "rescripts"
If you compare those last two commits, there will be no additional changes to the build
directory, because the PUBLIC_URL
var was properly read. To be extra sure, we can update that var:
# create a third build after updating the PUBLIC_URL
echo "PUBLIC_URL=https://cssdb.org" > .env
nom run build
git add .
git commit -a -m "PUBLIC_URL is cssdb.org"
Now, if you compare those last two commits, there will be several changes to the build
directory, and all of the URLs will be updated from example.com
to cssdb.org
.
Wait. Wait. Wait. Once I add "env", it does break. 😢
Adding a babel configuration breaks it as well. Seeing if it is limited to certain plugins.
It seems any plugin can break this functionality. I tried adding rescript-use-postcss-config
since it would not touch anything related to babel. Puzzling.
is it possible you had accidentally defined the URL to
PUBLIC_ENV
instead ofPUBLIC_URL
?
My bad, I updated the original comment. But yes I am using PUBLIC_URL
Adding any rescript plugin seems to remove the ability to define both the homepage
in package.json
or the PUBLIC_URL
in .env
. I am experiencing no workaround.
script on package.json -> "build": "node -r dotenv-flow/config 'node_modules/@rescripts/cli/bin/rescripts.js' build"
have fun, thats my dirty hack on projects using rescripts...
you need this https://www.npmjs.com/package/dotenv-flow
@jonathantneal
Any updates on this? I'm currently working on a project that has a different PUBLIC_URL
set for dev (.env.development
) and prod (.env.production
). @kamikazePT's workaround does work, but I had to explicitly set node_env
to production: node -r dotenv-flow/config node_modules/@rescripts/cli/bin/rescripts.js build --node-env=production
@goransh good catch
yeah you need to set the node-env since only inside the build script those are being hardcoded, dotenv is running before so the NODE_ENV it uses is the one set globally in your system.
In my case it was okay because I was running the build inside a docker image, where I would set the NODE_ENV value through a build-arg in CI as an env value
Description
When using the
PUBLIC_URL
field in.env
and then runningnpm run build
the build succeeds but thePUBLIC_URL
is not set. Seems that rescripts is ignoring the environmant variable. When settinghomepage
in package.jsonPUBLIC_URL
is set. This works in a base react-scripts app as well as with react-scripts-rewired. But when moving to rescripts it no longer works.@rescripts/cli@0.0.11
@rescripts/rescript-env@0.0.11
react-scripts@3.0.1
Expected Behavior
URLs using the
PUBLIC_URL
variable should be set using thePUBLIC_URL
environment variable.Current Workaround
Add the url to your package.json as
'homepage'