Skip to content

Commit 8bb8e49

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 728d7b0 commit 8bb8e49

File tree

7 files changed

+31
-4
lines changed

7 files changed

+31
-4
lines changed

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(experimental-zoneless) { %>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(experimental-zoneless) { %>providers: [provideExperimentalZonelessChangeDetection()],} %>
9+
provideZoneChangeDetection({ eventCoalescing: true })<% if (routing) { %>, provideRouter(routes)<% } %>]
810
};

packages/schematics/angular/application/index.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ import {
2525
} from '@angular-devkit/schematics';
2626
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks';
2727
import { Schema as ComponentOptions } from '../component/schema';
28-
import { NodeDependencyType, addPackageJsonDependency } from '../utility/dependencies';
28+
import {
29+
NodeDependencyType,
30+
addPackageJsonDependency,
31+
removePackageJsonDependency,
32+
} from '../utility/dependencies';
2933
import { latestVersions } from '../utility/latest-versions';
3034
import { relativePathToWorkspaceRoot } from '../utility/paths';
3135
import { getWorkspace, updateWorkspace } from '../utility/workspace';
@@ -132,6 +136,9 @@ function addDependenciesToPackageJson(options: ApplicationOptions) {
132136
if (!options.skipInstall) {
133137
context.addTask(new NodePackageInstallTask());
134138
}
139+
if (options.experimentalZoneless) {
140+
removePackageJsonDependency(host, 'zone.js');
141+
}
135142

136143
return host;
137144
};
@@ -239,7 +246,7 @@ function addAppToWorkspaceFile(
239246
outputPath: `dist/${folderName}`,
240247
index: `${sourceRoot}/index.html`,
241248
browser: `${sourceRoot}/main.ts`,
242-
polyfills: ['zone.js'],
249+
polyfills: options.experimentalZoneless ? [] : ['zone.js'],
243250
tsConfig: `${projectRoot}tsconfig.app.json`,
244251
inlineStyleLanguage,
245252
assets: [{ 'glob': '**/*', 'input': `${projectRoot}public` }],
@@ -279,7 +286,7 @@ function addAppToWorkspaceFile(
279286
: {
280287
builder: Builders.Karma,
281288
options: {
282-
polyfills: ['zone.js', 'zone.js/testing'],
289+
polyfills: options.experimentalZoneless ? [] : ['zone.js', 'zone.js/testing'],
283290
tsConfig: `${projectRoot}tsconfig.spec.json`,
284291
inlineStyleLanguage,
285292
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 without dependencies related to zone.js (zoneless)",
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/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 without dependencies related to zone.js (zoneless)",
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)