Modern.js provides support for environment variables, including built-in environment variables and custom environment variables.
The current path prefix of the asset, which is a read-only environment variable.
The current execution environment and is a read-only environment variable whose have different values under different execution commands:
production: Default value when running modern build or modern serve.development: Default value when running modern dev, also the default value in other cases.Set the current execution environment manually. In addition to the values in the NODE_ENV, custom environment names are supported here, such as staging, boe, etc.
MODERN_ENV priority is higher than NODE_ENV.
When using @modern-js/runtime, Modern.js will automatically inject MODERN_TARGET to distinguish between SSR and CSR environments.
You can use process.env.MODERN_TARGET to determine the environment and execute appropriate code.
After the development build, you can see that the SSR and CSR bundles as follows:
This can provide different outputs for different environments to ensure that the bundle size is minimized. It can also be convenient to deal with some side effects for different environments.
In production environment, minimizers such as Terser and SWC will analyze the code and remove dead code to reduce the bundle size. This process is called "Dead Code Elimination" (DCE).
For example, the code inside the if (false) statement will be removed, while the code inside the if (true) will be preserved.
If you do not use process.env.MODERN_TARGET as described above, the code minimizer may not be able to analyze the dead code, resulting in an increased bundle size.
You can specify custom environment variables in both shell and .env files.
shellAdd custom environment variables before the command:
.env fileCreate a .env file in the project root and add custom environment variables, which are added to the Node.js process by default, for example:
The .env file follows the following loading rules:
.env: default..env.{ MODERN_ENV | NODE_ENV }: Overrides .env for a specific environment.When you need to use different config according to the environment, you can define environment variables in the .env file corresponding to the environment name, and manually set the execution environment when starting the project.
For example, when starting a project with the following command, the .env and .env.staging will load:
NODE_ENV can be used directly in front-end code. In addition, custom environment variables starting with MODERN_ can also be used directly in code.
For Example:
After executing the pnpm run dev, you can see the following bundle:
In custom HTML templates, you can also use such environment variables directly. For example, in config/html/head.html:
If you need to use environment variables with any other names in your code, you can configure them in source.globalVars. For example:
At this point, the process.env.VERSION in the code will be replaced by the value of VERSION in the environment variables.
source.globalVars also supports replacing other expressions or strings with specified values, not limited to environment variables.
In addition to environment variables, Modern.js also supports replacing variables in code with other values or expressions, which can be used to distinguish between development environment and production environment in code.
For example, converts the expression TWO to 1 + 1:
In most cases, source.globalVars is already sufficient to replace variables. But the values passed in by source.globalVars will be serialized by JSON by default. So it cannot be replaced like 1 + 1 in the example above, at this time, we need to use source.define.