-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
71 lines (63 loc) · 1.78 KB
/
Copy pathindex.js
File metadata and controls
71 lines (63 loc) · 1.78 KB
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
const through = require('through2')
const hash = require('hash-sum')
const styleRewrite = require('./lib/style-rewrite')
const htmlRewrite = require('./lib/html-rewrite')
function prefixStream(prefixText) {
var stream = through()
stream.write(prefixText)
return stream
}
function getHash(file) {
let pathNoExt = file.relative.substring(0, file.relative.lastIndexOf('.'))
return '_' + hash(pathNoExt)
}
function style(opts) {
let getFileHash = getHash
if (typeof opts === 'object') {
if (typeof opts.getHash === 'function') {
getFileHash = opts.getHash
}
}
return through.obj(function (file, enc, cb) {
const id = getFileHash(file)
let originalContent = file.contents.toString()
let content = styleRewrite(originalContent, id)
if (file.isBuffer()) {
file.contents = new Buffer(content)
}
if (file.isStream()) {
file.contents = prefixStream(content)
}
this.push(file)
cb()
})
}
function html(opts) {
let getFileHash = getHash
if (typeof opts === 'object') {
if (typeof opts.getHash === 'function') {
getFileHash = opts.getHash
}
}
return through.obj(function (file, enc, cb) {
if (file.isNull()) {
cb(null, file)
return
}
const id = getFileHash(file)
let originalContent = file.contents.toString()
let content = htmlRewrite(originalContent, id)
if (file.isBuffer()) {
file.contents = new Buffer(content)
}
if (file.isStream()) {
file.contents = prefixStream(content)
}
this.push(file)
cb()
})
}
module.exports = {
styleRewrite: style,
htmlRewrite: html,
}