You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

angular-server-side-configuration

Package Overview
Dependencies
Maintainers
1
Versions
67
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-server-side-configuration

Configure an angular application on the server

1.3.0
Source
npmnpm
Version published
Weekly downloads
2K
11.78%
Maintainers
1
Weekly downloads
 
Created
Source

angular-server-side-configuration

Documentation

Configure an angular application at runtime on the server via environment variables.

Motivation

The Angular CLI provides build time configuration (via environment.ts). In a Continuous Delivery environment this is sometimes not enough.

How it works & Limitations

Environment variables are used for configuration. This package provides a script to search for usages in bundled angular files and a script for inserting populated environment variables into index.html file(s) by replacing <!--CONFIG--> (Missing environment variables will be represented by null). This should be done on the host serving the bundled angular files.

AoT

By default, this will not work in Module.forRoot or Module.forChild scripts or parameters. These are build time only due to AoT restrictions.

With ngssc wrap-aot ng build ... it is however possible to retain the configuration, by replacing the environment variables with tokens during the AoT build and reverting afterwards. (See CLI wrap-aot)

Getting Started

npm install --save angular-server-side-configuration

environment.prod.ts

Use process.env.NAME in your environment.prod.ts, where NAME is the environment variable that should be used.

import 'angular-server-side-configuration/process';

export const environment = {
  production: process.env.PROD !== 'false',
  apiAddress: process.env.API_ADDRESS || 'https://example-api.com'
};

index.html (Optional)

Add <!--CONFIG--> to index.html. This will be replaced by the configuration script tag. This is optional, as the environment variables can simply be inserted somewhere in the head tag. It is however the safest option.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Angular Example</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <!--CONFIG-->
</head>
<body>
  <app-root></app-root>
</body>
</html>

On host server or in Dockerfile (or similar)

npm install -g angular-server-side-configuration
ngssc insert /path/to/angular/files --search

CLI

angular-server-side-configuration provides a CLI.

npm install angular-server-side-configuration -g
ngssc --help

Insert

Usage: insert [options] [directory]

Search and replace the placeholder with environment variables (Directory defaults to current working directory)

OptionsDescription
-s, --searchSearch environment variables in available .js files (Defaults to false)
-e, --env <value>Add an environment variable to be resolved (default: [])
-p, --placeholder <value>Set the placeholder to replace with the environment variables (Defaults to <!--CONFIG-->)
-h, --headInsert environment variables into the head tag (after title tag, if available, otherwise before closing head tag)
--dryPerform the insert without actually inserting the variables
-h, --helpoutput usage information

Init

Usage: init [options] [directory]

Initialize an angular project with angular-server-side-configuration (Directory defaults to current working directory)

OptionsDescription
-ef, --environment-fileThe environment file to initialize (environmentFile defaults to src/environments/environment.prod.ts)
--npmInstall angular-service-side-configuration via npm (Default)
--yarnInstall angular-service-side-configuration via yarn
-h, --helpoutput usage information

Wrap-Aot

Usage: wrap-aot [options] [ng...]

Wrap an angular command with aot compilation to retain configuration (Use "ngssc wrap-aot ng build ..."). This will temporarily replace the content of the environment file with tokens. After the inner command completes, this is reverted and the tokens in the dist files will be replaced by the actual values.

OptionsDescription
-ef, --environment-fileThe environment file to prepare for aot-compilation (Defaults to src/environments/environment.prod.ts)
--distThe output path of the ng build (Defaults to dist/**)
-h, --helpoutput usage information

Native CLI

If node.js cannot be used on the target system, it is also possible to compile ngssc to a native CLI with tools like pkg or nexe.

Create a file named ngssc.js:

require('angular-server-side-configuration').cli().parse(process.argv);

And then use pkg or nexe to build the native ngssc CLI:

npm install pkg -g
pkg ngssc.js
npm install nexe -g
nexe ngssc.js --target os-of-target-system

License

Apache License, Version 2.0

Keywords

angular

FAQs

Package last updated on 09 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts