1- import { readdirSync } from 'fs' ;
1+ import { statSync } from 'fs' ;
22import { join } from 'path' ;
33import { getGlobalVariable } from '../../utils/env' ;
4- import { expectFileToExist , expectFileToMatch } from '../../utils/fs' ;
4+ import { expectFileToExist , expectFileToMatch , readFile } from '../../utils/fs' ;
55import { expectGitToBeClean } from '../../utils/git' ;
66import { ng } from '../../utils/process' ;
77
88
9+ function verifySize ( bundle : string , baselineBytes : number ) {
10+ const size = statSync ( `dist/test-project/${ bundle } ` ) . size ;
11+ const percentageBaseline = baselineBytes * 10 / 100 ;
12+ const maxSize = baselineBytes + percentageBaseline ;
13+ const minSize = baselineBytes - percentageBaseline ;
14+
15+ if ( size >= maxSize ) {
16+ throw new Error (
17+ `Expected ${ bundle } size to be less than ${ maxSize / 1024 } Kb but it was ${ size / 1024 } Kb.` ,
18+ ) ;
19+ }
20+
21+ if ( size <= minSize ) {
22+ throw new Error (
23+ `Expected ${ bundle } size to be greater than ${ minSize / 1024 } Kb but it was ${ size / 1024 } Kb.` ,
24+ ) ;
25+ }
26+ }
27+
928export default async function ( ) {
1029 // TODO(architect): Delete this test. It is now in devkit/build-angular.
1130
@@ -25,12 +44,22 @@ export default async function () {
2544 await expectFileToMatch ( 'dist/test-project/index.html' , / s t y l e s \. [ 0 - 9 a - f ] { 20 } \. c s s / ) ;
2645 await expectFileToMatch ( 'dist/test-project/3rdpartylicenses.txt' , / M I T / ) ;
2746
28- const dirContents = readdirSync ( './dist/test-project' ) ;
29- const mainES5 = dirContents . find ( name => / m a i n - e s 5 .[ a - z 0 - 9 ] + \. j s / . test ( name ) ) ;
30- await expectFileToMatch ( `dist/test-project/${ mainES5 } ` , bootstrapRegExp ) ;
47+ const indexContent = await readFile ( 'dist/test-project/index.html' ) ;
48+ const mainES5Path = indexContent . match ( / s r c = " ( m a i n - e s 5 \. [ a - z 0 - 9 ] { 0 , 32 } \. j s ) " / ) [ 1 ] ;
49+ const mainES2015Path = indexContent . match ( / s r c = " ( m a i n - e s 2 0 1 5 \. [ a - z 0 - 9 ] { 0 , 32 } \. j s ) " / ) [ 1 ] ;
50+
51+ // Content checks
52+ await expectFileToMatch ( `dist/test-project/${ mainES5Path } ` , bootstrapRegExp ) ;
53+ await expectFileToMatch ( `dist/test-project/${ mainES2015Path } ` , bootstrapRegExp ) ;
3154
32- const mainES2015 = dirContents . find ( name => / m a i n - e s 2 0 1 5 .[ a - z 0 - 9 ] + \. j s / . test ( name ) ) ;
33- await expectFileToMatch ( `dist/test-project/${ mainES2015 } ` , bootstrapRegExp ) ;
55+ // Size checks in bytes
56+ if ( ivyProject ) {
57+ verifySize ( mainES5Path , 147789 ) ;
58+ verifySize ( mainES2015Path , 130153 ) ;
59+ } else {
60+ verifySize ( mainES5Path , 155523 ) ;
61+ verifySize ( mainES2015Path , 135394 ) ;
62+ }
3463
3564 // Check that the process didn't change local files.
3665 await expectGitToBeClean ( ) ;
0 commit comments