-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathbuild-optimizer_spec.ts
78 lines (64 loc) · 2.69 KB
/
build-optimizer_spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Architect } from '@angular-devkit/architect';
import { BrowserBuilderOutput } from '@angular-devkit/build-angular';
import { join, normalize } from '@angular-devkit/core';
import { browserBuild, createArchitect, host } from '../../test-utils';
describe('Browser Builder build optimizer', () => {
const targetSpec = { project: 'app', target: 'build' };
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(async () => host.restore().toPromise());
it('works', async () => {
const overrides = { aot: true, buildOptimizer: true };
const { files } = await browserBuild(architect, host, targetSpec, overrides);
expect(await files['main.js']).not.toMatch(/\.decorators =/);
});
it('fails if AOT is disabled', async () => {
const overrides = { aot: false, buildOptimizer: true };
const run = await architect.scheduleTarget(targetSpec, overrides);
try {
await run.result;
expect('THE ABOVE LINE SHOULD THROW').toBe('');
} catch {}
});
it('reduces bundle size', async () => {
const noBoOverrides = { aot: true, optimization: true, vendorChunk: false };
const boOverrides = { ...noBoOverrides, buildOptimizer: true };
const run = await architect.scheduleTarget(targetSpec, noBoOverrides);
const output = await run.result as BrowserBuilderOutput;
expect(output.success).toBe(true);
const noBoStats = await host.stat(join(normalize(output.outputPath), 'main.js')).toPromise();
if (!noBoStats) {
throw new Error('Main file has no stats');
}
const noBoSize = noBoStats.size;
await run.stop();
const boRun = await architect.scheduleTarget(targetSpec, boOverrides);
const boOutput = await run.result as BrowserBuilderOutput;
expect(boOutput.success).toBe(true);
const boStats = await host.stat(join(normalize(output.outputPath), 'main.js')).toPromise();
if (!boStats) {
throw new Error('Main file has no stats');
}
const boSize = boStats.size;
await boRun.stop();
const sizeDiff = Math.round(((boSize - noBoSize) / noBoSize) * 10000) / 100;
if (sizeDiff > -1 && sizeDiff < 0) {
throw new Error('Total size difference is too small, '
+ 'build optimizer does not seem to have made any optimizations.');
}
if (sizeDiff > 1) {
throw new Error('Total size difference is positive, '
+ 'build optimizer made the bundle bigger.');
}
});
});