Skip to content

Commit 283b564

Browse files
committed
feat(@schematics/angular): remove environment files in new applications
This commit removes the usage of environment files and `fileReplacements` in new application projects. Previously, the environment files was used to distinguish between a prod build to invoke `enableProdMode`. The `enableProdMode` however needed only for the case of JIT mode in production mode, which is a rare case as JIT mode is recommanded to be used in production. In the CLI, calling `enableProdMode` is not needed as `ngDevMode` it's set using the minifier.
1 parent 56a1e8f commit 283b564

File tree

30 files changed

+68
-401
lines changed

30 files changed

+68
-401
lines changed

packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ describe('AppShell Builder', () => {
5151
5252
import { AppRoutingModule } from './app-routing.module';
5353
import { AppComponent } from './app.component';
54-
import { environment } from '../environments/environment';
5554
import { RouterModule } from '@angular/router';
5655
5756
@NgModule({
@@ -91,15 +90,8 @@ describe('AppShell Builder', () => {
9190
export class AppServerModule {}
9291
`,
9392
'src/main.ts': `
94-
import { enableProdMode } from '@angular/core';
9593
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
96-
9794
import { AppModule } from './app/app.module';
98-
import { environment } from './environments/environment';
99-
100-
if (environment.production) {
101-
enableProdMode();
102-
}
10395
10496
document.addEventListener('DOMContentLoaded', () => {
10597
platformBrowserDynamic().bootstrapModule(AppModule)

packages/angular_devkit/build_angular/src/builders/browser/specs/rebuild_spec.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,8 @@ describe('Browser Builder rebuilds', () => {
5555
export let X = '$$_E2E_GOLDEN_VALUE_2';
5656
`,
5757
'src/main.ts': `
58-
import { enableProdMode } from '@angular/core';
5958
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
60-
6159
import { AppModule } from './app/app.module';
62-
import { environment } from './environments/environment';
63-
64-
if (environment.production) {
65-
enableProdMode();
66-
}
6760
6861
platformBrowserDynamic().bootstrapModule(AppModule);
6962

packages/angular_devkit/build_angular/src/builders/browser/specs/unused-files-warning_spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ describe('Browser Builder unused files warnings', () => {
3838
});
3939

4040
it('should show warning when some files are unused', async () => {
41-
host.replaceInFile(
42-
'src/tsconfig.app.json',
43-
'"main.ts"',
44-
'"main.ts", "environments/environment.prod.ts"',
45-
);
41+
host.writeMultipleFiles({
42+
'src/unused-file.ts': `export const unused = '1';`,
43+
});
44+
45+
host.replaceInFile('src/tsconfig.app.json', '"main.ts"', '"main.ts", "unused-file.ts"');
4646

4747
const logger = new logging.Logger('');
4848
const logs: string[] = [];
@@ -51,7 +51,7 @@ describe('Browser Builder unused files warnings', () => {
5151
const run = await architect.scheduleTarget(targetSpec, undefined, { logger });
5252
const output = (await run.result) as BrowserBuilderOutput;
5353
expect(output.success).toBe(true);
54-
expect(logs.join().includes(`environment.prod.ts ${warningMessageSuffix}`)).toBe(true);
54+
expect(logs.join().includes(`unused-file.ts ${warningMessageSuffix}`)).toBe(true);
5555

5656
await run.stop();
5757
});

packages/angular_devkit/build_angular/src/builders/browser/specs/vendor-source-map_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ describe('Identifying third-party code in source maps', () => {
107107
expect(mainMap.sources.filter((_, i) => !mainMap[IGNORE_LIST].includes(i))).toEqual([
108108
'./src/app/app.component.ts',
109109
'./src/app/app.module.ts',
110-
'./src/environments/environment.ts',
111110
'./src/main.ts',
112111
]);
113112

packages/angular_devkit/build_angular/src/builders/browser/specs/web-worker_spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,8 @@ describe('Browser Builder Web Worker support', () => {
3737
});
3838
`,
3939
'src/main.ts': `
40-
import { enableProdMode } from '@angular/core';
4140
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
4241
import { AppModule } from './app/app.module';
43-
import { environment } from './environments/environment';
44-
if (environment.production) { enableProdMode(); }
4542
platformBrowserDynamic().bootstrapModule(AppModule).catch(err => console.error(err));
4643
4744
const worker = new Worker(new URL('./app/app.worker', import.meta.url), { type: 'module' });

packages/angular_devkit/build_angular/test/hello-world-app/angular.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@
4343
},
4444
"configurations": {
4545
"production": {
46-
"fileReplacements": [
47-
{
48-
"replace": "src/environments/environment.ts",
49-
"with": "src/environments/environment.prod.ts"
50-
}
51-
],
5246
"optimization": true,
5347
"outputHashing": "all",
5448
"sourceMap": false,

packages/angular_devkit/build_angular/test/hello-world-app/src/environments/environment.prod.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/angular_devkit/build_angular/test/hello-world-app/src/environments/environment.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/angular_devkit/build_angular/test/hello-world-app/src/main.server.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,5 @@
77
*/
88

99
import '@angular/localize/init';
10-
import { enableProdMode } from '@angular/core';
11-
12-
import { environment } from './environments/environment';
13-
14-
if (environment.production) {
15-
enableProdMode();
16-
}
1710

1811
export { AppServerModule } from './app/app.server.module';

packages/angular_devkit/build_angular/test/hello-world-app/src/main.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { enableProdMode } from '@angular/core';
109
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
1110

1211
import { AppModule } from './app/app.module';
13-
import { environment } from './environments/environment';
14-
15-
if (environment.production) {
16-
enableProdMode();
17-
}
1812

1913
platformBrowserDynamic()
2014
.bootstrapModule(AppModule)

packages/angular_devkit/build_webpack/test/angular-app/src/environments/environment.prod.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/angular_devkit/build_webpack/test/angular-app/src/environments/environment.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.

packages/angular_devkit/build_webpack/test/angular-app/src/main.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import { enableProdMode } from '@angular/core';
109
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
11-
1210
import { AppModule } from './app/app.module';
13-
import { environment } from './environments/environment';
14-
15-
if (environment.production) {
16-
enableProdMode();
17-
}
1811

1912
platformBrowserDynamic()
2013
.bootstrapModule(AppModule)

packages/schematics/angular/application/files/src/environments/environment.prod.ts.template

Lines changed: 0 additions & 3 deletions
This file was deleted.

packages/schematics/angular/application/files/src/environments/environment.ts.template

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { enableProdMode<% if(!!viewEncapsulation) { %>, ViewEncapsulation<% }%> } from '@angular/core';
1+
<% if(!!viewEncapsulation) { %>import { ViewEncapsulation } from '@angular/core';<% }%>
22
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
33

44
import { AppModule } from './app/app.module';
5-
import { environment } from './environments/environment';
65

7-
if (environment.production) {
8-
enableProdMode();
9-
}
106
<% if(!!viewEncapsulation) { %>
117
platformBrowserDynamic().bootstrapModule(AppModule, {
128
defaultEncapsulation: ViewEncapsulation.<%= viewEncapsulation %>
@@ -15,4 +11,4 @@ platformBrowserDynamic().bootstrapModule(AppModule, {
1511
<% } else { %>
1612
platformBrowserDynamic().bootstrapModule(AppModule)
1713
.catch(err => console.error(err));
18-
<% } %>
14+
<% } %>

packages/schematics/angular/application/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,6 @@ function addAppToWorkspaceFile(
168168
configurations: {
169169
production: {
170170
budgets,
171-
fileReplacements: [
172-
{
173-
replace: `${sourceRoot}/environments/environment.ts`,
174-
with: `${sourceRoot}/environments/environment.prod.ts`,
175-
},
176-
],
177171
outputHashing: 'all',
178172
},
179173
development: {

packages/schematics/angular/application/index_spec.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ describe('Application Schematic', () => {
5555
'/projects/foo/karma.conf.js',
5656
'/projects/foo/tsconfig.app.json',
5757
'/projects/foo/tsconfig.spec.json',
58-
'/projects/foo/src/environments/environment.ts',
59-
'/projects/foo/src/environments/environment.prod.ts',
6058
'/projects/foo/src/favicon.ico',
6159
'/projects/foo/src/index.html',
6260
'/projects/foo/src/main.ts',
@@ -147,7 +145,7 @@ describe('Application Schematic', () => {
147145
const path = '/projects/foo/src/main.ts';
148146
const content = tree.readContent(path);
149147
expect(content).toContain('defaultEncapsulation: ViewEncapsulation.ShadowDom');
150-
expect(content).toContain(`import { enableProdMode, ViewEncapsulation } from '@angular/core'`);
148+
expect(content).toContain(`import { ViewEncapsulation } from '@angular/core'`);
151149
});
152150

153151
it('should set the right paths in the tsconfig.app.json', async () => {
@@ -261,8 +259,6 @@ describe('Application Schematic', () => {
261259
expect(files).toEqual(
262260
jasmine.arrayContaining([
263261
'/projects/foo/tsconfig.app.json',
264-
'/projects/foo/src/environments/environment.ts',
265-
'/projects/foo/src/environments/environment.prod.ts',
266262
'/projects/foo/src/favicon.ico',
267263
'/projects/foo/src/index.html',
268264
'/projects/foo/src/main.ts',
@@ -289,8 +285,6 @@ describe('Application Schematic', () => {
289285
expect(files).toEqual(
290286
jasmine.arrayContaining([
291287
'/projects/foo/tsconfig.app.json',
292-
'/projects/foo/src/environments/environment.ts',
293-
'/projects/foo/src/environments/environment.prod.ts',
294288
'/projects/foo/src/favicon.ico',
295289
'/projects/foo/src/index.html',
296290
'/projects/foo/src/main.ts',
@@ -318,8 +312,6 @@ describe('Application Schematic', () => {
318312
expect(files).toEqual(
319313
jasmine.arrayContaining([
320314
'/projects/foo/tsconfig.app.json',
321-
'/projects/foo/src/environments/environment.ts',
322-
'/projects/foo/src/environments/environment.prod.ts',
323315
'/projects/foo/src/favicon.ico',
324316
'/projects/foo/src/index.html',
325317
'/projects/foo/src/main.ts',
@@ -399,8 +391,6 @@ describe('Application Schematic', () => {
399391
'/karma.conf.js',
400392
'/tsconfig.app.json',
401393
'/tsconfig.spec.json',
402-
'/src/environments/environment.ts',
403-
'/src/environments/environment.prod.ts',
404394
'/src/favicon.ico',
405395
'/src/index.html',
406396
'/src/main.ts',

packages/schematics/angular/service-worker/index.ts

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,7 @@ import {
2222
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2323
import * as ts from '../third_party/github.com/Microsoft/TypeScript/lib/typescript';
2424
import { readWorkspace, writeWorkspace } from '../utility';
25-
import {
26-
addSymbolToNgModuleMetadata,
27-
getEnvironmentExportName,
28-
insertImport,
29-
isImported,
30-
} from '../utility/ast-utils';
25+
import { addSymbolToNgModuleMetadata, insertImport } from '../utility/ast-utils';
3126
import { applyToUpdateRecorder } from '../utility/change';
3227
import { addPackageJsonDependency, getPackageJsonDependency } from '../utility/dependencies';
3328
import { getAppModulePath } from '../utility/ng-ast-utils';
@@ -61,50 +56,19 @@ function updateAppModule(mainPath: string): Rule {
6156
const modulePath = getAppModulePath(host, mainPath);
6257
context.logger.debug(`module path: ${modulePath}`);
6358

64-
// add import
65-
let moduleSource = getTsSourceFile(host, modulePath);
66-
let importModule = 'ServiceWorkerModule';
67-
let importPath = '@angular/service-worker';
68-
if (!isImported(moduleSource, importModule, importPath)) {
69-
const change = insertImport(moduleSource, modulePath, importModule, importPath);
70-
if (change) {
71-
const recorder = host.beginUpdate(modulePath);
72-
applyToUpdateRecorder(recorder, [change]);
73-
host.commitUpdate(recorder);
74-
}
75-
}
76-
77-
// add import for environments
78-
// import { environment } from '../environments/environment';
79-
moduleSource = getTsSourceFile(host, modulePath);
80-
const environmentExportName = getEnvironmentExportName(moduleSource);
81-
// if environemnt import already exists then use the found one
82-
// otherwise use the default name
83-
importModule = environmentExportName || 'environment';
84-
// TODO: dynamically find environments relative path
85-
importPath = '../environments/environment';
86-
87-
if (!environmentExportName) {
88-
// if environment import was not found then insert the new one
89-
// with default path and default export name
90-
const change = insertImport(moduleSource, modulePath, importModule, importPath);
91-
if (change) {
92-
const recorder = host.beginUpdate(modulePath);
93-
applyToUpdateRecorder(recorder, [change]);
94-
host.commitUpdate(recorder);
95-
}
96-
}
59+
addImport(host, modulePath, 'ServiceWorkerModule', '@angular/service-worker');
60+
addImport(host, modulePath, 'isDevMode', '@angular/core');
9761

9862
// register SW in application module
9963
const importText = tags.stripIndent`
10064
ServiceWorkerModule.register('ngsw-worker.js', {
101-
enabled: ${importModule}.production,
65+
enabled: !isDevMode(),
10266
// Register the ServiceWorker as soon as the application is stable
10367
// or after 30 seconds (whichever comes first).
10468
registrationStrategy: 'registerWhenStable:30000'
10569
})
10670
`;
107-
moduleSource = getTsSourceFile(host, modulePath);
71+
const moduleSource = getTsSourceFile(host, modulePath);
10872
const metadataChanges = addSymbolToNgModuleMetadata(
10973
moduleSource,
11074
modulePath,
@@ -172,3 +136,14 @@ export default function (options: ServiceWorkerOptions): Rule {
172136
]);
173137
};
174138
}
139+
140+
function addImport(host: Tree, filePath: string, symbolName: string, moduleName: string): void {
141+
const moduleSource = getTsSourceFile(host, filePath);
142+
const change = insertImport(moduleSource, filePath, symbolName, moduleName);
143+
144+
if (change) {
145+
const recorder = host.beginUpdate(filePath);
146+
applyToUpdateRecorder(recorder, [change]);
147+
host.commitUpdate(recorder);
148+
}
149+
}

0 commit comments

Comments
 (0)