6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
9
- import { runTargetSpec } from '@angular-devkit/architect/testing' ;
9
+ import { DefaultTimeout , runTargetSpec } from '@angular-devkit/architect/testing' ;
10
10
import { join , normalize , virtualFs } from '@angular-devkit/core' ;
11
- import { tap } from 'rxjs/operators' ;
11
+ import { concatMap , tap } from 'rxjs/operators' ;
12
12
import { browserTargetSpec , host } from '../utils' ;
13
13
14
14
15
15
describe ( 'Browser Builder build optimizer' , ( ) => {
16
16
const outputPath = normalize ( 'dist' ) ;
17
+ const fileName = join ( outputPath , 'main.js' ) ;
17
18
18
19
beforeEach ( done => host . initialize ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
19
20
afterEach ( done => host . restore ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
@@ -23,10 +24,49 @@ describe('Browser Builder build optimizer', () => {
23
24
runTargetSpec ( host , browserTargetSpec , overrides ) . pipe (
24
25
tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
25
26
tap ( ( ) => {
26
- const fileName = join ( outputPath , 'main.js' ) ;
27
27
const content = virtualFs . fileBufferToString ( host . scopedSync ( ) . read ( normalize ( fileName ) ) ) ;
28
28
expect ( content ) . not . toMatch ( / \. d e c o r a t o r s = / ) ;
29
29
} ) ,
30
30
) . toPromise ( ) . then ( done , done . fail ) ;
31
31
} ) ;
32
+
33
+ it ( 'reduces bundle size' , ( done ) => {
34
+ const noBoOverrides = { aot : true , optimization : true , vendorChunk : false } ;
35
+ const boOverrides = { ...noBoOverrides , buildOptimizer : true } ;
36
+
37
+ let noBoSize : number ;
38
+ let boSize : number ;
39
+
40
+ runTargetSpec ( host , browserTargetSpec , noBoOverrides , DefaultTimeout * 3 ) . pipe (
41
+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
42
+ tap ( ( ) => {
43
+ const noBoStats = host . scopedSync ( ) . stat ( normalize ( fileName ) ) ;
44
+ if ( ! noBoStats ) {
45
+ throw new Error ( 'Main file has no stats' ) ;
46
+ }
47
+ noBoSize = noBoStats . size ;
48
+ } ) ,
49
+ concatMap ( ( ) => runTargetSpec ( host , browserTargetSpec , boOverrides , DefaultTimeout * 3 ) ) ,
50
+ tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
51
+ tap ( ( ) => {
52
+ const boStats = host . scopedSync ( ) . stat ( normalize ( fileName ) ) ;
53
+ if ( ! boStats ) {
54
+ throw new Error ( 'Main file has no stats' ) ;
55
+ }
56
+ boSize = boStats . size ;
57
+ } ) ,
58
+ tap ( ( ) => {
59
+ const sizeDiff = Math . round ( ( ( boSize - noBoSize ) / noBoSize ) * 10000 ) / 100 ;
60
+ if ( sizeDiff > - 1 && sizeDiff < 0 ) {
61
+ throw new Error ( 'Total size difference is too small, '
62
+ + 'build optimizer does not seem to have made any optimizations.' ) ;
63
+ }
64
+
65
+ if ( sizeDiff > 1 ) {
66
+ throw new Error ( 'Total size difference is positive, '
67
+ + 'build optimizer made the bundle bigger.' ) ;
68
+ }
69
+ } ) ,
70
+ ) . toPromise ( ) . then ( done , done . fail ) ;
71
+ } ) ;
32
72
} ) ;
0 commit comments