From d8d1bb83dd88b09c58f211639464438edc78db21 Mon Sep 17 00:00:00 2001 From: Alan Date: Tue, 4 Jun 2019 16:10:58 +0200 Subject: [PATCH] fix(@angular-devkit/build-angular): server build is generating un-needed polyfill file Fixes #14655 --- .../models/webpack-configs/common.ts | 63 ++++++++++--------- .../test/server/base_spec_large.ts | 30 ++++++++- 2 files changed, 61 insertions(+), 32 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts b/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts index 6a842bd80ee8..7e9402759b0c 100644 --- a/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts +++ b/packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/common.ts @@ -66,42 +66,43 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration { entryPoints['main'] = [path.resolve(root, buildOptions.main)]; } - const es5Polyfills = path.join(__dirname, '..', 'es5-polyfills.js'); - const es5JitPolyfills = path.join(__dirname, '..', 'es5-jit-polyfills.js'); - - if (targetInFileName) { - // For differential loading we don't need to have 2 polyfill bundles - if (buildOptions.scriptTargetOverride === ScriptTarget.ES2015) { - entryPoints['polyfills'] = [path.join(__dirname, '..', 'safari-nomodule.js')]; - } else { - entryPoints['polyfills'] = [es5Polyfills]; - if (!buildOptions.aot) { - entryPoints['polyfills'].push(es5JitPolyfills); + if (wco.buildOptions.platform !== 'server') { + const es5Polyfills = path.join(__dirname, '..', 'es5-polyfills.js'); + const es5JitPolyfills = path.join(__dirname, '..', 'es5-jit-polyfills.js'); + if (targetInFileName) { + // For differential loading we don't need to have 2 polyfill bundles + if (buildOptions.scriptTargetOverride === ScriptTarget.ES2015) { + entryPoints['polyfills'] = [path.join(__dirname, '..', 'safari-nomodule.js')]; + } else { + entryPoints['polyfills'] = [es5Polyfills]; + if (!buildOptions.aot) { + entryPoints['polyfills'].push(es5JitPolyfills); + } } - } - } else { - // For NON differential loading we want to have 2 polyfill bundles - if (buildOptions.es5BrowserSupport - || (buildOptions.es5BrowserSupport === undefined && isEs5SupportNeeded(projectRoot))) { - entryPoints['polyfills-es5'] = [es5Polyfills]; - if (!buildOptions.aot) { - entryPoints['polyfills-es5'].push(es5JitPolyfills); + } else { + // For NON differential loading we want to have 2 polyfill bundles + if (buildOptions.es5BrowserSupport + || (buildOptions.es5BrowserSupport === undefined && isEs5SupportNeeded(projectRoot))) { + entryPoints['polyfills-es5'] = [es5Polyfills]; + if (!buildOptions.aot) { + entryPoints['polyfills-es5'].push(es5JitPolyfills); + } } } - } - if (buildOptions.polyfills) { - entryPoints['polyfills'] = [ - ...(entryPoints['polyfills'] || []), - path.resolve(root, buildOptions.polyfills), - ]; - } + if (buildOptions.polyfills) { + entryPoints['polyfills'] = [ + ...(entryPoints['polyfills'] || []), + path.resolve(root, buildOptions.polyfills), + ]; + } - if (!buildOptions.aot) { - entryPoints['polyfills'] = [ - ...(entryPoints['polyfills'] || []), - path.join(__dirname, '..', 'jit-polyfills.js'), - ]; + if (!buildOptions.aot) { + entryPoints['polyfills'] = [ + ...(entryPoints['polyfills'] || []), + path.join(__dirname, '..', 'jit-polyfills.js'), + ]; + } } if (buildOptions.profile || process.env['NG_BUILD_PROFILING']) { diff --git a/packages/angular_devkit/build_angular/test/server/base_spec_large.ts b/packages/angular_devkit/build_angular/test/server/base_spec_large.ts index 471529110938..6a3fa9403b2f 100644 --- a/packages/angular_devkit/build_angular/test/server/base_spec_large.ts +++ b/packages/angular_devkit/build_angular/test/server/base_spec_large.ts @@ -7,7 +7,7 @@ */ import { Architect } from '@angular-devkit/architect'; -import { join, normalize, virtualFs } from '@angular-devkit/core'; +import { getSystemPath, join, normalize, virtualFs } from '@angular-devkit/core'; import { take, tap } from 'rxjs/operators'; import { BrowserBuilderOutput } from '../../src/browser'; import { createArchitect, host } from '../utils'; @@ -37,6 +37,34 @@ describe('Server Builder', () => { await run.stop(); }); + it('should not emit polyfills', async () => { + const run = await architect.scheduleTarget(target); + const output = await run.result as BrowserBuilderOutput; + expect(output.success).toBe(true); + + expect(host.fileMatchExists(getSystemPath(outputPath), /polyfills/)).not.toBeDefined(); + expect(host.fileMatchExists(getSystemPath(outputPath), /main/)).toBeDefined(); + + await run.stop(); + }); + + it('should not emit polyfills when ES5 support is needed', async () => { + // the below is needed because of different code paths + // for polyfills if differential loading is needed + host.writeMultipleFiles({ + 'browserslist': 'IE 10', + }); + + const run = await architect.scheduleTarget(target); + const output = await run.result as BrowserBuilderOutput; + expect(output.success).toBe(true); + + expect(host.fileMatchExists(getSystemPath(outputPath), /polyfills/)).not.toBeDefined(); + expect(host.fileMatchExists(getSystemPath(outputPath), /main/)).toBeDefined(); + + await run.stop(); + }); + it('supports sourcemaps', async () => { const overrides = { sourceMap: true };