-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
57 lines (46 loc) · 1.32 KB
/
index.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
var StringDecoder = require('string_decoder').StringDecoder
var extractClass = require('extract-html-class')
var extractId = require('extract-html-id')
var extractTag = require('extract-html-tag')
var eos = require('end-of-stream')
var through = require('through2')
var hs = require('hstream')
var assert = require('assert')
var filter = require('./lib/filter')
module.exports = inline
function inline (css) {
if (Buffer.isBuffer(css)) css = String(css)
assert.strictEqual(typeof css, 'string', 'inline-critical-css: expected css to be type string')
var decoder = new StringDecoder('utf8')
var src = ''
return through(write, flush)
function write (chunk, _, cb) {
src += decoder.write(chunk)
cb()
}
function flush (cb) {
src += decoder.end()
var self = this
var valid = extractClass(src).map(function (cls) {
return '.' + cls
}).concat(extractId(src).map(function (id) {
return '#' + id
})).concat(extractTag(src))
var critical = filter(css, valid)
var style = ''
if (critical) {
style = '<style>' + critical + '</style>'
}
var source = hs({
head: { _appendHtml: style }
})
source.on('data', function (data) {
self.push(data)
})
eos(source, function (err) {
if (err) return cb(err)
cb()
})
source.end(src)
}
}