diff --git a/packages/schematics/angular/application/index.ts b/packages/schematics/angular/application/index.ts index 825f98c04158..14c0688cf334 100644 --- a/packages/schematics/angular/application/index.ts +++ b/packages/schematics/angular/application/index.ts @@ -149,6 +149,14 @@ function addDependenciesToPackageJson(options: ApplicationOptions) { }, ].forEach((dependency) => addPackageJsonDependency(host, dependency)); + if (!options.zoneless) { + addPackageJsonDependency(host, { + type: NodeDependencyType.Default, + name: 'zone.js', + version: latestVersions['zone.js'], + }); + } + if (!options.skipInstall) { context.addTask(new NodePackageInstallTask()); } diff --git a/packages/schematics/angular/application/index_spec.ts b/packages/schematics/angular/application/index_spec.ts index 458f91d6eef0..60700c9f45ff 100644 --- a/packages/schematics/angular/application/index_spec.ts +++ b/packages/schematics/angular/application/index_spec.ts @@ -268,6 +268,48 @@ describe('Application Schematic', () => { expect(pkg.devDependencies['typescript']).toEqual(latestVersions['typescript']); }); + it('should include zone.js if "zoneless" option is false', async () => { + const tree = await schematicRunner.runSchematic( + 'application', + { + ...defaultOptions, + zoneless: false, + }, + workspaceTree, + ); + + const pkg = JSON.parse(tree.readContent('/package.json')); + expect(pkg.dependencies['zone.js']).toEqual(latestVersions['zone.js']); + }); + + it('should include zone.js if "zoneless" option is not present', async () => { + const tree = await schematicRunner.runSchematic( + 'application', + { + ...defaultOptions, + zoneless: undefined, + }, + workspaceTree, + ); + + const pkg = JSON.parse(tree.readContent('/package.json')); + expect(pkg.dependencies['zone.js']).toEqual(latestVersions['zone.js']); + }); + + it('should not include zone.js if "zoneless" option is true', async () => { + const tree = await schematicRunner.runSchematic( + 'application', + { + ...defaultOptions, + zoneless: true, + }, + workspaceTree, + ); + + const pkg = JSON.parse(tree.readContent('/package.json')); + expect(pkg.dependencies['zone.js']).toBeUndefined(); + }); + it(`should not override existing users dependencies`, async () => { const oldPackageJson = workspaceTree.readContent('package.json'); workspaceTree.overwrite( diff --git a/packages/schematics/angular/library/index.ts b/packages/schematics/angular/library/index.ts index b409d986dd57..d96cc8b505a8 100644 --- a/packages/schematics/angular/library/index.ts +++ b/packages/schematics/angular/library/index.ts @@ -22,7 +22,11 @@ import { } from '@angular-devkit/schematics'; import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; import { join } from 'node:path/posix'; -import { NodeDependencyType, addPackageJsonDependency } from '../utility/dependencies'; +import { + NodeDependencyType, + addPackageJsonDependency, + getPackageJsonDependency, +} from '../utility/dependencies'; import { JSONFile } from '../utility/json-file'; import { latestVersions } from '../utility/latest-versions'; import { relativePathToWorkspaceRoot } from '../utility/paths'; @@ -96,6 +100,7 @@ function addLibToWorkspaceFile( options: LibraryOptions, projectRoot: string, projectName: string, + hasZoneDependency: boolean, ): Rule { return updateWorkspace((workspace) => { workspace.projects.add({ @@ -121,7 +126,7 @@ function addLibToWorkspaceFile( builder: Builders.BuildKarma, options: { tsConfig: `${projectRoot}/tsconfig.spec.json`, - polyfills: ['zone.js', 'zone.js/testing'], + polyfills: hasZoneDependency ? ['zone.js', 'zone.js/testing'] : undefined, }, }, }, @@ -172,9 +177,11 @@ export default function (options: LibraryOptions): Rule { move(libDir), ]); + const hasZoneDependency = getPackageJsonDependency(host, 'zone.js') !== null; + return chain([ mergeWith(templateSource), - addLibToWorkspaceFile(options, libDir, packageName), + addLibToWorkspaceFile(options, libDir, packageName, hasZoneDependency), options.skipPackageJson ? noop() : addDependenciesToPackageJson(), options.skipTsConfig ? noop() : updateTsConfig(packageName, './' + distRoot), options.skipTsConfig diff --git a/packages/schematics/angular/library/index_spec.ts b/packages/schematics/angular/library/index_spec.ts index 1d5503d70b38..97e517767f1f 100644 --- a/packages/schematics/angular/library/index_spec.ts +++ b/packages/schematics/angular/library/index_spec.ts @@ -195,6 +195,13 @@ describe('Library Schematic', () => { expect(workspace.projects.foo.prefix).toEqual('pre'); }); + it(`should not add zone.js to test polyfills when no zone.js dependency`, async () => { + const tree = await schematicRunner.runSchematic('library', defaultOptions, workspaceTree); + + const workspace = getJsonFileContent(tree, '/angular.json'); + expect(workspace.projects.foo.architect.test.options.polyfills).toBeUndefined(); + }); + it('should handle a pascalCasedName', async () => { const options = { ...defaultOptions, name: 'pascalCasedName' }; const tree = await schematicRunner.runSchematic('library', options, workspaceTree); diff --git a/packages/schematics/angular/workspace/files/package.json.template b/packages/schematics/angular/workspace/files/package.json.template index 4ee0cdd9ab73..dcbe6939829d 100644 --- a/packages/schematics/angular/workspace/files/package.json.template +++ b/packages/schematics/angular/workspace/files/package.json.template @@ -17,8 +17,7 @@ "@angular/platform-browser": "<%= latestVersions.Angular %>", "@angular/router": "<%= latestVersions.Angular %>", "rxjs": "<%= latestVersions['rxjs'] %>", - "tslib": "<%= latestVersions['tslib'] %>", - "zone.js": "<%= latestVersions['zone.js'] %>" + "tslib": "<%= latestVersions['tslib'] %>" }, "devDependencies": { "@angular/cli": "<%= '^' + version %>", diff --git a/packages/schematics/angular/workspace/index_spec.ts b/packages/schematics/angular/workspace/index_spec.ts index 520452e71b89..21efd7275b82 100644 --- a/packages/schematics/angular/workspace/index_spec.ts +++ b/packages/schematics/angular/workspace/index_spec.ts @@ -58,7 +58,6 @@ describe('Workspace Schematic', () => { const pkg = JSON.parse(tree.readContent('/package.json')); expect(pkg.dependencies['@angular/core']).toEqual(latestVersions.Angular); expect(pkg.dependencies['rxjs']).toEqual(latestVersions['rxjs']); - expect(pkg.dependencies['zone.js']).toEqual(latestVersions['zone.js']); expect(pkg.devDependencies['typescript']).toEqual(latestVersions['typescript']); });