Skip to content

Commit db76dd2

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 d09da83 commit db76dd2

File tree

30 files changed

+68
-388
lines changed

30 files changed

+68
-388
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+
import { <% if(!!viewEncapsulation) { %>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',
@@ -149,7 +147,7 @@ describe('Application Schematic', () => {
149147
const path = '/projects/foo/src/main.ts';
150148
const content = tree.readContent(path);
151149
expect(content).toContain('defaultEncapsulation: ViewEncapsulation.ShadowDom');
152-
expect(content).toContain(`import { enableProdMode, ViewEncapsulation } from '@angular/core'`);
150+
expect(content).toContain(`import { ViewEncapsulation } from '@angular/core'`);
153151
});
154152

155153
it('should set the right paths in the tsconfig.app.json', async () => {
@@ -265,8 +263,6 @@ describe('Application Schematic', () => {
265263
expect(files).toEqual(
266264
jasmine.arrayContaining([
267265
'/projects/foo/tsconfig.app.json',
268-
'/projects/foo/src/environments/environment.ts',
269-
'/projects/foo/src/environments/environment.prod.ts',
270266
'/projects/foo/src/favicon.ico',
271267
'/projects/foo/src/index.html',
272268
'/projects/foo/src/main.ts',
@@ -295,8 +291,6 @@ describe('Application Schematic', () => {
295291
expect(files).toEqual(
296292
jasmine.arrayContaining([
297293
'/projects/foo/tsconfig.app.json',
298-
'/projects/foo/src/environments/environment.ts',
299-
'/projects/foo/src/environments/environment.prod.ts',
300294
'/projects/foo/src/favicon.ico',
301295
'/projects/foo/src/index.html',
302296
'/projects/foo/src/main.ts',
@@ -326,8 +320,6 @@ describe('Application Schematic', () => {
326320
expect(files).toEqual(
327321
jasmine.arrayContaining([
328322
'/projects/foo/tsconfig.app.json',
329-
'/projects/foo/src/environments/environment.ts',
330-
'/projects/foo/src/environments/environment.prod.ts',
331323
'/projects/foo/src/favicon.ico',
332324
'/projects/foo/src/index.html',
333325
'/projects/foo/src/main.ts',
@@ -408,8 +400,6 @@ describe('Application Schematic', () => {
408400
'/karma.conf.js',
409401
'/tsconfig.app.json',
410402
'/tsconfig.spec.json',
411-
'/src/environments/environment.ts',
412-
'/src/environments/environment.prod.ts',
413403
'/src/favicon.ico',
414404
'/src/index.html',
415405
'/src/main.ts',

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

Lines changed: 16 additions & 28 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,33 +56,26 @@ 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);
59+
// add service-worker import
60+
{
61+
const moduleSource = getTsSourceFile(host, modulePath);
62+
const change = insertImport(
63+
moduleSource,
64+
modulePath,
65+
'ServiceWorkerModule',
66+
'@angular/service-worker',
67+
);
7068
if (change) {
7169
const recorder = host.beginUpdate(modulePath);
7270
applyToUpdateRecorder(recorder, [change]);
7371
host.commitUpdate(recorder);
7472
}
7573
}
7674

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);
75+
// add `isDevMode` import
76+
{
77+
const moduleSource = getTsSourceFile(host, modulePath);
78+
const change = insertImport(moduleSource, modulePath, 'isDevMode', '@angular/core');
9179
if (change) {
9280
const recorder = host.beginUpdate(modulePath);
9381
applyToUpdateRecorder(recorder, [change]);
@@ -98,13 +86,13 @@ function updateAppModule(mainPath: string): Rule {
9886
// register SW in application module
9987
const importText = tags.stripIndent`
10088
ServiceWorkerModule.register('ngsw-worker.js', {
101-
enabled: ${importModule}.production,
89+
enabled: !isDevMode(),
10290
// Register the ServiceWorker as soon as the application is stable
10391
// or after 30 seconds (whichever comes first).
10492
registrationStrategy: 'registerWhenStable:30000'
10593
})
10694
`;
107-
moduleSource = getTsSourceFile(host, modulePath);
95+
const moduleSource = getTsSourceFile(host, modulePath);
10896
const metadataChanges = addSymbolToNgModuleMetadata(
10997
moduleSource,
11098
modulePath,

0 commit comments

Comments
 (0)