This document contains an explanation of how you can create and maintain multiple environments for your Quasar
projects. It will be an expansion of the official documentation located here for Webpack
and here for Vite
. Bear in mind that this guide is applicable to both versions of Quasar
.
Create a variables
folder in the project root, with a parser.js
file inside. This will allow you to have all environment related files in a single place, which will improve readability.
Set the contents of newly created parser.js
file to be the following:
const dotenv = require('dotenv');
const files = {
...dotenv.config({ path: 'variables/.env' }).parsed,
...dotenv.config({ path: `variables/.env.${process.env.ENVIRONMENT}` }).parsed,
};
module.exports = () => {
Object.keys(files, (key) => {
if (typeof files[key] !== 'string') {
files[key] = JSON.stringify(files[key]);
}
});
return files;
};
As you can see, parser requires dotenv
package, so the npm i dotenv
command needs to be ran for it to work properly.
You could create multiple environment files in the variables
folder, like .env.development
, .env.testing
, .env.staging
, .env.production
or whatever you need for the particular project. The .env
file is the one you should use for all of the common variables. It's always a good practice to create a separate .env.example
file which will contain the list of all the variables expected in the other files for application to run properly.
Below is the default content of the files I use to start almost all of the projects with. I use comments as well, just as a precaution for new developers to be aware of what each variable does.
# Variable which separates environments
ENVIRONMENT =
# Sentry DSN tells the SDK where to send the events to
SENTRY_DSN =
# Base URL to which all API endpoints will be forwarded
BASE_URL =
In the quasar.config.js
file you need to import the parser and set env
property of the build
attribute to it:
const PARSER = require('./variables/parser')();
build: {
env: PARSER
}
In your .gitignore
file add the following lines to exclude all sensitive files from ending up on source control, except the .env.example
one.
# Exclude all environment files except example one
.env*
!.env.example
What is left is to create custom scripts for all of the environments, so in your package.json
file, under scripts
, you should add scripts like this for every required environment:
"dev:local": "cross-env ENVIRONMENT=local quasar dev",
"build:local": "cross-env ENVIRONMENT=local quasar build --debug",
"dev:production": "cross-env ENVIRONMENT=production quasar dev",
"build:production": "cross-env ENVIRONMENT=production quasar build",
If you often switch workstations and OS types, I suggest that you install cross-env
package which will save you a lot of time. So, running the command npm i cross-env
should be done, as well.
Now you have successfully implemented a sturdy base for creating multiple environments in your Quasar applications.
It should be handled natively in that version by Razvan and the rest of the Quasar core team, but the best place to confirm that would be their Discussions section.