-
Notifications
You must be signed in to change notification settings - Fork 2
/
addPreloadLink.js
43 lines (37 loc) · 1.09 KB
/
addPreloadLink.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
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const pathToEntry = './build/index.html';
const bundlesRegExp = /\/static\/\w+\/\w+.[a-z0-9]+.\w{2,3}/g;
const assets = glob.sync(__dirname + '/build/static/media/*.woff*')
.map((assetPath) => {
return path.relative(__dirname + '/build', assetPath);
});
const splitBy = '</title>';
const builtHTMLContent = fs.readFileSync(pathToEntry).toString();
const links = builtHTMLContent.match(bundlesRegExp);
const parts = builtHTMLContent.split(splitBy);
let fileWithPreload = [
parts[0],
splitBy,
];
links.forEach(link => {
let fileType = 'script';
if (/\.css$/.test(link)) {
fileType = 'style';
}
if(!(/\.chu$/.test(link))){
fileWithPreload = [
...fileWithPreload,
`<link rel="preload" href="${link}" as="${fileType}" crossorigin="anonymous">`,
];
}
});
for (const link of assets) {
fileWithPreload.push(`<link rel="preload" href="./${link}" as="font" crossorigin="anonymous">`);
}
fileWithPreload = [
...fileWithPreload,
parts[1],
];
fs.writeFileSync(pathToEntry, fileWithPreload.join(''));