Skip to content

Commit 2381fc4

Browse files
committed
feat(@schematics/angular): Applications are zoneless by default
This change updates applications to omit the ZoneJS dependency by default. It depends on angular/angular#62655, which allows us to also exclude the `provideZonelessChangeDetection` provider.
1 parent 331c85d commit 2381fc4

File tree

9 files changed

+19
-69
lines changed

9 files changed

+19
-69
lines changed

packages/schematics/angular/application/files/module-files/src/app/app-module.ts.template

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { NgModule, provideBrowserGlobalErrorListeners<% if(zoneless) { %>, provideZonelessChangeDetection<% } %> } from '@angular/core';
1+
import { NgModule, provideBrowserGlobalErrorListeners } from '@angular/core';
22
import { BrowserModule } from '@angular/platform-browser';
33
<% if (routing) { %>
44
import { AppRoutingModule } from './app-routing-module';<% } %>
@@ -13,8 +13,7 @@ import { App } from './app';
1313
AppRoutingModule<% } %>
1414
],
1515
providers: [
16-
provideBrowserGlobalErrorListeners()<% if (zoneless) { %>,
17-
provideZonelessChangeDetection()<% } %>
16+
provideBrowserGlobalErrorListeners()
1817
],
1918
bootstrap: [App]
2019
})

