Skip to content

Commit 68ce3fb

Browse files
committed
feat(@schematics/angular): add option to setup new workspace or application as zoneless mode
Introduces option `--experimental-zoneless` to setup workspace or application as zoneless mode.
1 parent c0b76e3 commit 68ce3fb

File tree

9 files changed

+38
-9
lines changed

9 files changed

+38
-9
lines changed

LICENSE

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License
22

3-
Copyright (c) 2017 Google, Inc.
3+
Copyright (c) 2010-2024 Google LLC. https://angular.dev/license
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
99
copies of the Software, and to permit persons to whom the Software is
1010
furnished to do so, subject to the following conditions:
1111

12-
The above copyright notice and this permission notice shall be included in all
13-
copies or substantial portions of the Software.
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
1414

1515
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1616
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1717
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1818
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1919
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21-
SOFTWARE.
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { AppModule } from './app/app.module';
55

66
platformBrowserDynamic().bootstrapModule(AppModule, {
7+
<% if(experimentalZoneless) { %>providers: [provideExperimentalZonelessChangeDetection()],} %>
78
ngZoneEventCoalescing: true<% if(!!viewEncapsulation) { %>,
89
defaultEncapsulation: ViewEncapsulation.<%= viewEncapsulation %><% } %>
910
})

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@ import { provideRouter } from '@angular/router';
44
import { routes } from './app.routes';<% } %>
55

66
export const appConfig: ApplicationConfig = {
7-
providers: [provideZoneChangeDetection({ eventCoalescing: true })<% if (routing) { %>, provideRouter(routes)<% } %>]
7+
providers: [
8+
<% if(experimentalZoneless) { %>providers: [provideExperimentalZonelessChangeDetection()],} %>
9+
provideZoneChangeDetection({ eventCoalescing: true })<% if (routing) { %>, provideRouter(routes)<% } %>]
810
};

packages/schematics/angular/application/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ function addAppToWorkspaceFile(
239239
outputPath: `dist/${folderName}`,
240240
index: `${sourceRoot}/index.html`,
241241
browser: `${sourceRoot}/main.ts`,
242-
polyfills: ['zone.js'],
242+
polyfills: options.experimentalZoneless ? [] : ['zone.js'],
243243
tsConfig: `${projectRoot}tsconfig.app.json`,
244244
inlineStyleLanguage,
245245
assets: [{ 'glob': '**/*', 'input': `${projectRoot}public` }],
@@ -279,7 +279,7 @@ function addAppToWorkspaceFile(
279279
: {
280280
builder: Builders.Karma,
281281
options: {
282-
polyfills: ['zone.js', 'zone.js/testing'],
282+
polyfills: options.experimentalZoneless ? [] : ['zone.js', 'zone.js/testing'],
283283
tsConfig: `${projectRoot}tsconfig.spec.json`,
284284
inlineStyleLanguage,
285285
assets: [{ 'glob': '**/*', 'input': `${projectRoot}public` }],

packages/schematics/angular/application/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@
117117
"type": "boolean",
118118
"default": false,
119119
"x-user-analytics": "ep.ng_ssr"
120+
},
121+
"experimentalZoneless": {
122+
"description": "Create an application that does not utilize zone.js.",
123+
"type": "boolean",
124+
"default": false
120125
}
121126
},
122127
"required": ["name"]

packages/schematics/angular/ng-new/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default function (options: NgNewOptions): Rule {
3939
minimal: options.minimal,
4040
strict: options.strict,
4141
packageManager: options.packageManager,
42+
experimentalZoneless: options.experimentalZoneless,
4243
};
4344
const applicationOptions: ApplicationOptions = {
4445
projectRoot: '',
@@ -57,6 +58,7 @@ export default function (options: NgNewOptions): Rule {
5758
minimal: options.minimal,
5859
standalone: options.standalone,
5960
ssr: options.ssr,
61+
experimentalZoneless: options.experimentalZoneless,
6062
};
6163

6264
return chain([

packages/schematics/angular/ng-new/index_spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,21 @@ describe('Ng New Schematic', () => {
9595
expect(confContent.projects.foo.e2e).toBeUndefined();
9696
});
9797

98-
it('should add packageManager option in angular.json', async () => {
98+
fit('should add packageManager option in angular.json', async () => {
9999
const tree = await schematicRunner.runSchematic('ng-new', {
100100
...defaultOptions,
101101
packageManager: 'npm',
102102
});
103103
const { cli } = JSON.parse(tree.readContent('/bar/angular.json'));
104104
expect(cli.packageManager).toBe('npm');
105105
});
106+
107+
it('should add provideExperimentalZonelessChangeDetection in app.config', async () => {
108+
const tree = await schematicRunner.runSchematic('ng-new', {
109+
...defaultOptions,
110+
experimentalZoneless: true,
111+
});
112+
const fileContent = tree.readContent('/foo/src/app/app.config.ts');
113+
expect(fileContent).toContain('provideExperimentalZonelessChangeDetection()');
114+
});
106115
});

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@
138138
"description": "Creates an application with Server-Side Rendering (SSR) and Static Site Generation (SSG/Prerendering) enabled.",
139139
"type": "boolean",
140140
"x-user-analytics": "ep.ng_ssr"
141+
},
142+
"experimentalZoneless": {
143+
"description": "Create an application that does not utilize zone.js.",
144+
"type": "boolean",
145+
"default": false
141146
}
142147
},
143148
"required": ["name", "version"]

packages/schematics/angular/workspace/schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
"description": "The package manager used to install dependencies.",
4242
"type": "string",
4343
"enum": ["npm", "yarn", "pnpm", "cnpm", "bun"]
44+
},
45+
"experimentalZoneless": {
46+
"description": "Create an application without dependencies related to zone.js (zoneless)",
47+
"type": "boolean",
48+
"default": false
4449
}
4550
},
4651
"required": ["name", "version"]

0 commit comments

Comments
 (0)