1
- import { readdirSync } from 'fs' ;
1
+ import { statSync } from 'fs' ;
2
2
import { join } from 'path' ;
3
3
import { getGlobalVariable } from '../../utils/env' ;
4
- import { expectFileToExist , expectFileToMatch } from '../../utils/fs' ;
4
+ import { expectFileToExist , expectFileToMatch , readFile } from '../../utils/fs' ;
5
5
import { expectGitToBeClean } from '../../utils/git' ;
6
6
import { ng } from '../../utils/process' ;
7
7
8
8
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
+
9
28
export default async function ( ) {
10
29
// TODO(architect): Delete this test. It is now in devkit/build-angular.
11
30
@@ -25,12 +44,22 @@ export default async function () {
25
44
await expectFileToMatch ( 'dist/test-project/index.html' , / s t y l e s \. [ 0 - 9 a - f ] { 20 } \. c s s / ) ;
26
45
await expectFileToMatch ( 'dist/test-project/3rdpartylicenses.txt' , / M I T / ) ;
27
46
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 ) ;
31
54
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
+ }
34
63
35
64
// Check that the process didn't change local files.
36
65
await expectGitToBeClean ( ) ;
0 commit comments