-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
55 lines (46 loc) · 1.54 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
var postcss = require('postcss')
var EXPORT_FUNC_IDENTIFIER = 'export-custom-media'
var EXPORT_DECL_START = EXPORT_FUNC_IDENTIFIER + '('
function exportCustomMedia (options) {
return function (styles, result) {
var customMediasMap = {}
styles.walkAtRules(function (rule) {
if (rule.name !== "custom-media") {
return
}
// TODO deal with circular and other stuff (see postcss-custom-media)
var params = rule.params.split(' ')
// @custom-media <extension-name> <media-query-list>;
// customMediasMap[<extension-name>] = <media-query-list>
customMediasMap[params.shift()] = params.join(' ')
})
styles.walkDecls(function (decl) {
var value = decl.value
if (value.indexOf(EXPORT_DECL_START) !== 0) {
return
}
// ignore last ')' and remove white spaces
var mediaQueryName = value.slice(EXPORT_DECL_START.length, -1).trim()
if (mediaQueryName === '') {
result.warn(
EXPORT_FUNC_IDENTIFIER + '() should contain non-whitespace custom media query',
{ node: decl }
)
decl.remove()
return
}
if (!customMediasMap.hasOwnProperty(mediaQueryName)) {
result.warn(
'Cannot resolve custom media ' + mediaQueryName,
{ node: decl }
)
decl.remove()
return
}
var clone = decl.cloneBefore()
clone.value = customMediasMap[mediaQueryName]
decl.remove()
})
}
}
module.exports = postcss.plugin('postcss-export-custom-media', exportCustomMedia)