I have this kind of configuration like below. However, it seems to load development script even though the if statement go into only "production"
if (process.env.NODE_ENV === 'production') {
module.exports = require('./configureStoreProd')
} else {
module.exports = require('./configureStoreDev')
}
If I delete the "import logger from 'redux-logoer", it does not show on analyzer.
I am guessing when webpack building vendor file, NODE_ENV is undefine or null. How do i set it properly ?
Workaround I used:
Your config file (that you will import in the js files you wish to use it in) that determines what file you will use depending on the environment you are in.
config.js
var config = null;
if (process.env.NODE_ENV !== 'production') {
config = require('./config.development');
}else {
config = require('./config.production');
}
export default config;
Your config production .json file that will be selected if you are in the production environment. Same goes for development.
config.production.json
{
"graphQlEndpoint": {
"uri": "YourUriHere"
},
}
In the store, you can import the config.js file and then choose what variable you want. Example: config -> graphQlEndpoint -> uri
store.js
import config from '../../configs/config';
networkInterface = createNetworkInterface({
uri: config.graphQlEndpoint.uri
});
const store = createStore(rootReducer, defaultState, networkInterface);
I found it easier to use require() and to have my config files in son. You can then use your config file with the correct environment everywhere you import it.
I suggest you test this with npm run build.
Hope this helps
Related
I'm developing a BabylonJS application. BabylonJS PostProcess class appends .fragment.fx to a given file name and requests that from the server. When my local Vite (version 4.0.4) dev server serves this file the content-type header is empty. This causes Firefox to intepret it as type xml and fail. Chrome fails through a different, but I think related, mechanism.
How do you configure Vite to serve the *.fragment.fx static files as text/plain? I assume I need to disable the default middleware and write some custom code instead, like this: https://vitejs.dev/config/server-options.html#server-middlewaremode but I wanted to first check there wasn't something else going on / a simpler way to configure / fix this.
The vite dev server is started using vite --host --port 3000 --force and the config in vite.config.js is:
import { defineConfig } from 'vite';
export default defineConfig(({ command, mode }) => {
// if (command === 'serve') {
// return {
// // dev specific config
// }
// } else {
// // command === 'build'
// return {
// // build specific config
// }
// }
return {
resolve: {
alias: {
"babylonjs": mode === "development" ? "babylonjs/babylon.max" : "babylonjs",
}
},
base: "",
// assetsInclude: ['**/*.fx'],
};
});
* edit 1 *
I have seen there's a parameter ?raw that can be added to the URL however I don't control how BabylonJS forms the URL so I can't see how to make this work in this situation.
I followed these instructions and set up a dev server using express. I added this block of code above the call to app.use(vite.middlewares):
app.use("**/*.*.fx", async (req, res, next) => {
const url = req.originalUrl
const file_path = path.resolve(__dirname, "." + url)
const file = fs.readFileSync(file_path, "utf-8")
res.status(200).set({ "Content-Type": "text/plain" }).end(file)
})
I now start the dev server using the following script line in the package.json of "dev": "node server",
I could not find a way to solve this by configuring the default vite dev server.
I am looking through next.js documentation and trying to understand what the suggested approach is for setting URLs that change in different environments. Mostly, I want to ensure that I'm pointing backend URLs correctly in development versus production.
I suppose you can create a constants configuration file, but is there a supported, best practice for this?
Open next.config.js and add publicRuntimeConfig config with your constants:
module.exports = {
publicRuntimeConfig: {
// Will be available on both server and client
yourKey: 'your-value'
},
}
you can call it from another .js file like this
import getConfig from 'next/config'
const { publicRuntimeConfig } = getConfig()
console.log(publicRuntimeConfig.yourKey)
or even call it from view like this
${publicRuntimeConfig.yourKey}
You can configure your next app using next-runtime-dotenv, it allows you to specify serverOnly / clientOnly values using next's runtime config.
Then in some component
import getConfig from 'next/config'
const {
publicRuntimeConfig: {MY_API_URL}, // Available both client and server side
serverRuntimeConfig: {GITHUB_TOKEN} // Only available server side
} = getConfig()
function HomePage() {
// Will display the variable on the server’s console
// Will display undefined into the browser’s console
console.log(GITHUB_TOKEN)
return (
<div>
My API URL is {MY_API_URL}
</div>
)
}
export default HomePage
If you don't need this separation, you can use dotenv lib to load your .env file, and configure Next's env property with it.
// next.config.js
require('dotenv').config()
module.exports = {
env: {
// Reference a variable that was defined in the .env file and make it available at Build Time
TEST_VAR: process.env.TEST_VAR,
},
}
Check this with-dotenv example.
In my React app (using create react app) I have a Constants.js file that has the folowing constants:
export const API_ROOT = process.env.REACT_APP_API_ROOT || 'http://www.example.com.com:4000/api';
export const APP_ROOT = process.env.REACT_APP_APP_ROOT || 'http://app.example.com:3001';
For some reason this is not being picked up on my server even though I have defined the ENV variables on the server. I changed the values around just to see where the values are being picked up from.
API_ROOT=http://dev.example.com/api
APP_ROOT=http://app.example.com
REACT_APP_API_ROOT=http://www.example.com:3002/api
REACT_APP_APP_ROOT=http://app.example.com:3002
I wasn't sure of the naming convention so I defined all 4 of the above.
When I push to my server I still see the API_ROOT and APP_ROOT values being the defaults i.e. not from the ENV variable:
http://www.example.com.com:4000/api
http://app.example.com:3001
I did verify by logging into the server and verifying the ENV variables existed:
echo $API_ROOT
echo $REACT_APP_API_ROOT
What am I doing wrong in terms of getting the values from ENV variables?
process.env is a global Object provided by your environment through NodeJs. Because we don't have NodeJS in browser, it won't understand process.env.API_ROOT. You init your app using react-create-app with webpack included by default, so I recommend you to use .env file to set environment variables by using dotenv.
Note: dotenv is included in create-react-app v0.2.3 and higher
Create .env file include
API_ROOT=http://dev.example.com/api
APP_ROOT=http://app.example.com
REACT_APP_API_ROOT=http://www.example.com:3002/api
REACT_APP_APP_ROOT=http://app.example.com:3002
Config webpack:
const webpack = require('webpack');
const dotenv = require('dotenv');
module.exports = () => {
// call dotenv and it will return an Object with a parsed key
const env = dotenv.config().parsed;
// reduce it to a nice object, the same as before
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev;
}, {});
return {
plugins: [
new webpack.DefinePlugin(envKeys)
]
};
Reference:
https://medium.com/#trekinbami/using-environment-variables-in-react-6b0a99d83cf5
https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables#adding-development-environment-variables-in-env
Hope this will help.
I am pretty new to React but I want to set
BODYMOVIN_EXPRESSION_SUPPORT in Webpack's Define plugin with Gatsby v1.
I followed the links below but I don't get what exactly I suppose to do...
https://github.com/QubitProducts/react-bodymovin
https://www.gatsbyjs.org/docs/environment-variables/
I made the file named .env.development and put it to src folder. the content in this file is below.
plugins: ([
new webpack.DefinePlugin({
BODYMOVIN_EXPRESSION_SUPPORT: true
})
])
The folder structures is
root--
|
|- public //where the build goes
|
|- src -- //where I develop site
|-components
|-data
|-pages
|-style
|-.env.development
What I noticed is there is a line said
/*global BODYMOVIN_EXPRESSION_SUPPORT*/
in bodymovin library and I think I just need to change that. I could modify in library directly maybe but I don't think that a best way to get around this problem. Does someone know how to set this up right?
Thanks in advance!
EDIT 2019-09-02
To use environment variables from .env files I recommend using dotenv because it's so simple. Here's an example that creates an object of all the variables in the .env file and makes them accessible on the client side (i.e in React) through DefinePlugin.
// gatsby-node.js
var dotenv = require('dotenv');
const env = dotenv.config().parsed;
// Create an object of all the variables in .env file
const envKeys = Object.keys(env).reduce((prev, next) => {
prev[`process.env.${next}`] = JSON.stringify(env[next]);
return prev;
}, {});
exports.onCreateWebpackConfig = ({ stage, rules, loaders, plugins, actions }) => {
actions.setWebpackConfig({
plugins: [
// Add the environment variables to webpack.DefinePlugin with define().
plugins.define(envKeys)
]
});
};
Here's an example of how I get the application name and version from package.json and using it in my service worker, I'm using Gatsby V2 though. Having the version in the service worker makes caching easier to handle. As you wrote, DefinePlugin is the way to go but it's a bit different when we use it in Gatsby.
We need to import the package.json file and add our custom webpack configuration in gatsby-node.js, with plugins.define() we tell webpack to use DefinePlugin:
const packageJson = require('./package');
exports.onCreateWebpackConfig = ({
plugins,
actions,
}) => {
actions.setWebpackConfig({
plugins: [
plugins.define({
__NAME__: JSON.stringify(packageJson.name),
__VERSION__: JSON.stringify(packageJson.version),
}),
],
})
}
The two defined variables __NAME__ and __VERSION__ are now accessible in my service worker sw.js:
self.addEventListener('install', function (e) {
// eslint-disable-next-line
console.log(__NAME__, __VERSION__);
e.waitUntil(
caches.open(__NAME__ + __VERSION__).then(function(cache) {
return cache.addAll(filesToCache);
})
);
});
Gatsby Reference: https://www.gatsbyjs.org/docs/add-custom-webpack-config/
I'm using Create-React-App. I have 2 static JSON files with data which is different depending on the environment they run in.
What is the best approach to importing the files based on the environment the application runs in?
I have this but it doesn't look right, any other advice?
if (process.env.NODE_ENV !== 'production') {
import data from './data/devData';
}
if (process.env.NODE_ENV === 'production') {
import data from './data/prodData';
}
Another idea - add to your wabpack alias
resolve: {
alias: {
'config/data': path.resolve(__dirname, `./data/${process.env.NODE_ENV}`)
}
}
and call it from component this way:
import { data } from 'config/data';
You can create one file config with keys for example "default" and "production". Once import with file, check process.env and return actual config object.
const config = require('./data/config.json');
function getConfig() {
let customConfig = config['default'];
if (process.env.NODE_ENV === 'production') {
customConfig = config['production'];
}
return customConfig;
}