-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
173 lines (160 loc) · 4.6 KB
/
plugin.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
'use strict'
const url = require('url')
const request = require('request')
const iconv = require('iconv-lite')
const eachSeries = require('async').eachSeries
const parseImport = require('parse-import')
const rework = require('rework')
const reworkPluginUrl = require('rework-plugin-url')
/**
* Load content by an external source URL
*
* @param {String|URL} source URL
* @param {Object} options
* @param {Function} next callback
* @api private
* @return null
*/
function load (source, options, next) {
// dependency injection for tests
const requestObj = (options.request && options.request.obj) || request
requestObj.get({
url: source,
headers: (options.request && options.request.headers) || {}
}, function (err, res, _) {
if (err) {
return next(err)
} else {
let reworkObj = null
let err = null
try {
reworkObj = rework(options.preProcessor(decodeBody(Buffer.from(res.body), res.headers).toString()), Object.assign({}, options.rework || {}, {
source: res.headers.location || source
})).use(reworkPluginUrl(function (uri) {
return url.resolve(source, uri)
}))
} catch (e) {
err = e
}
next(err, reworkObj)
}
})
}
/**
* Detect charset encoding by response headers and body
* @param {Object} headers response headers
* @param {Buffer|String} body [description]
* @return {String|null}
*/
function detectCharsetEncoding (headers, body) {
let encoding = null
const httpCharsetRegex = /charset=([a-z0-9\-_]+)/i
if (headers && headers['content-type']) {
let matches = httpCharsetRegex.exec(headers['content-type'])
encoding = (matches && matches[1]) || encoding
}
if (!encoding) {
// TODO detect by using stream of bytes that begins the style sheet. See table at https://www.w3.org/TR/CSS2/syndata.html#x57
const cssCharsetRegex = /^@charset "([a-z0-9\-_]+)"/i
var matches = cssCharsetRegex.exec(body)
encoding = (matches && matches[1]) || encoding
}
return encoding
}
/**
* decode body by detected encoding
* @param {Object} res of Response interface
* @return {Buffer}
*/
function decodeBody (body, headers) {
try {
const encoding = detectCharsetEncoding(headers, body.slice(0, 256).toString())
return (encoding) ? iconv.decode(body, encoding) : body
} catch (e) {
return body
}
}
/**
* Run plugin
*
* @param {Object} style
* @param {Object} options
* @param {Function} next callback
* @api private
* @return null
*/
function run (stylesheet, options, next) {
options = Object.assign({
rework: {
silent: false
},
preProcessor: function (val) { return val },
// TODO implement
formatter: function (val) { return val },
skipToImportList: []
}, options || {})
let rules = stylesheet.rules || []
let ret = []
let errList = []
eachSeries(rules, function (rule, cb) {
switch (rule.type) {
case 'import':
var importRule = '@import ' + rule.import + ';'
var data = parseImport(importRule)[0]
if (!data) {
return setTimeout(cb, 0)
}
options.source = url.resolve(rule.position.source, data.path)
if (options.skipToImportList.indexOf(options.source) >= 0) {
return setTimeout(cb, 0)
}
options.skipToImportList.push(options.source)
load(options.source, options, function (err, content) {
if (err) {
errList.push(err)
return setTimeout(cb, 0)
}
run(content.obj.stylesheet, options, function (_, __) {
if (!data.condition || !data.condition.length) {
ret = ret.concat(content.obj.stylesheet.rules)
} else {
ret.push({
media: data.condition,
rules: content.obj.stylesheet.rules,
type: 'media'
})
}
return setTimeout(cb, 0)
})
})
break
case 'charset':
return setTimeout(cb, 0)
case 'rule':
rule.declarations = rule.declarations || []
ret.push(rule)
return setTimeout(cb, 0)
default:
ret.push(rule)
return setTimeout(cb, 0)
}
}, function () {
stylesheet.rules = ret
next(errList.length ? errList : null, stylesheet)
})
}
/**
* Module exports
*/
module.exports = function (options, next) {
return function (style) {
run(style, options, next)
}
}
// export private methods for unit tests
if (process.env.NODE_ENV === 'test') {
module.exports.__test__ = {
load: load,
detectCharsetEncoding: detectCharsetEncoding
}
}