Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/nx-workspace/apps/ng-app-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@angular/platform-server": "^20.0.5",
"@angular/router": "^20.0.0",
"@angular/ssr": "^20.0.4",
"@ngx-env/builder": "workspace:20.1.0",
"@ngx-env/builder": "workspace:20.1.1",
"express": "^5.1.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
Expand All @@ -44,7 +44,7 @@
"@dotenv-run/core": "workspace:^1.3.8",
"@dotenv-run/jest-angular": "workspace:^0.2.1",
"@jest/transform": "^29.7.0",
"@ngx-env/builder": "workspace:^20.1.0",
"@ngx-env/builder": "workspace:^20.1.1",
"@types/express": "^5.0.1",
"@types/jasmine": "~5.1.0",
"@types/node": "^22.12.0",
Expand Down
31 changes: 31 additions & 0 deletions examples/nx-workspace/apps/ng-app-cli/src/custom-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Include theming for Angular Material with `mat.theme()`.
// This Sass mixin will define CSS variables that are used for styling Angular Material
// components according to the Material 3 design spec.
// Learn more about theming and how to use it for your application's
// custom components at https://material.angular.dev/guide/theming
@use "@angular/material" as mat;

html {
@include mat.theme(
(
color: (
primary: mat.$azure-palette,
tertiary: mat.$blue-palette,
),
typography: Roboto,
density: 0,
)
);

// Default the application to a light color theme. This can be changed to
// `dark` to enable the dark color theme, or to `light dark` to defer to the
// user's system settings.
color-scheme: light;

// Set a default background, font and text colors for the application using
// Angular Material's system-level CSS variables. Learn more about these
// variables at https://material.angular.dev/guide/system-variables
background-color: var(--mat-sys-surface);
color: var(--mat-sys-on-surface);
font: var(--mat-sys-body-medium);
}
6 changes: 6 additions & 0 deletions packages/angular/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @ngx-env/builder

## 20.1.1

### Patch Changes

- Fix SSR with runtime option

## 20.1.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngx-env/builder",
"version": "20.1.0",
"version": "20.1.1",
"description": "Easily inject environment variables into your Angular applications",
"author": "chihab <chihab@gmail.com>",
"homepage": "https://github.com/chihab/ngx-env/tree/main/packages/angular",
Expand Down
38 changes: 25 additions & 13 deletions packages/angular/src/builders/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { join } from "path";
import { from, switchMap, tap } from "rxjs";
import { NgxEnvSchema } from "../ngx-env/ngx-env-schema";
import { getEnvironment } from "../utils/get-environment";
import { indexHtml } from "../utils/index-html-build";
import { writeRuntimeFile } from "../utils/write-ngx-env-runtime";
import { getProjectCwd } from "../utils/project";
import { replaceHtmlVars } from "../utils/replace-html-vars";
import { indexHtmlTransformer } from "../utils/index-html-transform";

export const executeWithEnv = (
options: ApplicationBuilderOptions & NgxEnvSchema,
Expand All @@ -28,20 +30,30 @@ export const executeWithEnv = (
});
options.define = full;
return fromAsyncIterable<BuilderOutput>(
buildApplication(options, context)
buildApplication(options, context, {
indexHtmlTransformer: async (html) => {
return indexHtmlTransformer(
html,
raw,
false,
dotEnvOptions.runtime
);
},
})
).pipe(
tap(() => {
const outputDir = join(
context.workspaceRoot,
options.outputPath?.toString() ?? `dist/${context.target.project}`
);
indexHtml(
join(outputDir, "browser"),
options.ssr ? join(outputDir, "server") : null,
Array.isArray(options.localize) ? options.localize : [],
raw,
dotEnvOptions.runtime
);
if (dotEnvOptions.runtime) {
const outputDir = join(
context.workspaceRoot,
options.outputPath?.toString() ?? `dist/${context.target.project}`
);
writeRuntimeFile(
join(outputDir, "browser"),
options.ssr ? join(outputDir, "server") : null,
Array.isArray(options.localize) ? options.localize : [],
raw
);
}
})
);
})
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/builders/browser-esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { join } from "path";
import { from, switchMap, tap } from "rxjs";
import { NgxEnvSchema } from "../ngx-env/ngx-env-schema";
import { getEnvironment } from "../utils/get-environment";
import { indexHtml } from "../utils/index-html-build";
import { indexHtmlTransformerLegacy } from "../utils/index-html-transform-legacy";
import { getProjectCwd } from "../utils/project";

