-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathoutput-hashing_spec_large.ts
193 lines (162 loc) · 7.94 KB
/
output-hashing_spec_large.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { Architect } from '@angular-devkit/architect';
import { normalize } from '@angular-devkit/core';
import {
browserBuild,
createArchitect,
host,
lazyModuleFiles,
lazyModuleFnImport,
} from '../utils';
describe('Browser Builder output hashing', () => {
const target = { project: 'app', target: 'build' };
let architect: Architect;
beforeEach(async () => {
await host.initialize().toPromise();
architect = (await createArchitect(host.root())).architect;
});
afterEach(async () => host.restore().toPromise());
it('updates hash as content changes', async () => {
const OUTPUT_RE = /(main|styles|lazy\.module)\.([a-z0-9]+)\.(chunk|bundle)\.(js|css)$/;
function generateFileHashMap(): Map<string, string> {
const hashes = new Map<string, string>();
host
.scopedSync()
.list(normalize('./dist'))
.forEach(name => {
const matches = name.match(OUTPUT_RE);
if (matches) {
const [, module, hash] = matches;
hashes.set(module, hash);
}
});
return hashes;
}
function validateHashes(
oldHashes: Map<string, string>,
newHashes: Map<string, string>,
shouldChange: Array<string>,
): void {
newHashes.forEach((hash, module) => {
if (hash == oldHashes.get(module)) {
if (shouldChange.includes(module)) {
throw new Error(
`Module "${module}" did not change hash (${hash}), but was expected to.`,
);
}
} else if (!shouldChange.includes(module)) {
throw new Error(`Module "${module}" changed hash (${hash}), but was not expected to.`);
}
});
}
let oldHashes: Map<string, string>;
let newHashes: Map<string, string>;
host.writeMultipleFiles(lazyModuleFiles);
host.writeMultipleFiles(lazyModuleFnImport);
const overrides = { outputHashing: 'all', extractCss: true };
// We must do several builds instead of a single one in watch mode, so that the output
// path is deleted on each run and only contains the most recent files.
await browserBuild(architect, host, target, overrides);
// Save the current hashes.
oldHashes = generateFileHashMap();
host.writeMultipleFiles(lazyModuleFiles);
host.writeMultipleFiles(lazyModuleFnImport);
await browserBuild(architect, host, target, overrides);
newHashes = generateFileHashMap();
validateHashes(oldHashes, newHashes, []);
oldHashes = newHashes;
host.writeMultipleFiles({ 'src/styles.css': 'body { background: blue; }' });
// Style hash should change.
await browserBuild(architect, host, target, overrides);
newHashes = generateFileHashMap();
validateHashes(oldHashes, newHashes, ['styles']);
oldHashes = newHashes;
host.writeMultipleFiles({ 'src/app/app.component.css': 'h1 { margin: 10px; }' });
// Main hash should change, since inline styles go in the main bundle.
await browserBuild(architect, host, target, overrides);
newHashes = generateFileHashMap();
validateHashes(oldHashes, newHashes, ['main']);
oldHashes = newHashes;
host.appendToFile('src/app/lazy/lazy.module.ts', `console.log(1);`);
// Lazy loaded bundle should change, and so should inline.
await browserBuild(architect, host, target, overrides);
newHashes = generateFileHashMap();
validateHashes(oldHashes, newHashes, ['lazy.module']);
oldHashes = newHashes;
host.appendToFile('src/main.ts', '');
// Nothing should have changed.
await browserBuild(architect, host, target, overrides);
newHashes = generateFileHashMap();
validateHashes(oldHashes, newHashes, []);
});
it('supports options', async () => {
host.writeMultipleFiles({ 'src/styles.css': `h1 { background: url('./spectrum.png')}` });
host.writeMultipleFiles(lazyModuleFiles);
host.writeMultipleFiles(lazyModuleFnImport);
// We must do several builds instead of a single one in watch mode, so that the output
// path is deleted on each run and only contains the most recent files.
// 'all' should hash everything.
await browserBuild(architect, host, target, { outputHashing: 'all', extractCss: true });
expect(host.fileMatchExists('dist', /runtime\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /main\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /polyfills\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /vendor\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.css/)).toBeTruthy();
expect(host.fileMatchExists('dist', /spectrum\.[0-9a-f]{20}\.png/)).toBeTruthy();
// 'none' should hash nothing.
await browserBuild(architect, host, target, { outputHashing: 'none', extractCss: true });
expect(host.fileMatchExists('dist', /runtime\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /main\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /polyfills\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /vendor\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.css/)).toBeFalsy();
expect(host.fileMatchExists('dist', /spectrum\.[0-9a-f]{20}\.png/)).toBeFalsy();
// 'media' should hash css resources only.
await browserBuild(architect, host, target, { outputHashing: 'media', extractCss: true });
expect(host.fileMatchExists('dist', /runtime\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /main\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /polyfills\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /vendor\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.css/)).toBeFalsy();
expect(host.fileMatchExists('dist', /spectrum\.[0-9a-f]{20}\.png/)).toBeTruthy();
// 'bundles' should hash bundles only.
await browserBuild(architect, host, target, { outputHashing: 'bundles', extractCss: true });
expect(host.fileMatchExists('dist', /runtime\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /main\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /polyfills\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /vendor\.[0-9a-f]{20}\.js/)).toBeTruthy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.css/)).toBeTruthy();
expect(host.fileMatchExists('dist', /spectrum\.[0-9a-f]{20}\.png/)).toBeFalsy();
});
it('does not hash non injected styles', async () => {
const overrides = {
outputHashing: 'all',
extractCss: true,
styles: [{ input: 'src/styles.css', inject: false }],
};
await browserBuild(architect, host, target, overrides);
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js.map/)).toBeFalsy();
expect(host.scopedSync().exists(normalize('dist/styles.css'))).toBe(true);
expect(host.scopedSync().exists(normalize('dist/styles.css.map'))).toBe(true);
});
it('does not hash non injected styles when optimization is enabled', async () => {
const overrides = {
outputHashing: 'all',
extractCss: true,
optimization: true,
styles: [{ input: 'src/styles.css', inject: false }],
};
await browserBuild(architect, host, target, overrides);
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js/)).toBeFalsy();
expect(host.fileMatchExists('dist', /styles\.[0-9a-f]{20}\.js.map/)).toBeFalsy();
expect(host.scopedSync().exists(normalize('dist/styles.css'))).toBe(true);
expect(host.scopedSync().exists(normalize('dist/styles.css.map'))).toBe(true);
});
});