From 4f20da4f47bbdc3e05816636d5f0347e2d597e98 Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Wed, 21 Sep 2022 08:43:40 +0000 Subject: [PATCH] feat(@schematics/angular): drop `polyfills.ts` file from new templates With this change we drop the `polyfills.ts` from new application projects and add the polyfills directly in the `angular.json`. This is possible as now the `polyfills` option accept an array of module specifiers. This change also fixes another open issue (#14432) which was caused by the missing polyfills file in the library test setup. Closes #14432 --- .../files/src/polyfills.ts.template | 53 ------------------- .../application/files/src/test.ts.template | 2 +- .../files/tsconfig.app.json.template | 3 +- .../files/tsconfig.spec.json.template | 3 +- .../schematics/angular/application/index.ts | 4 +- .../angular/application/index_spec.ts | 15 ++---- .../library/files/src/test.ts.template | 3 +- packages/schematics/angular/library/index.ts | 1 + .../e2e/tests/build/disk-cache-purge.ts | 1 - .../legacy-cli/e2e/tests/build/disk-cache.ts | 1 - 10 files changed, 12 insertions(+), 74 deletions(-) delete mode 100644 packages/schematics/angular/application/files/src/polyfills.ts.template diff --git a/packages/schematics/angular/application/files/src/polyfills.ts.template b/packages/schematics/angular/application/files/src/polyfills.ts.template deleted file mode 100644 index 429bb9ef2d34..000000000000 --- a/packages/schematics/angular/application/files/src/polyfills.ts.template +++ /dev/null @@ -1,53 +0,0 @@ -/** - * This file includes polyfills needed by Angular and is loaded before the app. - * You can add your own extra polyfills to this file. - * - * This file is divided into 2 sections: - * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. - * 2. Application imports. Files imported after ZoneJS that should be loaded before your main - * file. - * - * The current setup is for so-called "evergreen" browsers; the last versions of browsers that - * automatically update themselves. This includes recent versions of Safari, Chrome (including - * Opera), Edge on the desktop, and iOS and Chrome on mobile. - * - * Learn more in https://angular.io/guide/browser-support - */ - -/*************************************************************************************************** - * BROWSER POLYFILLS - */ - -/** - * By default, zone.js will patch all possible macroTask and DomEvents - * user can disable parts of macroTask/DomEvents patch by setting following flags - * because those flags need to be set before `zone.js` being loaded, and webpack - * will put import in the top of bundle, so user need to create a separate file - * in this directory (for example: zone-flags.ts), and put the following flags - * into that file, and then add the following code before importing zone.js. - * import './zone-flags'; - * - * The flags allowed in zone-flags.ts are listed here. - * - * The following flags will work for all browsers. - * - * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame - * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick - * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames - * - * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js - * with the following flag, it will bypass `zone.js` patch for IE/Edge - * - * (window as any).__Zone_enable_cross_context_check = true; - * - */ - -/*************************************************************************************************** - * Zone JS is required by default for Angular itself. - */ -import 'zone.js'; // Included with Angular CLI. - - -/*************************************************************************************************** - * APPLICATION IMPORTS - */ diff --git a/packages/schematics/angular/application/files/src/test.ts.template b/packages/schematics/angular/application/files/src/test.ts.template index cac5aa3ea352..1ea234c664dd 100644 --- a/packages/schematics/angular/application/files/src/test.ts.template +++ b/packages/schematics/angular/application/files/src/test.ts.template @@ -1,6 +1,6 @@ // This file is required by karma.conf.js and loads recursively all the .spec and framework files - import 'zone.js/testing'; + import { getTestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, diff --git a/packages/schematics/angular/application/files/tsconfig.app.json.template b/packages/schematics/angular/application/files/tsconfig.app.json.template index 1966d893bf6d..7e2267cbe2d5 100644 --- a/packages/schematics/angular/application/files/tsconfig.app.json.template +++ b/packages/schematics/angular/application/files/tsconfig.app.json.template @@ -6,8 +6,7 @@ "types": [] }, "files": [ - "src/main.ts", - "src/polyfills.ts" + "src/main.ts" ], "include": [ "src/**/*.d.ts" diff --git a/packages/schematics/angular/application/files/tsconfig.spec.json.template b/packages/schematics/angular/application/files/tsconfig.spec.json.template index 0e856e624157..c9c241170103 100644 --- a/packages/schematics/angular/application/files/tsconfig.spec.json.template +++ b/packages/schematics/angular/application/files/tsconfig.spec.json.template @@ -8,8 +8,7 @@ ] }, "files": [ - "src/test.ts", - "src/polyfills.ts" + "src/test.ts" ], "include": [ "src/**/*.spec.ts", diff --git a/packages/schematics/angular/application/index.ts b/packages/schematics/angular/application/index.ts index 88485f9cc3ff..72e73b1ac0e1 100644 --- a/packages/schematics/angular/application/index.ts +++ b/packages/schematics/angular/application/index.ts @@ -158,7 +158,7 @@ function addAppToWorkspaceFile( outputPath: `dist/${folderName}`, index: `${sourceRoot}/index.html`, main: `${sourceRoot}/main.ts`, - polyfills: `${sourceRoot}/polyfills.ts`, + polyfills: ['zone.js'], tsConfig: `${projectRoot}tsconfig.app.json`, inlineStyleLanguage, assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`], @@ -211,7 +211,7 @@ function addAppToWorkspaceFile( builder: Builders.Karma, options: { main: `${sourceRoot}/test.ts`, - polyfills: `${sourceRoot}/polyfills.ts`, + polyfills: ['zone.js'], tsConfig: `${projectRoot}tsconfig.spec.json`, karmaConfig: `${projectRoot}karma.conf.js`, inlineStyleLanguage, diff --git a/packages/schematics/angular/application/index_spec.ts b/packages/schematics/angular/application/index_spec.ts index 0e1bfc6f0665..7055f485e7ca 100644 --- a/packages/schematics/angular/application/index_spec.ts +++ b/packages/schematics/angular/application/index_spec.ts @@ -60,7 +60,6 @@ describe('Application Schematic', () => { '/projects/foo/src/favicon.ico', '/projects/foo/src/index.html', '/projects/foo/src/main.ts', - '/projects/foo/src/polyfills.ts', '/projects/foo/src/styles.css', '/projects/foo/src/test.ts', '/projects/foo/src/app/app.module.ts', @@ -157,7 +156,7 @@ describe('Application Schematic', () => { .runSchematicAsync('application', defaultOptions, workspaceTree) .toPromise(); const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.app.json'); - expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']); + expect(files).toEqual(['src/main.ts']); expect(_extends).toBe('../../tsconfig.json'); }); @@ -166,7 +165,7 @@ describe('Application Schematic', () => { .runSchematicAsync('application', defaultOptions, workspaceTree) .toPromise(); const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.spec.json'); - expect(files).toEqual(['src/test.ts', 'src/polyfills.ts']); + expect(files).toEqual(['src/test.ts']); expect(_extends).toBe('../../tsconfig.json'); }); @@ -270,7 +269,6 @@ describe('Application Schematic', () => { '/projects/foo/src/favicon.ico', '/projects/foo/src/index.html', '/projects/foo/src/main.ts', - '/projects/foo/src/polyfills.ts', '/projects/foo/src/styles.css', '/projects/foo/src/app/app.module.ts', '/projects/foo/src/app/app.component.ts', @@ -300,7 +298,6 @@ describe('Application Schematic', () => { '/projects/foo/src/favicon.ico', '/projects/foo/src/index.html', '/projects/foo/src/main.ts', - '/projects/foo/src/polyfills.ts', '/projects/foo/src/styles.css', '/projects/foo/src/app/app.module.ts', '/projects/foo/src/app/app.component.css', @@ -331,7 +328,6 @@ describe('Application Schematic', () => { '/projects/foo/src/favicon.ico', '/projects/foo/src/index.html', '/projects/foo/src/main.ts', - '/projects/foo/src/polyfills.ts', '/projects/foo/src/styles.css', '/projects/foo/src/app/app.module.ts', '/projects/foo/src/app/app.component.html', @@ -413,7 +409,6 @@ describe('Application Schematic', () => { '/src/favicon.ico', '/src/index.html', '/src/main.ts', - '/src/polyfills.ts', '/src/styles.css', '/src/test.ts', '/src/app/app.module.ts', @@ -437,7 +432,7 @@ describe('Application Schematic', () => { const buildOpt = prj.architect.build.options; expect(buildOpt.index).toEqual('src/index.html'); expect(buildOpt.main).toEqual('src/main.ts'); - expect(buildOpt.polyfills).toEqual('src/polyfills.ts'); + expect(buildOpt.polyfills).toEqual(['zone.js']); expect(buildOpt.tsConfig).toEqual('tsconfig.app.json'); const testOpt = prj.architect.test.options; @@ -515,7 +510,7 @@ describe('Application Schematic', () => { expect(appTsConfig.extends).toEqual('./tsconfig.json'); const specTsConfig = readJsonFile(tree, '/tsconfig.spec.json'); expect(specTsConfig.extends).toEqual('./tsconfig.json'); - expect(specTsConfig.files).toEqual(['src/test.ts', 'src/polyfills.ts']); + expect(specTsConfig.files).toEqual(['src/test.ts']); }); it(`should create correct paths when 'newProjectRoot' is blank`, async () => { @@ -532,7 +527,7 @@ describe('Application Schematic', () => { const buildOpt = project.architect.build.options; expect(buildOpt.index).toEqual('foo/src/index.html'); expect(buildOpt.main).toEqual('foo/src/main.ts'); - expect(buildOpt.polyfills).toEqual('foo/src/polyfills.ts'); + expect(buildOpt.polyfills).toEqual(['zone.js']); expect(buildOpt.tsConfig).toEqual('foo/tsconfig.app.json'); const appTsConfig = readJsonFile(tree, '/foo/tsconfig.app.json'); diff --git a/packages/schematics/angular/library/files/src/test.ts.template b/packages/schematics/angular/library/files/src/test.ts.template index 29e4db3829fe..1ea234c664dd 100644 --- a/packages/schematics/angular/library/files/src/test.ts.template +++ b/packages/schematics/angular/library/files/src/test.ts.template @@ -1,7 +1,6 @@ // This file is required by karma.conf.js and loads recursively all the .spec and framework files - -import 'zone.js'; import 'zone.js/testing'; + import { getTestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, diff --git a/packages/schematics/angular/library/index.ts b/packages/schematics/angular/library/index.ts index a6959bdf70cb..f9df2f1d2131 100644 --- a/packages/schematics/angular/library/index.ts +++ b/packages/schematics/angular/library/index.ts @@ -110,6 +110,7 @@ function addLibToWorkspaceFile( options: { main: `${projectRoot}/src/test.ts`, tsConfig: `${projectRoot}/tsconfig.spec.json`, + polyfills: ['zone.js'], karmaConfig: `${projectRoot}/karma.conf.js`, }, }, diff --git a/tests/legacy-cli/e2e/tests/build/disk-cache-purge.ts b/tests/legacy-cli/e2e/tests/build/disk-cache-purge.ts index 5cc3b6d0606c..bba2ad7e826b 100644 --- a/tests/legacy-cli/e2e/tests/build/disk-cache-purge.ts +++ b/tests/legacy-cli/e2e/tests/build/disk-cache-purge.ts @@ -9,7 +9,6 @@ export default async function () { // No need to include all applications code to verify disk cache existence. await writeFile('src/main.ts', 'console.log(1);'); - await writeFile('src/polyfills.ts', 'console.log(1);'); // Enable cache for all environments await updateJsonFile('angular.json', (config) => { diff --git a/tests/legacy-cli/e2e/tests/build/disk-cache.ts b/tests/legacy-cli/e2e/tests/build/disk-cache.ts index 559313163187..1873905646ca 100644 --- a/tests/legacy-cli/e2e/tests/build/disk-cache.ts +++ b/tests/legacy-cli/e2e/tests/build/disk-cache.ts @@ -10,7 +10,6 @@ export default async function () { // No need to include all applications code to verify disk cache existence. await writeFile('src/main.ts', 'console.log(1);'); - await writeFile('src/polyfills.ts', 'console.log(1);'); try { // Should be enabled by default.