Skip to content

Env

Zova loads environment files based on multi-dimensional variables, providing a more flexible configuration mechanism and supporting more complex business scenarios

meta & .env files

Zova uses dotenv to load environment variables from the following files in the directory env:

txt
.env                # loaded in all cases
.env.[meta]         # only loaded in specified condition
.env.mine           # loaded in all cases, ignored by git
.env.[meta].mine    # only loaded in specified condition, ignored by git
  • [meta] can be any combination of the following three variables
NameDescription
mode'development' | 'production'
appMode'spa' | 'ssr'
flavor'web' | 'admin'

npm scripts

Corresponding to the multidimensional variables, the correspondence between the commands and the scripts are as follows:

bash
$ npm run dev:ssr:admin
$ npm run build:ssr:admin
json
"scripts": {
  "dev": "npm run dev:ssr:admin",
  "build": "npm run build:ssr:admin",
  "preview": "npm run preview:ssr",
  "dev:ssr:admin": "npm run prerun && quasar dev --mode ssr --flavor admin",
  "build:ssr:admin": "npm run prerun && quasar build --mode ssr --flavor admin",
  "preview:ssr": "concurrently \"node ./dist-mock/index.js\" \"node ./dist/ssr/index.js\"",
},

For example

Execute npm run dev on the command line, then the corresponding meta variable values are:

NameValue
mode'development'
flavor'admin'
appMode'ssr'

The system will automatically load the environment variables in the following files and merge them:

txt
.env
.env.ssr
.env.ssr.admin
.env.ssr.admin.development
.env.mine
.env.ssr.mine
.env.ssr.admin.mine
.env.ssr.admin.development.mine

Tree-shaking

ZovaJS only supports tree-shaking during builds for the following environment variables:

NameDescription
META_MODERuntime Environment
META_APP_MODEApp Mode
META_FLAVORFlavor
NODE_ENV= process.env.META_MODE
DEV= process.env.META_MODE === 'development'
PROD= process.env.META_MODE === 'production'
SSR= process.env.META_APP_MODE === 'ssr'
CLIENTCheck if Client
SERVERCheck if Server

For example:

typescript
if (process.env.DEV) {
  console.log('for development');
}

During builds, this is automatically converted to:

typescript
if (false) {
  console.log('for development');
}

Since the condition is false, tree-shaking is performed

Obtaining Environment Variables

1. process.env

For environment variables that support tree-shaking, use process.env

typescript
process.env.META_MODE;
process.env.META_APP_MODE;
process.env.META_FLAVOR;
process.env.NODE_ENV;
process.env.DEV;
process.env.PROD;
process.env.SSR;
process.env.CLIENT;
process.env.SERVER;
  • process.env.NODE_ENV: For compatibility with the Node.js ecosystem only; process.env.META_MODE is preferred

2. sys.env

For environment variables that don't support tree-shaking, use sys.env to obtain them

typescript
this.sys.env.APP_NAME;
this.sys.env.APP_TITLE;
this.sys.env.APP_PUBLIC_PATH;

Built-in env variables

ZovaJS provides several built-in env variables:

Meta

NameDescription
META_MODEMode
META_APP_MODEApp Mode
META_FLAVORFlavor

App

NameDescription
APP_NAMEApp Name
APP_TITLEApp Title
APP_DESCRIPTIONApp Description
APP_VERSIONApp Version
APP_PUBLIC_PATHVite: Public Base Path
APP_LOCALE_DEFAULTDefault Locale

Router

NameDescription
ROUTER_MODEVue Router: History Modes

Dev Server

NameDescription
DEV_SERVER_HOSTNAMEDev server host Vite: server.host
DEV_SERVER_PORTDev server port

Project

NameDescription
PROJECT_DISABLED_MODULESList of disabled modules
PROJECT_DISABLED_SUITESList of disabled suites

Build

NameDescription
BUILD_OUTDIRSpecify the output directory
BUILD_MINIFYWhether to enable minify
BUILD_SOURCEMAPWhether to generate sourcemap
BUILD_ANALYZEWhether to display the analyze info

API

NameDescription
API_BASE_URL
API_PREFIX
API_JWTWhether to enable JWT

PINIA

NameDescription
PINIA_ENABLEDWhether to enable Pinia

Proxy

NameDescription
PROXY_API_ENABLEDWhether to enable proxy: Vite: server.proxy
PROXY_API_BASE_URLproxy target
PROXY_API_PREFIXproxy key

SSR

See: SSR

Mock

See: Mock

Dynamic environment variables

The following are the environment variables set according to the runtime environment:

NameDescription
SSRIf SSR mode
DEVIf Development
PRODIf Production
CLIENTIf Client
SERVERIf Server

Released under the MIT License.