Skip to content

Commit 5b1fcfb

Browse files
clydindgp1130
authored andcommitted
fix(@angular-devkit/build-angular): properly process es2016+ targets with differential loading
A target of es2015 was previously assumed when using differential loading. This could result in erroneously downleveling an es2016+ output file instead of generating a new es5 output file.
1 parent 574e76e commit 5b1fcfb

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ export function buildWebpackBrowser(
347347
}
348348
// If not optimizing then ES2015 polyfills do not need processing
349349
// Unlike other module scripts, it is never downleveled
350-
const es2015Polyfills = file.file.startsWith('polyfills-es2015');
350+
const es2015Polyfills = file.file.startsWith('polyfills-es20');
351351
if (!actionOptions.optimize && es2015Polyfills) {
352352
continue;
353353
}
@@ -368,7 +368,7 @@ export function buildWebpackBrowser(
368368

369369
if (es5Polyfills) {
370370
fs.unlinkSync(filename);
371-
filename = filename.replace('-es2015', '');
371+
filename = filename.replace(/\-es20\d{2}/, '');
372372
}
373373

374374
// Record the bundle processing action
@@ -393,8 +393,8 @@ export function buildWebpackBrowser(
393393

394394
// Add the newly created ES5 bundles to the index as nomodule scripts
395395
const newFilename = es5Polyfills
396-
? file.file.replace('-es2015', '')
397-
: file.file.replace('es2015', 'es5');
396+
? file.file.replace(/\-es20\d{2}/, '')
397+
: file.file.replace(/\-es20\d{2}/, '-es5');
398398
noModuleFiles.push({ ...file, file: newFilename });
399399
}
400400

packages/angular_devkit/build_angular/src/utils/process-bundle.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
8989

9090
const basePath = path.dirname(options.filename);
9191
const filename = path.basename(options.filename);
92-
const downlevelFilename = filename.replace('es2015', 'es5');
92+
const downlevelFilename = filename.replace(/\-es20\d{2}/, '-es5');
9393
const downlevel = !options.optimizeOnly;
9494

9595
// if code size is larger than 1 MB, manually handle sourcemaps with newer source-map package.
@@ -376,9 +376,9 @@ async function processRuntime(
376376

377377
// Adjust lazy loaded scripts to point to the proper variant
378378
// Extra spacing is intentional to align source line positions
379-
downlevelCode = downlevelCode.replace('"-es2015.', ' "-es5.');
379+
downlevelCode = downlevelCode.replace(/"\-es20\d{2}\./, ' "-es5.');
380380

381-
const downlevelFilePath = options.filename.replace('es2015', 'es5');
381+
const downlevelFilePath = options.filename.replace(/\-es20\d{2}/, '-es5');
382382
let downlevelMap;
383383
let result;
384384
if (options.optimize) {

packages/angular_devkit/build_angular/test/browser/differential_loading_spec_large.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Browser Builder with differential loading', () => {
2626

2727
afterEach(async () => host.restore().toPromise());
2828

29-
it('emits all the neccessary files', async () => {
29+
it('emits all the neccessary files for default configuration', async () => {
3030
const { files } = await browserBuild(architect, host, target);
3131

3232
const expectedOutputs = [
@@ -62,6 +62,48 @@ describe('Browser Builder with differential loading', () => {
6262
expect(Object.keys(files)).toEqual(jasmine.arrayWithExactContents(expectedOutputs));
6363
});
6464

65+
it('emits all the neccessary files for target of ES2016', async () => {
66+
host.replaceInFile(
67+
'tsconfig.json',
68+
'"target": "es2015",',
69+
`"target": "es2016",`,
70+
);
71+
72+
const { files } = await browserBuild(architect, host, target);
73+
74+
const expectedOutputs = [
75+
'favicon.ico',
76+
'index.html',
77+
78+
'main-es2016.js',
79+
'main-es2016.js.map',
80+
'main-es5.js',
81+
'main-es5.js.map',
82+
83+
'polyfills-es2016.js',
84+
'polyfills-es2016.js.map',
85+
'polyfills-es5.js',
86+
'polyfills-es5.js.map',
87+
88+
'runtime-es2016.js',
89+
'runtime-es2016.js.map',
90+
'runtime-es5.js',
91+
'runtime-es5.js.map',
92+
93+
'styles-es2016.js',
94+
'styles-es2016.js.map',
95+
'styles-es5.js',
96+
'styles-es5.js.map',
97+
98+
'vendor-es2016.js',
99+
'vendor-es2016.js.map',
100+
'vendor-es5.js',
101+
'vendor-es5.js.map',
102+
] as PathFragment[];
103+
104+
expect(Object.keys(files)).toEqual(jasmine.arrayWithExactContents(expectedOutputs));
105+
});
106+
65107
it('deactivates differential loading for watch mode', async () => {
66108
const { files } = await browserBuild(architect, host, target, { watch: true });
67109

0 commit comments

Comments
 (0)