I am using webpack, reactjs, typescript. In my .tsx code, I have a requirement where I need to route to URL depending upon my environment i.e. Production or Development. So, how can I check for the environment and load the correct URL dynamically ?
You can add a plugin to define an environment variable that can be accessed within the code, like this:
new webpack.DefinePlugin({
    'process.env.NODE_ENV': JSON.stringify('production')
})
And then inside your code you just have to check for process.env.NODE_ENV.
Needless to say that you can manage the plugin with an environment variable that you can pass via cli, like this:
webpack --env.production
And then have your webpack.config.js with something like this:
module.exports = function(env) {
    return {
        /*[...]*/
        plugins: [new webpack.DefinePlugin({
            'process.env.NODE_ENV': JSON.stringify(env.production ? 'production' : 'development')
        })]
        /*[...]*/
    };
};
Source: That's how react works :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With