-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
43 lines (35 loc) · 1.26 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
const postcss = require('postcss');
const regViewportUnit = /\d(vw|vh|vmax|vmin)\b/;
const CONTENT_PROP = 'content';
const PREFIX = 'viewport-units-buggyfill';
module.exports = postcss.plugin('postcss-viewport-units', options => (root, result) => {
const opts = options || {};
const test = opts.test || (value => regViewportUnit.test(value));
const { silence = false } = opts;
if (opts.filterFile && !opts.filterFile(root.source.input.file || '')) return;
root.walkRules((rule) => {
let hasContent;
const viewportUnitDecls = [];
if (opts.filterRule && !opts.filterRule(rule)) return;
rule.nodes.slice(0).forEach((decl) => {
if (decl.prop === CONTENT_PROP && !hasContent) {
hasContent = true;
}
if (test(decl.value)) {
if (opts.onlyCalc && decl.value.indexOf('calc') !== 0) return;
viewportUnitDecls.push(`${decl.prop}: ${decl.value}`);
}
});
if (viewportUnitDecls.length === 0) return;
if (hasContent) {
if (!silence) {
rule.warn(result, `'${rule.selector}' already has a 'content' property, give up to overwrite it.`);
}
} else {
rule.append({
prop: CONTENT_PROP,
value: `'${[PREFIX].concat(viewportUnitDecls).join('; ')}'`,
});
}
});
});