npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nx-angular-env

v1.0.0

Published

Nx plugin for Angular, enabling configuration via external environment variables

Downloads

4

Readme

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

| Option | Description | Default | | -------------- | --------------------------------------------------- | -------------------------------------------- | | envFile | Path to the environment file to be generated | None | | templateFile | Path 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 File | Description | | --- | ------------------------------------------- | -------------------------------------------------- | | 1 | System Environment | Primary and overarching configuration source | | 2 | apps/my-app/.env.<target>.<configuration> | Target and configuration-specific environment file | | 3 | apps/my-app/.<target>.<configuration>.env | Alternative format for above | | 4 | apps/my-app/.env.<configuration> | Configuration-specific environment file | | 5 | apps/my-app/.<configuration>.env | Alternative format for above | | 6 | apps/my-app/.env.<target> | Target-specific environment file | | 7 | apps/my-app/.<target>.env | Alternative format for above | | 8 | apps/my-app/.env.local | Local environment settings for the app | | 9 | apps/my-app/.local.env | Alternative format for above | | 10 | apps/my-app/.env | General environment file for the app | | 11 | .env.<target>.<configuration> | Global, yet target and configuration-specific file | | 12 | .<target>.<configuration>.env | Alternative format for above | | 13 | .env.<configuration> | Global configuration-specific environment file | | 14 | .<configuration>.env | Alternative format for above | | 15 | .env.<target> | Global target-specific environment file | | 16 | .<target>.env | Alternative format for above | | 17 | .local.env | Global local environment settings | | 18 | .env.local | Alternative format for above | | 19 | .env | The most general, global environment settings file |

🤝 Support

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

Contributions are welcome!