
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
angular-server-side-configuration
Advanced tools
Configure an angular application at runtime on the server via environment variables.
The Angular CLI provides build time configuration (via environment.ts). In a Continuous Delivery environment this is sometimes not enough.
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.
This will not work in Module.forRoot or Module.forChild scripts or parameters. These are build time only due to AOT restrictions.
npm install --save angular-server-side-configuration
Use process.env.NAME in your environment.prod.ts, where NAME is the environment variable that should be used.
export const environment = {
production: process.env.PROD !== 'false',
apiAddress: process.env.API_ADDRESS || 'https://example-api.com'
};
Import angular-server-side-configuration/process
in polyfill.ts. This will enable
the typings for process.env.* in the code and register a fallback value for process.env.
/**
* This file includes polyfills needed by Angular and is loaded before the app.
* You can add your own extra polyfills to this file.
...
/***************************************************************************************************
* APPLICATION IMPORTS
*/
import 'angular-server-side-configuration/process';
Add <!--CONFIG-->
to index.html. This will be replaced by the configuration script tag.
<!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>
EnvironmentVariablesConfiguration
provides a way of looking for usages of process.env.*
in the bundled angular javascript files and provides a way of inserting the environment variables
into the index.html file(s).
// Express is completely optional
import * as express from 'express';
const app = express();
...
import { EnvironmentVariablesConfiguration } from 'angular-server-side-configuration';
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
envVariables.insertAndSaveRecursively(bundledAngularFilesRoot)
// Optional
.then(() => {
app.listen(PORT, () => {
console.log(`Node server listening on http://localhost:${PORT}`);
});
});
Import angular-server-side-configuration/process
in polyfill.ts. This will enable
the typings for process.env.* in the code and register a fallback value for process.env.
import 'angular-server-side-configuration/process';
Accepts an array of environment variables.
const envVariables = new EnvironmentVariablesConfiguration(['PROD', 'API_ADDRESS']);
By default the searchEnvironmentVariables method uses a regex
(/process\s*\.\s*env\s*\.\s*[a-zA-Z0-9_]+/gm
) to look for process.env.* usages.
It is possible to provide a discovery function in the options as a second parameter.
root
The root directory from which to search.
options
Optional options for searching environment variables.
options.filePattern
The file pattern in which environment variables should be searched
(Defaults to /.js$/).
options.environmentVariablesDiscovery
The function to discover environment variables in
the matched files (Defaults to process.env.VARIABLE => VARIABLE).
Returns an instance of EnvironmentVariablesConfiguration.
const envVariables: EnvironmentVariablesConfiguration =
EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
Generates an object, with the environment variable names being the key and the actual values being the values. Missing environment variables will be represented by null.
const envVariables = new EnvironmentVariablesConfiguration(['PROD', 'API_ADDRESS']);
process.env.API_ADDRESS = 'https://example-api.com';
const variables = envVariables.populateVariables();
// { PROD: null, API_ADDRESS: 'https://example-api.com' }
Generates the IIFE in which the environment variables are assigned to window.process.env.
const envVariables = new EnvironmentVariablesConfiguration(['PROD', 'API_ADDRESS']);
process.env.API_ADDRESS = 'https://example-api.com';
const iife = envVariables.generateIIFE();
// (function(self){self.process={env:{"PROD":null,"API_ADDRESS":"https://example-api.com"}};})(window)
Inserts the discovered environment variables as an IIFE wrapped in a script tag into the specified file content without saving the file.
file
The file to be read.
options
Optional options for insertion.
options.insertionRegex
The replacement pattern, where the configuration should be inserted
(Defaults to /<!--\s*CONFIG\s*-->/
).
Returns a promise, which resolves to the file content with the environment variables inserted.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
const content = await envVariables.apply(pathToIndexHtml);
// <html>...<script>(function(self){self.process={env:{"PROD":null,"API_ADDRESS":"https://example-api.com"}};})(window)</script>...</html>
Inserts the discovered environment variables as an IIFE wrapped in a script tag into the specified file.
file
The file into which the environment variables should be inserted.
options
Optional options for insertion.
options.insertionRegex
The replacement pattern, where the configuration should be inserted
(Defaults to /<!--\s*CONFIG\s*-->/
).
Returns a promise, which resolves after the enivornment variables have been saved to the given file.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables.insertAndSave(pathToIndexHtml);
Inserts the discovered enviornment variables as an IIFE wrapped in a script tag into the matched files.
root
The root directory from which to search insertion files.
options
Optional options for insertion.
options.filePattern
The file pattern in which the configuration should be inserted
(Defaults to /index.html$/).
options.insertionRegex
The replacement pattern, where the configuration should
be inserted (Defaults to /<!--\s*CONFIG\s*-->/
).
Returns a promise, which resolves after the environment variables have been inserted to all matched files.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables.insertAndSaveRecursively(bundledAngularFilesRoot);
Add a replacement function for the file received through apply, insertAndSave or insertAndSaveRecursively. The function receives the file content and the file name as parameters and returns the file content with the replacement applied.
replacement
The replacement function.
Returns this instance.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables
.replace((fileContent, fileName) => fileContent.replace('search-example', 'replace-example'))
.insertAndSaveRecursively(bundledAngularFilesRoot);
Add a replacement for the file received through apply, insertAndSave or insertAndSaveRecursively.
regex
A RegExp object or literal. The match or matches are replaced with replaceValue.
replaceValue
The value that replaces the substring matched by the regex parameter.
Returns this instance.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables
.regexReplace(/search-example-[0-9]/g, 'replace-example')
.insertAndSaveRecursively(bundledAngularFilesRoot);
Replace the attribute value of a tag for the file received through apply, insertAndSave or insertAndSaveRecursively.
tag
The tag, whose attribute value should be replaced.
attribute
The attribute, whose value should be replaced.
newValue
The new attribute value.
Returns this instance.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables
.replaceTagAttribute('body', 'id', newId)
.insertAndSaveRecursively(bundledAngularFilesRoot);
Replace the html lang attribute for the file received through apply, insertAndSave or insertAndSaveRecursively.
newHtmlLang
The new base href.
Returns this instance.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables
.replaceHtmlLang('de')
.insertAndSaveRecursively(bundledAngularFilesRoot);
Replace the base href attribute for the file received through apply, insertAndSave or insertAndSaveRecursively.
newBaseHref
The new base href.
Returns this instance.
const envVariables = EnvironmentVariablesConfiguration.searchEnvironmentVariables(bundledAngularFilesRoot);
await envVariables
.replaceBaseHref('/de/')
.insertAndSaveRecursively(bundledAngularFilesRoot);
Apache License, Version 2.0
FAQs
Configure an angular application on the server
The npm package angular-server-side-configuration receives a total of 1,551 weekly downloads. As such, angular-server-side-configuration popularity was classified as popular.
We found that angular-server-side-configuration demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.