@@ -5,52 +5,92 @@ import {ng} from '../../utils/process';
5
5
import { writeFile } from '../../utils/fs' ;
6
6
import { addImportToModule } from '../../utils/ast' ;
7
7
8
+ const OUTPUT_RE = / ( m a i n | p o l y f i l l s | v e n d o r | i n l i n e | s t y l e s | \d + ) \. [ a - z 0 - 9 ] + \. ( c h u n k | b u n d l e ) \. ( j s | c s s ) $ / ;
9
+
10
+ function generateFileHashMap ( ) : Map < string , string > {
11
+ const hashes = new Map < string , string > ( ) ;
12
+
13
+ fs . readdirSync ( './dist' )
14
+ . forEach ( name => {
15
+ if ( ! name . match ( OUTPUT_RE ) ) {
16
+ return ;
17
+ }
18
+
19
+ const [ module , hash ] = name . split ( '.' ) ;
20
+ hashes . set ( module , hash ) ;
21
+ } ) ;
22
+
23
+ return hashes ;
24
+ }
25
+
26
+ function validateHashes (
27
+ oldHashes : Map < string , string > ,
28
+ newHashes : Map < string , string > ,
29
+ shouldChange : Array < string > ) : void {
30
+
31
+ console . log ( ' Validating hashes...' ) ;
32
+ console . log ( ` Old hashes: ${ JSON . stringify ( [ ...oldHashes ] ) } ` ) ;
33
+ console . log ( ` New hashes: ${ JSON . stringify ( [ ...newHashes ] ) } ` ) ;
34
+
35
+ oldHashes . forEach ( ( hash , module ) => {
36
+ if ( hash == newHashes . get ( module ) ) {
37
+ if ( shouldChange . includes ( module ) ) {
38
+ throw new Error ( `Module "${ module } " did not change hash (${ hash } )...` ) ;
39
+ }
40
+ } else if ( ! shouldChange . includes ( module ) ) {
41
+ throw new Error ( `Module "${ module } " changed hash (${ hash } )...` ) ;
42
+ }
43
+ } ) ;
44
+ }
8
45
9
46
export default function ( ) {
10
- const oldHashes : { [ module : string ] : string } = { } ;
11
- const newHashes : { [ module : string ] : string } = { } ;
47
+ let oldHashes : Map < string , string > ;
48
+ let newHashes : Map < string , string > ;
12
49
// First, collect the hashes.
13
50
return Promise . resolve ( )
14
51
. then ( ( ) => ng ( 'generate' , 'module' , 'lazy' , '--routing' ) )
15
52
. then ( ( ) => addImportToModule ( 'src/app/app.module.ts' , oneLine `
16
53
RouterModule.forRoot([{ path: "lazy", loadChildren: "./lazy/lazy.module#LazyModule" }])
17
54
` , '@angular/router' ) )
55
+ . then ( ( ) => addImportToModule (
56
+ 'src/app/app.module.ts' , 'ReactiveFormsModule' , '@angular/forms' ) )
18
57
. then ( ( ) => ng ( 'build' , '--prod' ) )
19
58
. then ( ( ) => {
20
- fs . readdirSync ( './dist' )
21
- . forEach ( name => {
22
- if ( ! name . match ( / ( m a i n | i n l i n e | s t y l e s | \d + ) \. [ a - z 0 - 9 ] + \. b u n d l e \. ( j s | c s s ) / ) ) {
23
- return ;
24
- }
25
-
26
- const [ module , hash ] = name . split ( '.' ) ;
27
- oldHashes [ module ] = hash ;
28
- } ) ;
59
+ oldHashes = generateFileHashMap ( ) ;
29
60
} )
30
- . then ( ( ) => writeFile ( 'src/app/app.component.css' , 'h1 { margin: 5px; }' ) )
31
- . then ( ( ) => writeFile ( 'src/styles.css' , 'body { background: red; }' ) )
32
61
. then ( ( ) => ng ( 'build' , '--prod' ) )
33
62
. then ( ( ) => {
34
- fs . readdirSync ( './dist' )
35
- . forEach ( name => {
36
- if ( ! name . match ( / ( m a i n | i n l i n e | s t y l e s | \d + ) \. [ a - z 0 - 9 ] + \. b u n d l e \. ( j s | c s s ) / ) ) {
37
- return ;
38
- }
39
-
40
- const [ module , hash ] = name . split ( '.' ) ;
41
- newHashes [ module ] = hash ;
42
- } ) ;
63
+ newHashes = generateFileHashMap ( ) ;
43
64
} )
44
65
. then ( ( ) => {
45
- console . log ( ' Validating hashes...' ) ;
46
- console . log ( ` Old hashes: ${ JSON . stringify ( oldHashes ) } ` ) ;
47
- console . log ( ` New hashes: ${ JSON . stringify ( newHashes ) } ` ) ;
48
-
49
- Object . keys ( oldHashes )
50
- . forEach ( module => {
51
- if ( oldHashes [ module ] == newHashes [ module ] ) {
52
- throw new Error ( `Module "${ module } " did not change hash (${ oldHashes [ module ] } )...` ) ;
53
- }
54
- } ) ;
66
+ validateHashes ( oldHashes , newHashes , [ ] ) ;
67
+ oldHashes = newHashes ;
68
+ } )
69
+ . then ( ( ) => writeFile ( 'src/styles.css' , 'body { background: blue; }' ) )
70
+ . then ( ( ) => ng ( 'build' , '--prod' ) )
71
+ . then ( ( ) => {
72
+ newHashes = generateFileHashMap ( ) ;
73
+ } )
74
+ . then ( ( ) => {
75
+ validateHashes ( oldHashes , newHashes , [ 'styles' ] ) ;
76
+ oldHashes = newHashes ;
77
+ } )
78
+ . then ( ( ) => writeFile ( 'src/app/app.component.css' , 'h1 { margin: 10px; }' ) )
79
+ . then ( ( ) => ng ( 'build' , '--prod' ) )
80
+ . then ( ( ) => {
81
+ newHashes = generateFileHashMap ( ) ;
82
+ } )
83
+ . then ( ( ) => {
84
+ validateHashes ( oldHashes , newHashes , [ 'inline' , 'main' ] ) ;
85
+ oldHashes = newHashes ;
86
+ } )
87
+ . then ( ( ) => addImportToModule (
88
+ 'src/app/lazy/lazy.module.ts' , 'ReactiveFormsModule' , '@angular/forms' ) )
89
+ . then ( ( ) => ng ( 'build' , '--prod' ) )
90
+ . then ( ( ) => {
91
+ newHashes = generateFileHashMap ( ) ;
92
+ } )
93
+ . then ( ( ) => {
94
+ validateHashes ( oldHashes , newHashes , [ 'inline' , '0' ] ) ;
55
95
} ) ;
56
96
}
0 commit comments