Skip to content

Commit 91ec0e1

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

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
@@ -67,42 +67,43 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
6767
entryPoints['main'] = [path.resolve(root, buildOptions.main)];
6868
}
6969

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

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

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

108109
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)