Skip to content

feat: change default search pattern to only include environment files #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2023
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
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ Starting with version 13, there are experimental builders for the `build` and `s
To install the experimental builders run `ng add angular-server-side-configuration` and answer
`Would you like to use the experimental builders for build and serve?` with `y(es)`.

## Changes with version 16

In order to better follow `Secure by default`, the default for `searchPattern` has been changed
to `{sourceRoot}/**/environments/environment*.ts` (from `{sourceRoot}/**/!(*server*).ts`).
If your project uses environment variables in other files, update the `searchPattern` entry
in your `ngsscbuild` block in your angular.json.

## Changes with version 15.1

With the change in version 15 to search all files for environment variables, a potential
Expand All @@ -35,17 +42,18 @@ This should have little to no impact, if you only have a simple angular.json wor
a backend component.

With 15.1 this risk is mitigated, as the lookup path is restricted by adding an option
`searchPattern` which defaults to `{sourceRoot}/**/!(*server*).ts` (only search `sourceRoot`
of the related angular.json project and exclude files with `server` in the name).
`searchPattern` which defaults to ~~`{sourceRoot}/**/!(*server*).ts` (only search `sourceRoot`
of the related angular.json project and exclude files with `server` in the name)~~
`{sourceRoot}/**/environments/environment*.ts` with version 16.

## Changes with version 15

With version 15 of the Angular CLI, environment files are no longer generated by default. In
order to adapt, using `ng add angular-server-side-configuration` will try to add the import in
either `environment.prod.ts`, `environment.ts`, `app.module.ts`, `app.components.ts` or `main.ts`
depending on availability. The ngssc builder will also now search all files for environment
depending on availability. ~~The ngssc builder will also now search all files for environment
variable usages instead of just the defined environment file. Due to this, the
`ngsscEnvironmentFile` option can be removed.
`ngsscEnvironmentFile` option can be removed.~~

## Getting Started

Expand Down Expand Up @@ -94,8 +102,8 @@ used environment variables and generate an [ngssc.json](#ngsscjson) in the defin
// Optional
// (Defaults to the basename of the index option of the browser target)
"filePattern": "index.html",
// (Defaults to {sourceRoot}/**/!(*server*).ts)
"searchPattern": "src/environments/environment*.ts"
// (Defaults to {sourceRoot}/**/environments/environment*.ts)
"searchPattern": "src/environments/environment.ts"
},
"configurations": {
"production": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export async function detectVariables(

const projectMetadata = await context.getProjectMetadata(projectName);
const sourceRoot = projectMetadata.sourceRoot as string | undefined;
const defaultSearchPattern = sourceRoot ? `${sourceRoot}/**/!(*server*).ts` : '**/!(*server*).ts';
const defaultSearchPattern = sourceRoot
? `${sourceRoot}/**/environments/environment*.ts`
: '**/environments/environment*.ts';

const detector = new VariableDetector(context.logger);
const typeScriptFiles = await glob(searchPattern || defaultSearchPattern, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"searchPattern": {
"type": "string",
"description": "The search pattern to use when searching for environment variable occurrences (Defaults to {sourceRoot}/**/!(*server*).ts)",
"description": "The search pattern to use when searching for environment variable occurrences (Defaults to {sourceRoot}/**/environments/environment*.ts)",
"default": ""
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
} from '@angular-devkit/schematics';
import { InsertChange } from '@schematics/angular/utility/change';
import { getWorkspace, updateWorkspace } from '@schematics/angular/utility/workspace';
import type { FileReplacement } from '@angular-devkit/build-angular';
import { Schema } from './schema';

export function ngAdd(options: Schema): Rule {
Expand Down
3 changes: 2 additions & 1 deletion test/hello-world-app/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
*/

import { Component } from '@angular/core';
import { environment } from '../environments/environment';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
})
export class AppComponent {
title = process.env.TEST;
title = environment.title;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const environment = {
title: 'dev',
};
5 changes: 5 additions & 0 deletions test/hello-world-app/src/environments/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'angular-server-side-configuration/process';

export const environment = {
title: process.env.TEST,
};
4 changes: 4 additions & 0 deletions test/hello-world-app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"compileOnSave": false,
"compilerOptions": {
"paths": {
"angular-server-side-configuration": ["../../dist/angular-server-side-configuration"],
"angular-server-side-configuration/*": ["../../dist/angular-server-side-configuration/*"]
},
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
Expand Down