Comments (9)
Correct!
it's working well!
Thanks.
from rescripts.
I have created a repo in case anybody wants to see the solution
from rescripts.
Hi @saostad –– absolutely. You can install react-app-rewire-define-plugin
and use it in conjunction with @rescripts/rescript-use-rewire
:
.rescriptsrc.js
module.exports = [
[
'use-rewire',
'react-app-rewire-define-plugin',
process.env.NODE_ENV,
{
'process.env.VERSION': JSON.stringify(require('./package.json').version)
}
]
]
Although I'd recommend doing this instead:
.rescriptsrc.js
const {appendWebpackPlugin} = require('@rescripts/utilities')
const {DefinePlugin} = require('webpack')
module.exports = config =>
appendWebpackPlugin(new DefinePlugin({
'process.env.VERSION': JSON.stringify(require('./package.json').version)
}), config)
^ that should do the trick.
Please let me know if you encounter any problems. If this solves your problem, please close the issue 👍
& thanks for using Rescripts! Let me know anything I can do to make it a better developer experience for you!
from rescripts.
thanks for quick response.
how can I check if I am in prod or dev environment?
I need something like this:
const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");
require("dotenv").config();
// if in dev (npm start script)
const API_URL = process.env.API_DEV_URL
// if in prod (npm run build script)
const API_URL = process.env.API_PROD_URL
module.exports = config =>
appendWebpackPlugin(
new DefinePlugin({
"process.env.API_URL": JSON.stringify(API_URL)
}),
config
);
from rescripts.
@saostad it depends where you need the reference. I don't think you'd need the dotenv
dependency. You should be able to reference env variables directly from process.env
. My understanding is that DefinePlugin
is meant to make variables globally accessible to the app after it has been built. hope this has been helpful!
from rescripts.
what I wanna do is just run build or start command but because I have 2 different API url for dev and prod environments I wanna define one global variable (API_URL) and in build time set it based on the environment.
technically if I run
npm start
set API_URL to http://dev.mydomain.com
and
npm run build
set API_URL to http://prod.mydomain.com
and in my react app just use process.env.API_URL
from rescripts.
@saostad then you'll probably want to do something similar to what you had above:
const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");
const { NODE_ENV, API_PROD_URL, API_DEV_URL } = process.env
const API_URL = NODE_ENV === 'production' ? API_PROD_URL : API_DEV_URL
module.exports = config =>
appendWebpackPlugin(
new DefinePlugin({ API_URL }),
config,
);
Also, @rescripts/utilities
has built-in support for currying, so you could do this instead:
const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");
const { NODE_ENV, API_PROD_URL, API_DEV_URL } = process.env
const API_URL = NODE_ENV === 'production' ? API_PROD_URL : API_DEV_URL
module.exports = appendWebpackPlugin(
new DefinePlugin({ API_URL }),
);
Please let me know if this was helpful (and if so, please close the issue) 👍
from rescripts.
this is the only way it works for me:
const { appendWebpackPlugin } = require("@rescripts/utilities");
const { DefinePlugin } = require("webpack");
require("dotenv").config();
const { NODE_ENV, API_PROD_URL, API_DEV_URL } = process.env;
const API_URL = NODE_ENV === "production" ? API_PROD_URL : API_DEV_URL;
module.exports = config =>
appendWebpackPlugin(
new DefinePlugin({
"process.env.API_URL": JSON.stringify(API_URL)
}),
config
);
for some reason I still need to do require("dotenv").config();
thank you so much.
from rescripts.
@saostad ohhh––you're passing the environment vars through a .env file, not as script params... in that case, using dotenv
should do the trick. Please let me know if you have any other problems :)
from rescripts.
Related Issues (20)
- Compatibility with custom react-scripts forks HOT 1
- Show error output for rescript config errors HOT 3
- Global configuration not taken into account HOT 1
- Dev server hangs when trying to add another entry point HOT 4
- react-dev-utils need to be updated HOT 1
- .babelrc.js export function does not work HOT 3
- Adding ".babelrc" breaks typescript support in react HOT 1
- Allow react-scripts v4 without npm warning
- Typescript update to 4.X? HOT 1
- Install the last version of react-dev-utils to fix a vulnerability HOT 2
- No matching version for @rescripts/rescript-use-eslint-config dependency in @rescripts/rescript-env HOT 7
- How do I extend the jest config? HOT 1
- Setting a static name for the files generated by build HOT 2
- Webpack config HOT 4
- how to config webpack devServer' before(app) HOT 6
- PostCSS config not working HOT 1
- [Question] Multiple entry points + shared/common chunks howto example for noobs HOT 1
- Is there any way how to easily change paths? HOT 3
- How to add a devserver before or after hook? HOT 1
- How to get React Fast Refresh to work HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rescripts.