1.0.0 • Published 5 months ago

nx-angular-env v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

This plugin for Nx offers a modern approach to configuring Angular applications. Traditionally, each Angular project has its own set of environment.*.ts files to cover different environments. While this method works, it can become cumbersome as the project grows, especially for containerized deployments. That's where Nx Angular Env comes in, providing a more flexible and scalable solution by utilizing environment variables.

Contents

🛠️ Overview

Nx Angular Env requires a single TypeScript template file for each project. This template maps TypeScript variables to external environment variables, optionally providing default values. Here's an example of what the template file could look like:

// environment.template.ts
const env = {}
export const environment = {
  production: env["IS_PRODUCTION"] ?? false,
  apiUrl: env["API_URL"] ?? "http://localhost:8080",
}

The generated environment file could then look like this:

// environment.ts
export const environment = {
  production: true,
  apiUrl: "https://api.example.com",
}

By following this approach, you can configurate Angular applications with external environment variables. In Docker, for example, you can use the --env flag to set environment variables. For local development or repository-level configuration, you can also set environment variables using .env files. Read more about the usage of .env files here.

📦 Installation

Prerequisites

Make sure you have the following:

  • An existing Nx workspace
  • An Angular project set up within this workspace

Step 1: Install the plugin

NPM:

npm install nx-angular-env --save-dev

Yarn:

yarn add nx-angular-env --dev

Step 2: Configure a new target

Configure a new target in your project's project.json file and use the nx-angular-env:prepare executor.

Options

OptionDescriptionDefault
envFilePath to the environment file to be generatedNone
templateFilePath to the environment file to be used as template{envFileDirectory}/environment.template.ts
{
  "prepare-env": {
    "executor": "nx-angular-env:prepare",
    "options": {
      "envFile": "apps/my-app/src/environment.ts",
      "templateFile": "apps/my-app/src/environment.template.ts"
    }
  }
}

Step 3: Integrate the target into your workflow

To make sure that the generated environment file is up to date before building, serving or testing your app, you need to add the prepare-env target as a dependency to the respective targets.

{
  "build": {
    "executor": "@angular-devkit/build-angular:application",
    "dependsOn": ["prepare-env"]
  },
  "serve": {
    "executor": "@angular-devkit/build-angular:dev-server",
    "dependsOn": ["prepare-env"]
  },
  "test": {
    "executor": "@nx/jest:jest",
    "dependsOn": ["prepare-env"]
  }
}

Step 4: Create a template file

Define the variables and their default values in a template file like in the following example:

// environment.template.ts
const env = {}
export const environment = {
  production: env["IS_PRODUCTION"] ?? false,
  apiUrl: env["API_URL"] ?? "http://localhost:8080",
}

Step 5: Ignore the environment file

It is recommended to ignore the environment.ts file in your version control system.

🚀 Usage

After successful installation and configuration, you can use environment variables to configure your Angular application. Whenever your app is built, served or tested, the plugin updates the environment file.

Manual generation

You can run the prepare-env target to manually generate the environment file:

nx prepare-env my-app

Usage of .env files

Nx provides a flexible way to manage environment variables in your project. It reads .env files for different configurations or targets on a workspace or app level. For example, you can have files like .development.env and .production.env in your workspace or app root.

Environment variables follow a specific order of priority, with the highest importance given to the system environment. Below is the order of precedence for environment configurations:

#Configuration FileDescription
1System EnvironmentPrimary and overarching configuration source
2apps/my-app/.env.<target>.<configuration>Target and configuration-specific environment file
3apps/my-app/.<target>.<configuration>.envAlternative format for above
4apps/my-app/.env.<configuration>Configuration-specific environment file
5apps/my-app/.<configuration>.envAlternative format for above
6apps/my-app/.env.<target>Target-specific environment file
7apps/my-app/.<target>.envAlternative format for above
8apps/my-app/.env.localLocal environment settings for the app
9apps/my-app/.local.envAlternative format for above
10apps/my-app/.envGeneral environment file for the app
11.env.<target>.<configuration>Global, yet target and configuration-specific file
12.<target>.<configuration>.envAlternative format for above
13.env.<configuration>Global configuration-specific environment file
14.<configuration>.envAlternative format for above
15.env.<target>Global target-specific environment file
16.<target>.envAlternative format for above
17.local.envGlobal local environment settings
18.env.localAlternative format for above
19.envThe most general, global environment settings file

🤝 Support

Encountered an issue or have suggestions for improvements? Feel free to raise an issue.

Contributions are welcome!