-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasm_path_fixer.js
52 lines (44 loc) · 1.3 KB
/
wasm_path_fixer.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
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs');
/* eslint-disable @typescript-eslint/no-var-requires */
const glob = require('glob');
glob('dist/*.wasm', {}, (_, wasmFiles) => {
const hashes = [];
for (const file of wasmFiles) {
const fileName = file.replace('dist/', '');
const hash = fileName.split('.')[0];
hashes.push(hash);
fs.renameSync(file, `dist/static/${fileName}`);
fs.readFile('dist/index.html', 'utf8', (err, data) => {
if (err) {
console.log(err);
return;
}
const result = data.replace(fileName, `static/${fileName}`);
fs.writeFile('dist/index.html', result, 'utf8', (err) => {
if (err) console.log(err);
});
});
}
glob('dist/static/js/*.js', (_, jsFiles) => {
for (const jsFile of jsFiles) {
fs.readFile(jsFile, 'utf8', (readErr, js) => {
if (readErr) {
console.log(readErr);
return;
}
let result = js;
for (const hash of hashes) {
result = result.replace(hash, `static/${hash}`);
}
if (result !== js) {
fs.writeFile(jsFile, result, 'utf8', (writeErr) => {
if (writeErr) {
throw new Error(writeErr);
}
});
}
});
}
});
});