-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.js
71 lines (68 loc) · 1.96 KB
/
index.js
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
const path = require('path');
const rollup = require('../../dist/rollup');
const { extend, runTestSuiteWithSamples } = require('../utils.js');
const assert = require('assert');
runTestSuiteWithSamples('file hashes', path.resolve(__dirname, 'samples'), (dir, config) => {
(config.skip ? describe.skip : config.solo ? describe.only : describe)(
path.basename(dir) + ': ' + config.description,
() => {
it(`generates correct hashes`, () => {
process.chdir(dir);
return Promise.all(
[config.options1, config.options2].map(options =>
rollup
.rollup(options)
.then(bundle =>
bundle.generate(
extend(
{ format: 'esm', chunkFileNames: '[hash]', entryFileNames: '[hash]' },
options.output
)
)
)
.then(mapGeneratedToHashMap)
)
).then(([hashMap1, hashMap2]) => {
for (const name of config.expectedEqualHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.equal(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk "${name}" to be equal but they were different.`
);
}
for (const name of config.expectedDifferentHashes || []) {
checkChunkExists(hashMap1, hashMap2, name);
assert.notEqual(
hashMap1[name],
hashMap2[name],
`Expected hashes for chunk "${name}" to be different but they were equal.`
);
}
});
});
}
);
});
function mapGeneratedToHashMap(generated) {
const hashMap = {};
for (const chunk of generated.output) {
let name = chunk.name;
let index = 1;
while (hashMap[name]) {
name = `${chunk.name}${index++}`;
}
hashMap[name] = chunk.fileName;
}
return hashMap;
}
function checkChunkExists(hashMap1, hashMap2, name) {
[hashMap1, hashMap2].forEach((hashMap, index) =>
assert.ok(
hashMap[name],
`Bundle ${index + 1} did not contain chunk "${name}", found chunks: ${Object.keys(hashMap)
.map(key => `"${key}"`)
.join(', ')}`
)
);
}