forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk-hash.ts
More file actions
105 lines (94 loc) · 3.27 KB
/
chunk-hash.ts
File metadata and controls
105 lines (94 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import * as fs from 'fs';
import {ng} from '../../utils/process';
import {writeFile, prependToFile, replaceInFile} from '../../utils/fs';
const OUTPUT_RE = /(main|polyfills|vendor|inline|styles|\d+)\.[a-z0-9]+\.(chunk|bundle)\.(js|css)$/;
function generateFileHashMap(): Map<string, string> {
const hashes = new Map<string, string>();
fs.readdirSync('./dist')
.forEach(name => {
if (!name.match(OUTPUT_RE)) {
return;
}
const [module, hash] = name.split('.');
hashes.set(module, hash);
});
return hashes;
}
function validateHashes(
oldHashes: Map<string, string>,
newHashes: Map<string, string>,
shouldChange: Array<string>): void {
console.log(' Validating hashes...');
console.log(` Old hashes: ${JSON.stringify([...oldHashes])}`);
console.log(` New hashes: ${JSON.stringify([...newHashes])}`);
oldHashes.forEach((hash, module) => {
if (hash == newHashes.get(module)) {
if (shouldChange.includes(module)) {
throw new Error(`Module "${module}" did not change hash (${hash})...`);
}
} else if (!shouldChange.includes(module)) {
throw new Error(`Module "${module}" changed hash (${hash})...`);
}
});
}
export default function() {
// TODO(architect): Delete this test. It is now in devkit/build-angular.
return;
let oldHashes: Map<string, string>;
let newHashes: Map<string, string>;
// First, collect the hashes.
return Promise.resolve()
.then(() => ng('generate', 'module', 'lazy', '--routing'))
.then(() => prependToFile('src/app/app.module.ts', `
import { RouterModule } from '@angular/router';
import { ReactiveFormsModule } from '@angular/forms';
`))
.then(() => replaceInFile('src/app/app.module.ts', 'imports: [', `imports: [
RouterModule.forRoot([{ path: "lazy", loadChildren: "./lazy/lazy.module#LazyModule" }]),
ReactiveFormsModule,
`))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
oldHashes = generateFileHashMap();
})
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, []);
oldHashes = newHashes;
})
.then(() => writeFile('src/styles.css', 'body { background: blue; }'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['styles']);
oldHashes = newHashes;
})
.then(() => writeFile('src/app/app.component.css', 'h1 { margin: 10px; }'))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['main']);
oldHashes = newHashes;
})
.then(() => prependToFile('src/app/lazy/lazy.module.ts', `
import { ReactiveFormsModule } from '@angular/forms';
`))
.then(() => replaceInFile('src/app/lazy/lazy.module.ts', 'imports: [', `
imports: [
ReactiveFormsModule,
`))
.then(() => ng('build', '--output-hashing=all'))
.then(() => {
newHashes = generateFileHashMap();
})
.then(() => {
validateHashes(oldHashes, newHashes, ['inline', '0']);
});
}