Skip to content

Commit 597bfea

Browse files
committed
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
1 parent c592ec5 commit 597bfea

File tree

11 files changed

+35
-79
lines changed

11 files changed

+35
-79
lines changed

packages/angular_devkit/build_angular/src/webpack/configs/common.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,33 @@ export async function getCommonConfig(wco: WebpackConfigOptions): Promise<Config
105105
extraPlugins.push(new ContextReplacementPlugin(/@?hapi|express[\\/]/));
106106
}
107107

108-
if (buildOptions.polyfills.length) {
108+
if (polyfills?.length) {
109+
// `zone.js/testing` is a **special** polyfill because when not imported in the main it fails with the below errors:
110+
// `Error: Expected to be running in 'ProxyZone', but it was not found.`
111+
// This was also the reason why previously it was imported in `test.ts` as the first module.
112+
// From Jia li:
113+
// This is because the jasmine functions such as beforeEach/it will not be patched by zone.js since
114+
// jasmine will not be loaded yet, so the ProxyZone will not be there. We have to load zone-testing.js after
115+
// jasmine is ready.
116+
// We could force loading 'zone.js/testing' prior to jasmine by changing the order of scripts in 'karma-context.html'.
117+
// But this has it's own problems as zone.js needs to be loaded prior to jasmine due to patching of timing functions
118+
// See: https://github.com/jasmine/jasmine/issues/1944
119+
// Thus the correct order is zone.js -> jasmine -> zone.js/testing.
120+
const zoneTestingEntryPoint = 'zone.js/testing';
121+
const polyfillsExludingZoneTesting = polyfills.filter((p) => p !== zoneTestingEntryPoint);
122+
109123
if (Array.isArray(entryPoints['polyfills'])) {
110-
entryPoints['polyfills'].push(...buildOptions.polyfills);
124+
entryPoints['polyfills'].push(...polyfillsExludingZoneTesting);
111125
} else {
112-
entryPoints['polyfills'] = buildOptions.polyfills;
126+
entryPoints['polyfills'] = polyfillsExludingZoneTesting;
127+
}
128+
129+
if (polyfillsExludingZoneTesting.length !== polyfills.length) {
130+
if (Array.isArray(entryPoints['main'])) {
131+
entryPoints['main'].unshift(zoneTestingEntryPoint);
132+
} else {
133+
entryPoints['main'] = [zoneTestingEntryPoint, entryPoints['main'] as string];
134+
}
113135
}
114136
}
115137

packages/schematics/angular/application/files/src/polyfills.ts.template

Lines changed: 0 additions & 53 deletions
This file was deleted.

packages/schematics/angular/application/files/src/test.ts.template

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
2-
3-
import 'zone.js/testing';
42
import { getTestBed } from '@angular/core/testing';
53
import {
64
BrowserDynamicTestingModule,

packages/schematics/angular/application/files/tsconfig.app.json.template

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"types": []
77
},
88
"files": [
9-
"src/main.ts",
10-
"src/polyfills.ts"
9+
"src/main.ts"
1110
],
1211
"include": [
1312
"src/**/*.d.ts"

packages/schematics/angular/application/files/tsconfig.spec.json.template

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
]
99
},
1010
"files": [
11-
"src/test.ts",
12-
"src/polyfills.ts"
11+
"src/test.ts"
1312
],
1413
"include": [
1514
"src/**/*.spec.ts",

packages/schematics/angular/application/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ function addAppToWorkspaceFile(
158158
outputPath: `dist/${folderName}`,
159159
index: `${sourceRoot}/index.html`,
160160
main: `${sourceRoot}/main.ts`,
161-
polyfills: `${sourceRoot}/polyfills.ts`,
161+
polyfills: ['zone.js'],
162162
tsConfig: `${projectRoot}tsconfig.app.json`,
163163
inlineStyleLanguage,
164164
assets: [`${sourceRoot}/favicon.ico`, `${sourceRoot}/assets`],
@@ -211,7 +211,7 @@ function addAppToWorkspaceFile(
211211
builder: Builders.Karma,
212212
options: {
213213
main: `${sourceRoot}/test.ts`,
214-
polyfills: `${sourceRoot}/polyfills.ts`,
214+
polyfills: ['zone.js', 'zone.js/testing'],
215215
tsConfig: `${projectRoot}tsconfig.spec.json`,
216216
karmaConfig: `${projectRoot}karma.conf.js`,
217217
inlineStyleLanguage,

packages/schematics/angular/application/index_spec.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ describe('Application Schematic', () => {
6060
'/projects/foo/src/favicon.ico',
6161
'/projects/foo/src/index.html',
6262
'/projects/foo/src/main.ts',
63-
'/projects/foo/src/polyfills.ts',
6463
'/projects/foo/src/styles.css',
6564
'/projects/foo/src/test.ts',
6665
'/projects/foo/src/app/app.module.ts',
@@ -157,7 +156,7 @@ describe('Application Schematic', () => {
157156
.runSchematicAsync('application', defaultOptions, workspaceTree)
158157
.toPromise();
159158
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.app.json');
160-
expect(files).toEqual(['src/main.ts', 'src/polyfills.ts']);
159+
expect(files).toEqual(['src/main.ts']);
161160
expect(_extends).toBe('../../tsconfig.json');
162161
});
163162

@@ -166,7 +165,7 @@ describe('Application Schematic', () => {
166165
.runSchematicAsync('application', defaultOptions, workspaceTree)
167166
.toPromise();
168167
const { files, extends: _extends } = readJsonFile(tree, '/projects/foo/tsconfig.spec.json');
169-
expect(files).toEqual(['src/test.ts', 'src/polyfills.ts']);
168+
expect(files).toEqual(['src/test.ts']);
170169
expect(_extends).toBe('../../tsconfig.json');
171170
});
172171

@@ -270,7 +269,6 @@ describe('Application Schematic', () => {
270269
'/projects/foo/src/favicon.ico',
271270
'/projects/foo/src/index.html',
272271
'/projects/foo/src/main.ts',
273-
'/projects/foo/src/polyfills.ts',
274272
'/projects/foo/src/styles.css',
275273
'/projects/foo/src/app/app.module.ts',
276274
'/projects/foo/src/app/app.component.ts',
@@ -300,7 +298,6 @@ describe('Application Schematic', () => {
300298
'/projects/foo/src/favicon.ico',
301299
'/projects/foo/src/index.html',
302300
'/projects/foo/src/main.ts',
303-
'/projects/foo/src/polyfills.ts',
304301
'/projects/foo/src/styles.css',
305302
'/projects/foo/src/app/app.module.ts',
306303
'/projects/foo/src/app/app.component.css',
@@ -331,7 +328,6 @@ describe('Application Schematic', () => {
331328
'/projects/foo/src/favicon.ico',
332329
'/projects/foo/src/index.html',
333330
'/projects/foo/src/main.ts',
334-
'/projects/foo/src/polyfills.ts',
335331
'/projects/foo/src/styles.css',
336332
'/projects/foo/src/app/app.module.ts',
337333
'/projects/foo/src/app/app.component.html',
@@ -413,7 +409,6 @@ describe('Application Schematic', () => {
413409
'/src/favicon.ico',
414410
'/src/index.html',
415411
'/src/main.ts',
416-
'/src/polyfills.ts',
417412
'/src/styles.css',
418413
'/src/test.ts',
419414
'/src/app/app.module.ts',
@@ -437,7 +432,7 @@ describe('Application Schematic', () => {
437432
const buildOpt = prj.architect.build.options;
438433
expect(buildOpt.index).toEqual('src/index.html');
439434
expect(buildOpt.main).toEqual('src/main.ts');
440-
expect(buildOpt.polyfills).toEqual('src/polyfills.ts');
435+
expect(buildOpt.polyfills).toEqual(['zone.js']);
441436
expect(buildOpt.tsConfig).toEqual('tsconfig.app.json');
442437

443438
const testOpt = prj.architect.test.options;
@@ -515,7 +510,7 @@ describe('Application Schematic', () => {
515510
expect(appTsConfig.extends).toEqual('./tsconfig.json');
516511
const specTsConfig = readJsonFile(tree, '/tsconfig.spec.json');
517512
expect(specTsConfig.extends).toEqual('./tsconfig.json');
518-
expect(specTsConfig.files).toEqual(['src/test.ts', 'src/polyfills.ts']);
513+
expect(specTsConfig.files).toEqual(['src/test.ts']);
519514
});
520515

521516
it(`should create correct paths when 'newProjectRoot' is blank`, async () => {
@@ -532,7 +527,7 @@ describe('Application Schematic', () => {
532527
const buildOpt = project.architect.build.options;
533528
expect(buildOpt.index).toEqual('foo/src/index.html');
534529
expect(buildOpt.main).toEqual('foo/src/main.ts');
535-
expect(buildOpt.polyfills).toEqual('foo/src/polyfills.ts');
530+
expect(buildOpt.polyfills).toEqual(['zone.js']);
536531
expect(buildOpt.tsConfig).toEqual('foo/tsconfig.app.json');
537532

538533
const appTsConfig = readJsonFile(tree, '/foo/tsconfig.app.json');

packages/schematics/angular/library/files/src/test.ts.template

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
// This file is required by karma.conf.js and loads recursively all the .spec and framework files
2-
3-
import 'zone.js';
4-
import 'zone.js/testing';
52
import { getTestBed } from '@angular/core/testing';
63
import {
74
BrowserDynamicTestingModule,

packages/schematics/angular/library/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ function addLibToWorkspaceFile(
110110
options: {
111111
main: `${projectRoot}/src/test.ts`,
112112
tsConfig: `${projectRoot}/tsconfig.spec.json`,
113+
polyfills: ['zone.js', 'zone.js/testing'],
113114
karmaConfig: `${projectRoot}/karma.conf.js`,
114115
},
115116
},

tests/legacy-cli/e2e/tests/build/disk-cache-purge.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export default async function () {
99

1010
// No need to include all applications code to verify disk cache existence.
1111
await writeFile('src/main.ts', 'console.log(1);');
12-
await writeFile('src/polyfills.ts', 'console.log(1);');
1312

1413
// Enable cache for all environments
1514
await updateJsonFile('angular.json', (config) => {

tests/legacy-cli/e2e/tests/build/disk-cache.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default async function () {
1010

1111
// No need to include all applications code to verify disk cache existence.
1212
await writeFile('src/main.ts', 'console.log(1);');
13-
await writeFile('src/polyfills.ts', 'console.log(1);');
1413

1514
try {
1615
// Should be enabled by default.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy