Skip to content

Commit d8438f7

Browse files
clydindgp1130
authored andcommitted
fix(@angular-devkit/build-angular): prevent webpack from adding suffixes to polyfills files
The ES5 polyfills file was erroneously being suffixed with `es2015`. The webpack configuration does not support conditional customization per chunk for the output filenames (`chunkFilename` option schema only supports string values). This change adds an additional small webpack plugin that allows the chunk filenames to be adjusted based on the chunk name. The plugin is only added when differential loading is enabled as this is the only time that a chunk currently requires its filename to be adjusted. Closes angular#15915
1 parent 2f45167 commit d8438f7

File tree

1 file changed

+28
-5
lines changed
  • packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs

1 file changed

+28
-5
lines changed

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

+28-5
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import * as path from 'path';
1515
import { RollupOptions } from 'rollup';
1616
import { ScriptTarget } from 'typescript';
1717
import {
18+
ChunkData,
1819
Compiler,
1920
Configuration,
2021
ContextReplacementPlugin,
2122
HashedModuleIdsPlugin,
23+
Plugin,
2224
Rule,
2325
compilation,
2426
debug,
@@ -57,11 +59,13 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
5759
throw new Error('Cannot locate node_modules directory.');
5860
}
5961

60-
// tslint:disable-next-line:no-any
61-
const extraPlugins: any[] = [];
62+
const extraPlugins: Plugin[] = [];
6263
const extraRules: Rule[] = [];
6364
const entryPoints: { [key: string]: string[] } = {};
6465

66+
// determine hashing format
67+
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
68+
6569
const targetInFileName = getEsVersionForFileName(
6670
tsConfig.options.target,
6771
buildOptions.esVersionInFileName,
@@ -146,6 +150,28 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
146150
// Add zone.js legacy support to the es5 polyfills
147151
// This is a noop execution-wise if zone-evergreen is not used.
148152
entryPoints[polyfillsChunkName].push('zone.js/dist/zone-legacy');
153+
154+
// Since the chunkFileName option schema does not allow the function overload, add a plugin
155+
// that changes the name of the ES5 polyfills chunk to not include ES2015.
156+
extraPlugins.push({
157+
apply(compiler) {
158+
compiler.hooks.compilation.tap('build-angular', compilation => {
159+
// Webpack typings do not contain MainTemplate assetPath hook
160+
// The webpack.Compilation assetPath hook is a noop in 4.x so the template must be used
161+
// tslint:disable-next-line: no-any
162+
(compilation.mainTemplate.hooks as any).assetPath.tap(
163+
'build-angular',
164+
(filename: string, data: ChunkData) => {
165+
const isMap = filename && filename.endsWith('.map');
166+
167+
return data.chunk && data.chunk.name === 'polyfills-es5'
168+
? `polyfills-es5${hashFormat.chunk}.js${isMap ? '.map' : ''}`
169+
: filename;
170+
},
171+
);
172+
});
173+
},
174+
});
149175
}
150176
if (!buildOptions.aot) {
151177
if (differentialLoadingMode) {
@@ -183,9 +209,6 @@ export function getCommonConfig(wco: WebpackConfigOptions): Configuration {
183209
);
184210
}
185211

186-
// determine hashing format
187-
const hashFormat = getOutputHashFormat(buildOptions.outputHashing || 'none');
188-
189212
// process global scripts
190213
const globalScriptsByBundleName = normalizeExtraEntryPoints(
191214
buildOptions.scripts,

0 commit comments

Comments
 (0)