66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { runTargetSpec } from '@angular-devkit/architect/testing' ;
9+ import { DefaultTimeout , runTargetSpec } from '@angular-devkit/architect/testing' ;
1010import { join , normalize , virtualFs } from '@angular-devkit/core' ;
11- import { tap } from 'rxjs/operators' ;
11+ import { concatMap , tap } from 'rxjs/operators' ;
1212import { browserTargetSpec , host } from '../utils' ;
1313
1414
1515describe ( 'Browser Builder build optimizer' , ( ) => {
1616 const outputPath = normalize ( 'dist' ) ;
17+ const fileName = join ( outputPath , 'main.js' ) ;
1718
1819 beforeEach ( done => host . initialize ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
1920 afterEach ( done => host . restore ( ) . toPromise ( ) . then ( done , done . fail ) ) ;
@@ -23,10 +24,49 @@ describe('Browser Builder build optimizer', () => {
2324 runTargetSpec ( host , browserTargetSpec , overrides ) . pipe (
2425 tap ( ( buildEvent ) => expect ( buildEvent . success ) . toBe ( true ) ) ,
2526 tap ( ( ) => {
26- const fileName = join ( outputPath , 'main.js' ) ;
2727 const content = virtualFs . fileBufferToString ( host . scopedSync ( ) . read ( normalize ( fileName ) ) ) ;
2828 expect ( content ) . not . toMatch ( / \. d e c o r a t o r s = / ) ;
2929 } ) ,
3030 ) . toPromise ( ) . then ( done , done . fail ) ;
3131 } ) ;
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+ } ) ;
3272} ) ;
0 commit comments