Skip to content

feat(@schematics/angular): Applications are zoneless by default #30718

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModule, provideBrowserGlobalErrorListeners<% if(zoneless) { %>, provideZonelessChangeDetection<% } %> } from '@angular/core';
import { NgModule, provideBrowserGlobalErrorListeners } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
<% if (routing) { %>
import { AppRoutingModule } from './app-routing-module';<% } %>
Expand All @@ -13,8 +13,7 @@ import { App } from './app';
AppRoutingModule<% } %>
],
providers: [
provideBrowserGlobalErrorListeners()<% if (zoneless) { %>,
provideZonelessChangeDetection()<% } %>
provideBrowserGlobalErrorListeners()
],
bootstrap: [App]
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<% if(zoneless) { %>import { provideZonelessChangeDetection } from '@angular/core';
<% } %>import { TestBed } from '@angular/core/testing';<% if (routing) { %>
import { TestBed } from '@angular/core/testing';<% if (routing) { %>
import { RouterModule } from '@angular/router';<% } %>
import { App } from './app';

Expand All @@ -11,8 +10,7 @@ describe('App', () => {
],<% } %>
declarations: [
App
],<% if(zoneless) { %>
providers: [provideZonelessChangeDetection()]<% } %>
],
}).compileComponents();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ApplicationConfig, provideBrowserGlobalErrorListeners, <% if(!zoneless) { %>provideZoneChangeDetection<% } else { %>provideZonelessChangeDetection<% } %> } from '@angular/core';<% if (routing) { %>
import { ApplicationConfig, provideBrowserGlobalErrorListeners<% if(!zoneless) { %>, provideZoneChangeDetection<% } %> } from '@angular/core';<% if (routing) { %>
import { provideRouter } from '@angular/router';

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

export const appConfig: ApplicationConfig = {
providers: [
provideBrowserGlobalErrorListeners(),
<% if(zoneless) { %>provideZonelessChangeDetection()<% } else { %>provideZoneChangeDetection({ eventCoalescing: true })<% } %>,
provideBrowserGlobalErrorListeners(),<% if(!zoneless) { %>
provideZoneChangeDetection({ eventCoalescing: true }),<% } %>
<% if (routing) {%>provideRouter(routes)<% } %>
]
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<% if(zoneless) { %>import { provideZonelessChangeDetection } from '@angular/core';
<% } %>import { TestBed } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { App } from './app';

describe('App', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [App],<% if(zoneless) { %>
providers: [provideZonelessChangeDetection()]<% } %>
imports: [App],
}).compileComponents();
});

Expand Down
53 changes: 5 additions & 48 deletions packages/schematics/angular/application/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('Application Schematic', () => {
expect(pkg.devDependencies['less']).toEqual(latestVersions['less']);
});

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

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

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

it('should add provideZonelessChangeDetection() in app-module.ts when zoneless is true', async () => {
it('should not add provideZonelessChangeDetection() in app-module.ts when zoneless is true', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
Expand All @@ -812,53 +812,10 @@ describe('Application Schematic', () => {
);
const path = '/projects/foo/src/app/app-module.ts';
const fileContent = tree.readContent(path);
expect(fileContent).toContain('provideZonelessChangeDetection()');
});

it('should not add provideZonelessChangeDetection() in app-module.ts when zoneless is false', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
...defaultOptions,
zoneless: false,
standalone: false,
},
workspaceTree,
);
const path = '/projects/foo/src/app/app-module.ts';
const fileContent = tree.readContent(path);
expect(fileContent).not.toContain('provideZonelessChangeDetection()');
});

it('should add provideZonelessChangeDetection() when zoneless is true', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
...defaultOptions,
zoneless: true,
},
workspaceTree,
);
const path = '/projects/foo/src/app/app.config.ts';
const fileContent = tree.readContent(path);
expect(fileContent).toContain('provideZonelessChangeDetection()');
});

it('should not add provideZonelessChangeDetection() when zoneless is false', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
...defaultOptions,
zoneless: false,
},
workspaceTree,
);
const path = '/projects/foo/src/app/app.config.ts';
const fileContent = tree.readContent(path);
expect(fileContent).not.toContain('provideZonelessChangeDetection()');
});

it('should not add provideZoneChangeDetection when zoneless is true', async () => {
it('should not add any change detection provider when zoneless is true', async () => {
const tree = await schematicRunner.runSchematic(
'application',
{
Expand All @@ -869,7 +826,7 @@ describe('Application Schematic', () => {
);
const path = '/projects/foo/src/app/app.config.ts';
const fileContent = tree.readContent(path);
expect(fileContent).not.toContain('provideZoneChangeDetection');
expect(fileContent).not.toMatch(/provideZone(less)?ChangeDetection/gi);
});
});
});
3 changes: 1 addition & 2 deletions packages/schematics/angular/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@
},
"zoneless": {
"description": "Generate an application that does not use `zone.js`.",
"x-prompt": "Do you want to create a 'zoneless' application without zone.js (Developer Preview)?",
"type": "boolean",
"default": false
"default": true
}
},
"required": ["name"]
Expand Down
3 changes: 1 addition & 2 deletions packages/schematics/angular/ng-new/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@
},
"zoneless": {
"description": "Create an initial application that does not utilize `zone.js`.",
"x-prompt": "Do you want to create a 'zoneless' application without zone.js (Developer Preview)?",
"type": "boolean",
"default": false
"default": true
}
},
"required": ["name", "version"]
Expand Down
2 changes: 1 addition & 1 deletion tests/legacy-cli/e2e/initialize/500-create-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default async function () {
// Ensure local test registry is used when outside a project
await setNPMConfigRegistry(true);

await ng('new', 'test-project', '--skip-install');
await ng('new', 'test-project', '--skip-install', '--no-zoneless');
await expectFileToExist(join(process.cwd(), 'test-project'));
process.chdir('./test-project');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ng } from '../../../utils/process';
import { useCIChrome } from '../../../utils/project';

export default function () {
return ng('generate', 'application', 'app2')
return ng('generate', 'application', 'app2', '--no-zoneless')
.then(() => expectFileToMatch('angular.json', /\"app2\":/))
.then(() => useCIChrome('app2', 'projects/app2'))
.then(() => ng('test', 'app2', '--watch=false'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { ng } from '../../../utils/process';
import { useCIChrome } from '../../../utils/project';

export default async function () {
await ng('generate', 'app', 'standalone', '--zoneless', '--standalone');
await ng('generate', 'app', 'standalone', '--standalone');
await useCIChrome('standalone', 'projects/standalone');
await ng('test', 'standalone', '--watch=false');
await ng('build', 'standalone');

await ng('generate', 'app', 'ngmodules', '--zoneless', '--no-standalone', '--skip-install');
await ng('generate', 'app', 'ngmodules', '--no-standalone', '--skip-install');
await useCIChrome('ngmodules', 'projects/ngmodules');
await ng('test', 'ngmodules', '--watch=false');
await ng('build', 'ngmodules');
Expand Down
Loading