-
Notifications
You must be signed in to change notification settings - Fork 5
/
transform.js
39 lines (32 loc) · 1.05 KB
/
transform.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
function transform(template, chunks) {
let htmlOutput = template;
for (let chunk of chunks) {
const { hash, files, names } = chunk;
for (let file of files) {
const extension = file.split('.').slice(-1).join('');
const fileName = file
.split(hash)
.join('')
.split('.')
.slice(0, -1)
.join('')
.replace(/[^a-z0-9_$]/gi, '');
let regex = null;
switch (extension) {
case 'js': {
regex = new RegExp(`(src=["'].*/)(${fileName}\\.js)(["'])`, 'i');
break;
}
case 'css': {
regex = new RegExp(`(href=["'].*/)(${fileName}\\.css)(["'])`, 'i');
break;
}
default:
continue;
}
htmlOutput = htmlOutput.replace(regex, `$1${file}$3`);
}
}
return htmlOutput;
}
module.exports = transform;