-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.ts
74 lines (71 loc) · 2.49 KB
/
plugin.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
import through2 from "through2";
import RewritingStream from "parse5-html-rewriting-stream"
import * as url from "url";
import { default as File } from "vinyl";
interface PluginOption {
name?: string,
hash?(pathname?: string):string
match?(pathname: string): boolean
}
interface PluginHash {
(pathname?: string):string
}
export default function(params: PluginOption|PluginHash) {
const options = {
name: "_v",
hash: function(pathname?: string): string { return "" + Date.now() },
match: function(pathname: string): boolean { return /^\/[^/]+/.test(pathname) }
}
if (typeof params === "function") {
options.hash = params
} else {
if (params.name !== undefined) {
options.name = params.name
}
if(typeof params.hash === "function") {
options.hash = params.hash
}
if (typeof params.match === "function") {
options.match = params.match
}
}
return through2.obj(function(file: File, _, cb) {
if(file.isBuffer()) {
let tpl = "";
const rewriter = new RewritingStream();
rewriter.on('startTag', (token, html) => {
let attr;
if (token.tagName === 'link') {
attr = token.attrs.filter(attr => attr.name === "href")[0];
} else if (token.tagName === 'script') {
attr = token.attrs.filter(attr => attr.name === "src")[0];
}
if (attr && attr.value && options.match!(attr.value)) {
const urlObject = url.parse(attr.value, true);
const query = urlObject.query;
const hash = options.hash!(urlObject.pathname);
if (hash) {
query[options.name!] = hash;
attr.value = url.format(urlObject);
}
rewriter.emitStartTag(token);
} else {
rewriter.emitRaw(html)
}
})
rewriter.on("text", function(token) {
rewriter.emitRaw(token.text)
})
rewriter.on("data", function(token) {
tpl += token
})
rewriter.on("end", function() {
file.contents = Buffer.from(tpl)
cb(null, file)
})
rewriter.end(file.contents.toString())
} else {
cb(null, file)
}
})
}