Skip to content

Commit 6893fc1

Browse files
Alanalexeagle
authored andcommitted
fix(@angular-devkit/build-angular): server build is generating un-needed polyfill file
Fixes #14655
1 parent e3bb367 commit 6893fc1

File tree

2 files changed

+61
-32
lines changed

2 files changed

+61
-32
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,43 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
6666
entryPoints['main'] = [path.resolve(root, buildOptions.main)];
6767
}
6868

69-
const es5Polyfills = path.join(__dirname, '..', 'es5-polyfills.js');
70-
const es5JitPolyfills = path.join(__dirname, '..', 'es5-jit-polyfills.js');
71-
72-
if (targetInFileName) {
73-
// For differential loading we don't need to have 2 polyfill bundles
74-
if (buildOptions.scriptTargetOverride === ScriptTarget.ES2015) {
75-
entryPoints['polyfills'] = [path.join(__dirname, '..', 'safari-nomodule.js')];
76-
} else {
77-
entryPoints['polyfills'] = [es5Polyfills];
78-
if (!buildOptions.aot) {
79-
entryPoints['polyfills'].push(es5JitPolyfills);
69+
if (wco.buildOptions.platform !== 'server') {
70+
const es5Polyfills = path.join(__dirname, '..', 'es5-polyfills.js');
71+
const es5JitPolyfills = path.join(__dirname, '..', 'es5-jit-polyfills.js');
72+
if (targetInFileName) {
73+
// For differential loading we don't need to have 2 polyfill bundles
74+
if (buildOptions.scriptTargetOverride === ScriptTarget.ES2015) {
75+
entryPoints['polyfills'] = [path.join(__dirname, '..', 'safari-nomodule.js')];
76+
} else {
77+
entryPoints['polyfills'] = [es5Polyfills];
78+
if (!buildOptions.aot) {
79+
entryPoints['polyfills'].push(es5JitPolyfills);
80+
}
8081
}
81-
}
82-
} else {
83-
// For NON differential loading we want to have 2 polyfill bundles
84-
if (buildOptions.es5BrowserSupport
85-
|| (buildOptions.es5BrowserSupport === undefined && isEs5SupportNeeded(projectRoot))) {
86-
entryPoints['polyfills-es5'] = [es5Polyfills];
87-
if (!buildOptions.aot) {
88-
entryPoints['polyfills-es5'].push(es5JitPolyfills);
82+
} else {
83+
// For NON differential loading we want to have 2 polyfill bundles
84+
if (buildOptions.es5BrowserSupport
85+
|| (buildOptions.es5BrowserSupport === undefined && isEs5SupportNeeded(projectRoot))) {
86+
entryPoints['polyfills-es5'] = [es5Polyfills];
87+
if (!buildOptions.aot) {
88+
entryPoints['polyfills-es5'].push(es5JitPolyfills);
89+
}
8990
}
9091
}
91-
}
9292

93-
if (buildOptions.polyfills) {
94-
entryPoints['polyfills'] = [
95-
...(entryPoints['polyfills'] || []),
96-
path.resolve(root, buildOptions.polyfills),
97-
];
98-
}
93+
if (buildOptions.polyfills) {
94+
entryPoints['polyfills'] = [
95+
...(entryPoints['polyfills'] || []),
96+
path.resolve(root, buildOptions.polyfills),
97+
];
98+
}
9999

100-
if (!buildOptions.aot) {
101-
entryPoints['polyfills'] = [
102-
...(entryPoints['polyfills'] || []),
103-
path.join(__dirname, '..', 'jit-polyfills.js'),
104-
];
100+
if (!buildOptions.aot) {
101+
entryPoints['polyfills'] = [
102+
...(entryPoints['polyfills'] || []),
103+
path.join(__dirname, '..', 'jit-polyfills.js'),
104+
];
105+
}
105106
}
106107

107108
if (buildOptions.profile || process.env['NG_BUILD_PROFILING']) {

packages/angular_devkit/build_angular/test/server/base_spec_large.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { Architect } from '@angular-devkit/architect';
10-
import { join, normalize, virtualFs } from '@angular-devkit/core';
10+
import { getSystemPath, join, normalize, virtualFs } from '@angular-devkit/core';
1111
import { take, tap } from 'rxjs/operators';
1212
import { BrowserBuilderOutput } from '../../src/browser';
1313
import { createArchitect, host } from '../utils';
@@ -37,6 +37,34 @@ describe('Server Builder', () => {
3737
await run.stop();
3838
});
3939

40+
it('should not emit polyfills', async () => {
41+
const run = await architect.scheduleTarget(target);
42+
const output = await run.result as BrowserBuilderOutput;
43+
expect(output.success).toBe(true);
44+
45+
expect(host.fileMatchExists(getSystemPath(outputPath), /polyfills/)).not.toBeDefined();
46+
expect(host.fileMatchExists(getSystemPath(outputPath), /main/)).toBeDefined();
47+
48+
await run.stop();
49+
});
50+
51+
it('should not emit polyfills when ES5 support is needed', async () => {
52+
// the below is needed because of different code paths
53+
// for polyfills if differential loading is needed
54+
host.writeMultipleFiles({
55+
'browserslist': 'IE 10',
56+
});
57+
58+
const run = await architect.scheduleTarget(target);
59+
const output = await run.result as BrowserBuilderOutput;
60+
expect(output.success).toBe(true);
61+
62+
expect(host.fileMatchExists(getSystemPath(outputPath), /polyfills/)).not.toBeDefined();
63+
expect(host.fileMatchExists(getSystemPath(outputPath), /main/)).toBeDefined();
64+
65+
await run.stop();
66+
});
67+
4068
it('supports sourcemaps', async () => {
4169
const overrides = { sourceMap: true };
4270

0 commit comments

Comments
 (0)