How To Use .env Variables In Webpack.js
In this tutorial we’re going to learn how we can use our .env variables inside the webpack.js config. The benefit of this is that we can now build the frontend assets based off variables in the application.
uk.js file and a
us.js file, you'll use the
Therefore you can just use
MIX_APP_REGION or if you also need a
But these variables are not available in your
webpack.mix.js file, therefore you can't compile your assets based off environment variables.
Using Variables In webpack.mix.js
The solution to get these variables in your
webpack.mix.js is to use a package called
dotenv, which is already installed by Laravel mix.
Then you can add this to the top of your
This will add your environment variables to be accessed in the process variable.
mix.js('resources/js/' + process.env.APP_REGION + '.js', 'public/assets/js/region.js').version()
Originally published at https://paulund.co.uk.