packages/schematics/angular/application/files/module-files/src/app/app.spec.ts.template

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<% if(zoneless) { %>import { provideZonelessChangeDetection } from '@angular/core';
2-
<% } %>import { TestBed } from '@angular/core/testing';<% if (routing) { %>
1+
import { TestBed } from '@angular/core/testing';<% if (routing) { %>
32
import { RouterModule } from '@angular/router';<% } %>
43
import { App } from './app';
54

@@ -11,8 +10,7 @@ describe('App', () => {
1110
],<% } %>
1211
declarations: [
1312
App
14-
],<% if(zoneless) { %>
15-
providers: [provideZonelessChangeDetection()]<% } %>
13+
],
1614
}).compileComponents();
1715
});
1816

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { ApplicationConfig, provideBrowserGlobalErrorListeners, <% if(!zoneless) { %>provideZoneChangeDetection<% } else { %>provideZonelessChangeDetection<% } %> } from '@angular/core';<% if (routing) { %>
1+
import { ApplicationConfig, provideBrowserGlobalErrorListeners<% if(!zoneless) { %>, provideZoneChangeDetection<% } %> } from '@angular/core';<% if (routing) { %>
22
import { provideRouter } from '@angular/router';
33

44
import { routes } from './app.routes';<% } %>
55

66
export const appConfig: ApplicationConfig = {
77
providers: [
8-
provideBrowserGlobalErrorListeners(),
9-
<% if(zoneless) { %>provideZonelessChangeDetection()<% } else { %>provideZoneChangeDetection({ eventCoalescing: true })<% } %>,
8+
provideBrowserGlobalErrorListeners(),<% if(!zoneless) { %>
9+
provideZoneChangeDetection({ eventCoalescing: true }),<% } %>
1010
<% if (routing) {%>provideRouter(routes)<% } %>
1111
]
1212
};

packages/schematics/angular/application/files/standalone-files/src/app/app.spec.ts.template

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
<% if(zoneless) { %>import { provideZonelessChangeDetection } from '@angular/core';
2-
<% } %>import { TestBed } from '@angular/core/testing';
1+
import { TestBed } from '@angular/core/testing';
32
import { App } from './app';
43

54
describe('App', () => {
65
beforeEach(async () => {
76
await TestBed.configureTestingModule({
8-
imports: [App],<% if(zoneless) { %>
9-
providers: [provideZonelessChangeDetection()]<% } %>
7+
imports: [App],
108
}).compileComponents();
119
});
1210

packages/schematics/angular/application/index_spec.ts

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ describe('Application Schematic', () => {
296296
expect(pkg.devDependencies['less']).toEqual(latestVersions['less']);
297297
});
298298

299-
it('should include zone.js if "zoneless" option is not present', async () => {
299+
it('should _not_ include zone.js if "zoneless" option is not present', async () => {
300300
const tree = await schematicRunner.runSchematic(
301301
'application',
302302
{
@@ -307,7 +307,7 @@ describe('Application Schematic', () => {
307307
);
308308

309309
const pkg = JSON.parse(tree.readContent('/package.json'));
310-
expect(pkg.dependencies['zone.js']).toEqual(latestVersions['zone.js']);
310+
expect(pkg.dependencies['zone.js']).toBeUndefined();
311311
});
312312

313313
it('should not include zone.js if "zoneless" option is true', async () => {
@@ -800,7 +800,7 @@ describe('Application Schematic', () => {
800800
);
801801
});
802802

803-
it('should add provideZonelessChangeDetection() in app-module.ts when zoneless is true', async () => {
803+
it('should not add provideZonelessChangeDetection() in app-module.ts when zoneless is true', async () => {
804804
const tree = await schematicRunner.runSchematic(
805805
'application',
806806
{
@@ -812,53 +812,10 @@ describe('Application Schematic', () => {
812812
);
813813
const path = '/projects/foo/src/app/app-module.ts';
814814
const fileContent = tree.readContent(path);
815-
expect(fileContent).toContain('provideZonelessChangeDetection()');
816-
});
817-
818-
it('should not add provideZonelessChangeDetection() in app-module.ts when zoneless is false', async () => {
819-
const tree = await schematicRunner.runSchematic(
820-
'application',
821-
{
822-
...defaultOptions,
823-
zoneless: false,
824-
standalone: false,
825-
},
826-
workspaceTree,
827-
);
828-
const path = '/projects/foo/src/app/app-module.ts';
829-
const fileContent = tree.readContent(path);
830-
expect(fileContent).not.toContain('provideZonelessChangeDetection()');
831-
});
832-
833-
it('should add provideZonelessChangeDetection() when zoneless is true', async () => {
834-
const tree = await schematicRunner.runSchematic(
835-
'application',
836-
{
837-
...defaultOptions,
838-
zoneless: true,
839-
},
840-
workspaceTree,
841-
);
842-
const path = '/projects/foo/src/app/app.config.ts';
843-
const fileContent = tree.readContent(path);
844-
expect(fileContent).toContain('provideZonelessChangeDetection()');
845-
});
846-
847-
it('should not add provideZonelessChangeDetection() when zoneless is false', async () => {
848-
const tree = await schematicRunner.runSchematic(
849-
'application',
850-
{
851-
...defaultOptions,
852-
zoneless: false,
853-
},
854-
workspaceTree,
855-
);
856-
const path = '/projects/foo/src/app/app.config.ts';
857-
const fileContent = tree.readContent(path);
858815
expect(fileContent).not.toContain('provideZonelessChangeDetection()');
859816
});
860817

861-
it('should not add provideZoneChangeDetection when zoneless is true', async () => {
818+
it('should not add any change detection provider when zoneless is true', async () => {
862819
const tree = await schematicRunner.runSchematic(
863820
'application',
864821
{
@@ -869,7 +826,7 @@ describe('Application Schematic', () => {
869826
);
870827
const path = '/projects/foo/src/app/app.config.ts';
871828
const fileContent = tree.readContent(path);
872-
expect(fileContent).not.toContain('provideZoneChangeDetection');
829+
expect(fileContent).not.toMatch(/provideZone(less)?ChangeDetection/gi);
873830
});
874831
});
875832
});

packages/schematics/angular/application/schema.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@
120120
},
121121
"zoneless": {
122122
"description": "Generate an application that does not use `zone.js`.",
123-
"x-prompt": "Do you want to create a 'zoneless' application without zone.js (Developer Preview)?",
124123
"type": "boolean",
125-
"default": false
124+
"default": true
126125
}
127126
},
128127
"required": ["name"]

packages/schematics/angular/ng-new/schema.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,8 @@
141141
},
142142
"zoneless": {
143143
"description": "Create an initial application that does not utilize `zone.js`.",
144-
"x-prompt": "Do you want to create a 'zoneless' application without zone.js (Developer Preview)?",
145144
"type": "boolean",
146-
"default": false
145+
"default": true
147146
}
148147
},
149148
"required": ["name", "version"]

tests/legacy-cli/e2e/tests/generate/application/application-basic.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ng } from '../../../utils/process';
33
import { useCIChrome } from '../../../utils/project';
44

55
export default function () {
6-
return ng('generate', 'application', 'app2')
6+
return ng('generate', 'application', 'app2', '--no-zoneless')
77
.then(() => expectFileToMatch('angular.json', /\"app2\":/))
88
.then(() => useCIChrome('app2', 'projects/app2'))
99
.then(() => ng('test', 'app2', '--watch=false'));

tests/legacy-cli/e2e/tests/generate/application/application-zoneless.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { ng } from '../../../utils/process';
22
import { useCIChrome } from '../../../utils/project';
33

44
export default async function () {
5-
await ng('generate', 'app', 'standalone', '--zoneless', '--standalone');
5+
await ng('generate', 'app', 'standalone', '--standalone');
66
await useCIChrome('standalone', 'projects/standalone');
77
await ng('test', 'standalone', '--watch=false');
88
await ng('build', 'standalone');
99

10-
await ng('generate', 'app', 'ngmodules', '--zoneless', '--no-standalone', '--skip-install');
10+
await ng('generate', 'app', 'ngmodules', '--no-standalone', '--skip-install');
1111
await useCIChrome('ngmodules', 'projects/ngmodules');
1212
await ng('test', 'ngmodules', '--watch=false');
1313
await ng('build', 'ngmodules');

0 commit comments

Comments
 (0)