-
Notifications
You must be signed in to change notification settings - Fork 3
/
postbuild.ts
75 lines (64 loc) · 2.41 KB
/
postbuild.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
// @ts-ignore
const fs = require('fs')
const getExtension = fileName => {
let extensionArr = fileName.split('.')
return extensionArr[extensionArr.length - 1]
}
const postBuild = targetPath => {
let fileNames = fs.readdirSync(targetPath)
// console.log(targetPath, files)
let scripts = []
let resources = []
let resourceExts = ['css', 'png', 'jpg', 'jpeg', 'gif']
// Collection and Classification
for (let fileName of fileNames) {
// console.log(fileName)
if (getExtension(fileName) == 'js') scripts.push(fileName)
else {
for (let resourceExt of resourceExts) {
if (getExtension(fileName) == resourceExt) {
resources.push(fileName)
break
}
}
}
}
// Exclude targets already scheduled to load
let indexHtmlPath = targetPath + '/index.html'
let indexHtml = fs.readFileSync(indexHtmlPath)
let needToPrefetchScripts = []
let needToPrefetchResources = []
for (let script of scripts) {
let isExistInIndexHtml = String(indexHtml).indexOf(script) != -1
if (!isExistInIndexHtml) needToPrefetchScripts.push(script)
}
for (let resource of resources) {
let isExistInIndexHtml = String(indexHtml).indexOf(resource) != -1
if (!isExistInIndexHtml) needToPrefetchResources.push(resource)
}
//console.log('scripts:', needToPrefetchScripts)
//console.log('resources:', needToPrefetchResources)
//console.log('origina HTML', indexHtml)
// Create Prefetch Inejct Code
let injectPrefetchList = ''
for (let needToPrefetchScript of needToPrefetchScripts)
injectPrefetchList += `<link rel="prefetch" href="${needToPrefetchScript}">`
for (let needToPrefetchResource of needToPrefetchResources)
injectPrefetchList += `<link rel="prefetch" href="${needToPrefetchResource}">`
let changedHtml = String(indexHtml).replace(
'</head>',
injectPrefetchList + '</head>'
)
fs.writeFileSync(indexHtmlPath, changedHtml)
console.log(
`${needToPrefetchScripts.length +
needToPrefetchResources.length}'s prefetch registrations completed.`
)
}
// @ts-ignore
const targetPath = String(process.argv[2])
// @ts-ignore
if (process.argv[1].indexOf('postbuild.ts') !== -1 && targetPath.length != 0)
postBuild(targetPath)
// @ts-ignore
module.exports = postBuild