-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
54 lines (46 loc) · 1.46 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
/**
* Constants.
*/
var EXTENSION_RE = /\((--[\w-]+)\)/;
/**
* Module export.
*/
module.exports = function customMedia(options) {
return function (ast) {
options = options || {};
var map = options.map || {};
var indices = [];
// define custom media query aliases
ast.rules.forEach(function (rule, i) {
if (rule.type !== 'custom-media') return;
map[rule.name] = rule.media;
indices.push(i);
});
// substitute custom media query aliases
ast.rules.forEach(function (rule, i) {
if (rule.type !== 'media') return;
rule.media = rule.media.replace(EXTENSION_RE, function(_, name) {
var replacement = map[name];
var column = rule.position.start.column;
var line = rule.position.start.line;
var source = rule.position.source;
if (replacement) {
return replacement;
} else {
console.warn(
'WARNING: undefined CSS custom media alias "' + name + '" at ' +
line + ':' + column + (source ? ' in ' + source : '') + '.\n' +
'The rule has been removed from the output. Please check your ' +
'@custom-media definitions.'
);
indices.push(i);
}
});
});
// remove @custom-media blocks from css in reverse order to avoid affecting
// indices before they are removed
for (var i = indices.length - 1; i >= 0; i -= 1) {
ast.rules.splice(indices[i], 1);
}
};
};