export const executeWithEnv = (
Expand All @@ -31,7 +31,7 @@ export const executeWithEnv = (
buildEsbuildBrowser(options, context, undefined)
).pipe(
tap(() => {
indexHtml(
indexHtmlTransformerLegacy(
join(
context.workspaceRoot,
options.outputPath.toString(),
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/builders/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { join } from "path";
import { from, switchMap, tap } from "rxjs";
import { NgxEnvSchema } from "../ngx-env/ngx-env-schema";
import { getEnvironment } from "../utils/get-environment";
import { indexHtml } from "../utils/index-html-build";
import { indexHtmlTransformerLegacy } from "../utils/index-html-transform-legacy";
import { getProjectCwd } from "../utils/project";
import { plugin } from "../utils/webpack-plugin";

Expand All @@ -26,7 +26,7 @@ export const executeWithEnv = (
webpackConfiguration,
}).pipe(
tap(() => {
indexHtml(
indexHtmlTransformerLegacy(
join(context.workspaceRoot, options.outputPath.toString()),
null, // no ssr support with browser,
Array.isArray(options.localize) ? options.localize : [],
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/builders/dev-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { DotenvRunOptions } from "@dotenv-run/core";
import { env } from "@dotenv-run/core";
import { catchError, combineLatest, switchMap } from "rxjs";
import { getEnvironment } from "../utils/get-environment";
import { indexHtml } from "../utils/index-html-serve";
import { indexHtmlTransformer } from "../utils/index-html-transform";
import { getProjectCwd } from "../utils/project";
import { validateAndPrepareBuildContext } from "../utils/validate-prepare-context";

Expand Down Expand Up @@ -35,7 +35,7 @@ export const executeWithEnv = (
});
return executeDevServerBuilder(options, context, {
indexHtmlTransformer: async (content: string) =>
indexHtml(content, raw, dotenvRunOptions.runtime),
indexHtmlTransformer(content, raw, true, dotenvRunOptions.runtime),
});
}),
catchError((e) => {
Expand Down
90 changes: 0 additions & 90 deletions packages/angular/src/builders/utils/index-html-build.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/angular/src/builders/utils/index-html-serve.ts

This file was deleted.

58 changes: 58 additions & 0 deletions packages/angular/src/builders/utils/index-html-transform-legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { type Dict } from "@dotenv-run/core";
import { readFileSync, writeFileSync } from "fs";
import * as glob from "glob";
import { replaceHtmlVars } from "./replace-html-vars";
import { writeRuntimeFile } from "./write-ngx-env-runtime";

export function indexHtmlTransformerLegacy(
browserOutputDir: string,
serverOutputDir: string | null,
locales: string[] = [],
raw: Dict,
runtime = false
) {
glob
.sync(`${browserOutputDir}/**/index{.html,.csr.html}`)
.forEach((filePath) => {
const html = readFileSync(filePath, "utf-8");
const content = replaceHtmlVars(html, raw); // Replace %VARIABLE% with the actual value
try {
writeFileSync(
filePath,
runtime
? content.replace(
/<head>/,
`<head><script src="/ngx-env.js"></script>`
)
: content
);
} catch (e) {
console.log(`❌ Failed to replace variables in ${filePath} ❌`);
throw e;
}
});
if (serverOutputDir) {
glob.sync(`${serverOutputDir}/**/*html*`).forEach((filePath) => {
const html = readFileSync(filePath, "utf-8");
const content = replaceHtmlVars(html, raw); // Replace %VARIABLE% with the actual value
console.log(`📦 Replace variables in ${filePath}`);
try {
writeFileSync(
filePath,
runtime
? content.replace(
/<head>/,
`<head><script src="/ngx-env.js"></script>`
)
: content
);
} catch (e) {
console.log(`❌ Failed to replace variables in ${filePath} ❌`);
throw e;
}
});
}
if (runtime) {
writeRuntimeFile(browserOutputDir, serverOutputDir, locales, raw);
}
}
21 changes: 21 additions & 0 deletions packages/angular/src/builders/utils/index-html-transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { Dict } from "@dotenv-run/core";
import { replaceHtmlVars } from "./replace-html-vars";

export function indexHtmlTransformer(
content: string,
raw: Dict,
serve: boolean,
runtime = false
) {
const html = replaceHtmlVars(content, raw);
return runtime
? html.replace(
/<head>/,
serve
? `<head><script>globalThis._NGX_ENV_ = ${JSON.stringify(
raw
)}</script>`
: `<head><script src="ngx-env.js"></script>`
)
: html;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Dict } from "@dotenv-run/core";
import { escapeStringRegexp } from "./escape-string-regexp";

export const variablesReducer = (content: string, raw: Dict) => {
export const replaceHtmlVars = (content: string, raw: Dict) => {
return Object.keys(raw).reduce(
(html, key) =>
html.replace(
Expand Down
4 changes: 2 additions & 2 deletions packages/angular/src/builders/utils/webpack-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DotenvRunOptions } from "@dotenv-run/core";
import { DotenvRunPlugin } from "@dotenv-run/webpack";
import type { Configuration } from "webpack";
import { indexHtml } from "./index-html-serve";
import { indexHtmlTransformer } from "./index-html-transform";

export function plugin(options: DotenvRunOptions, ssr = false) {
const dotEnvPlugin = new DotenvRunPlugin(
Expand All @@ -16,7 +16,7 @@ export function plugin(options: DotenvRunOptions, ssr = false) {
return webpackConfig;
},
indexHtml: async (content: string) => {
return indexHtml(content, raw, options.runtime);
return indexHtmlTransformer(content, raw, false, options.runtime);
},
};
}
Loading