Skip to content

Commit cfc0aa4

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 5c9fc32 commit cfc0aa4

File tree

4 files changed

+58
-18
lines changed

4 files changed

+58
-18
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export function buildWebpackBrowser(
370370
}
371371
// If not optimizing then ES2015 polyfills do not need processing
372372
// Unlike other module scripts, it is never downleveled
373-
const es2015Polyfills = file.file.startsWith('polyfills-es2015');
373+
const es2015Polyfills = file.file.startsWith('polyfills-es20');
374374
if (!actionOptions.optimize && es2015Polyfills) {
375375
continue;
376376
}
@@ -392,7 +392,7 @@ export function buildWebpackBrowser(
392392

393393
if (es5Polyfills) {
394394
fs.unlinkSync(filename);
395-
filename = filename.replace('-es2015', '');
395+
filename = filename.replace(/\-es20\d{2}/, '');
396396
}
397397

398398
// Record the bundle processing action
@@ -417,8 +417,8 @@ export function buildWebpackBrowser(
417417

418418
// Add the newly created ES5 bundles to the index as nomodule scripts
419419
const newFilename = es5Polyfills
420-
? file.file.replace('-es2015', '')
421-
: file.file.replace('es2015', 'es5');
420+
? file.file.replace(/\-es20\d{2}/, '')
421+
: file.file.replace(/\-es20\d{2}/, '-es5');
422422
noModuleFiles.push({ ...file, file: newFilename });
423423
}
424424

packages/angular_devkit/build_angular/src/utils/action-cache.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class BundleActionCache {
148148
cacheEntry = entries[CacheKey.DownlevelCode];
149149
if (cacheEntry) {
150150
result.downlevel = {
151-
filename: action.filename.replace('es2015', 'es5'),
151+
filename: action.filename.replace(/\-es20\d{2}/, '-es5'),
152152
size: cacheEntry.size,
153153
integrity: cacheEntry.integrity,
154154
};
@@ -158,7 +158,7 @@ export class BundleActionCache {
158158
cacheEntry = entries[CacheKey.DownlevelMap];
159159
if (cacheEntry) {
160160
result.downlevel.map = {
161-
filename: action.filename.replace('es2015', 'es5') + '.map',
161+
filename: action.filename.replace(/\-es20\d{2}/, '-es5') + '.map',
162162
size: cacheEntry.size,
163163
};
164164

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function process(options: ProcessBundleOptions): Promise<ProcessBun
9797

9898
const basePath = path.dirname(options.filename);
9999
const filename = path.basename(options.filename);
100-
const downlevelFilename = filename.replace('es2015', 'es5');
100+
const downlevelFilename = filename.replace(/\-es20\d{2}/, '-es5');
101101
const downlevel = !options.optimizeOnly;
102102

103103
// if code size is larger than 500kB, manually handle sourcemaps with newer source-map package.
@@ -403,9 +403,9 @@ async function processRuntime(
403403

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

408-
const downlevelFilePath = options.filename.replace('es2015', 'es5');
408+
const downlevelFilePath = options.filename.replace(/\-es20\d{2}/, '-es5');
409409
let downlevelMap;
410410
let result;
411411
if (options.optimize) {

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

+49-9
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

@@ -89,14 +131,12 @@ describe('Browser Builder with differential loading', () => {
89131
});
90132

91133
it('emits the right ES formats', async () => {
92-
if (!process.env['NG_BUILD_FULL_DIFFERENTIAL']) {
93-
// The test fails depending on the order of previously executed tests
94-
// The wrong data is being read from the filesystem.
95-
pending('Incredibly flaky outside full build differential loading');
96-
}
97-
const { files } = await browserBuild(architect, host, target, { optimization: true });
98-
expect(await files['main-es5.js']).not.toContain('class');
99-
expect(await files['main-es2015.js']).toContain('class');
134+
const { files } = await browserBuild(architect, host, target, {
135+
optimization: true,
136+
vendorChunk: false,
137+
});
138+
expect(await files['main-es5.js']).not.toContain('const ');
139+
expect(await files['main-es2015.js']).toContain('const ');
100140
});
101141

102142
it('uses the right zone.js variant', async () => {

0 commit comments

Comments
 (0)