/
contentHashing.js
74 lines (59 loc) 路 2.27 KB
/
contentHashing.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
72
73
74
const assert = require('assert');
const path = require('path');
const fs = require('@parcel/fs');
const {bundle, rimraf, ncp} = require('@parcel/test-utils');
describe('content hashing', function() {
beforeEach(async function() {
await rimraf(path.join(__dirname, '/input'));
});
it('should update content hash when content changes', async function() {
await ncp(
path.join(__dirname, '/integration/html-css'),
path.join(__dirname, '/input')
);
await bundle(path.join(__dirname, '/input/index.html'), {
production: true
});
let html = await fs.readFile(
path.join(__dirname, '/dist/index.html'),
'utf8'
);
let filename = html.match(
/<link rel="stylesheet" href="[/\\]{1}(input\.[a-f0-9]+\.css)">/
)[1];
assert(await fs.exists(path.join(__dirname, '/dist/', filename)));
await fs.writeFile(
path.join(__dirname, '/input/index.css'),
'body { background: green }'
);
await bundle(path.join(__dirname, '/input/index.html'), {
production: true
});
html = await fs.readFile(path.join(__dirname, '/dist/index.html'), 'utf8');
let newFilename = html.match(
/<link rel="stylesheet" href="[/\\]{1}(input\.[a-f0-9]+\.css)">/
)[1];
assert(await fs.exists(path.join(__dirname, '/dist/', newFilename)));
assert.notEqual(filename, newFilename);
});
it('should update content hash when raw asset changes', async function() {
await ncp(
path.join(__dirname, '/integration/import-raw'),
path.join(__dirname, '/input')
);
await bundle(path.join(__dirname, '/input/index.js'), {
production: true
});
let js = await fs.readFile(path.join(__dirname, '/dist/index.js'), 'utf8');
let filename = js.match(/\/(test\.[0-9a-f]+\.txt)/)[1];
assert(await fs.exists(path.join(__dirname, '/dist/', filename)));
await fs.writeFile(path.join(__dirname, '/input/test.txt'), 'hello world');
await bundle(path.join(__dirname, '/input/index.js'), {
production: true
});
js = await fs.readFile(path.join(__dirname, '/dist/index.js'), 'utf8');
let newFilename = js.match(/\/(test\.[0-9a-f]+\.txt)/)[1];
assert(await fs.exists(path.join(__dirname, '/dist/', newFilename)));
assert.notEqual(filename, newFilename);
});
});