Skip to content

Commit 362b031

Browse files
author
xiongshiji
committed
perf: support postcss8
1 parent 9d6c5ce commit 362b031

File tree

2 files changed

+104
-100
lines changed

2 files changed

+104
-100
lines changed

index.js

Lines changed: 102 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var defaults = {
2525
var ignoreNextComment = 'px-to-viewport-ignore-next';
2626
var ignorePrevComment = 'px-to-viewport-ignore';
2727

28-
module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
28+
module.exports = function (options) {
2929
var opts = objectAssign({}, defaults, options);
3030

3131
checkRegExpOrArray(opts, 'exclude');
@@ -34,115 +34,119 @@ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
3434
var pxRegex = getUnitRegexp(opts.unitToConvert);
3535
var satisfyPropList = createPropListMatcher(opts.propList);
3636
var landscapeRules = [];
37-
38-
return function (css, result) {
39-
css.walkRules(function (rule) {
40-
// Add exclude option to ignore some files like 'node_modules'
41-
var file = rule.source && rule.source.input.file;
42-
43-
if (opts.include && file) {
44-
if (Object.prototype.toString.call(opts.include) === '[object RegExp]') {
45-
if (!opts.include.test(file)) return;
46-
} else if (Object.prototype.toString.call(opts.include) === '[object Array]') {
47-
var flag = false;
48-
for (var i = 0; i < opts.include.length; i++) {
49-
if (opts.include[i].test(file)) {
50-
flag = true;
51-
break;
37+
return {
38+
postcssPlugin: 'px-to-viewport-ignore',
39+
Once(css, { result }) {
40+
css.walkRules(function (rule) {
41+
// Add exclude option to ignore some files like 'node_modules'
42+
var file = rule.source && rule.source.input.file;
43+
44+
if (opts.include && file) {
45+
if (Object.prototype.toString.call(opts.include) === '[object RegExp]') {
46+
if (!opts.include.test(file)) return;
47+
} else if (Object.prototype.toString.call(opts.include) === '[object Array]') {
48+
var flag = false;
49+
for (var i = 0; i < opts.include.length; i++) {
50+
if (opts.include[i].test(file)) {
51+
flag = true;
52+
break;
53+
}
5254
}
55+
if (!flag) return;
5356
}
54-
if (!flag) return;
5557
}
56-
}
57-
58-
if (opts.exclude && file) {
59-
if (Object.prototype.toString.call(opts.exclude) === '[object RegExp]') {
60-
if (opts.exclude.test(file)) return;
61-
} else if (Object.prototype.toString.call(opts.exclude) === '[object Array]') {
62-
for (var i = 0; i < opts.exclude.length; i++) {
63-
if (opts.exclude[i].test(file)) return;
58+
59+
if (opts.exclude && file) {
60+
if (Object.prototype.toString.call(opts.exclude) === '[object RegExp]') {
61+
if (opts.exclude.test(file)) return;
62+
} else if (Object.prototype.toString.call(opts.exclude) === '[object Array]') {
63+
for (var i = 0; i < opts.exclude.length; i++) {
64+
if (opts.exclude[i].test(file)) return;
65+
}
6466
}
6567
}
66-
}
67-
68-
if (blacklistedSelector(opts.selectorBlackList, rule.selector)) return;
69-
70-
if (opts.landscape && !rule.parent.params) {
71-
var landscapeRule = rule.clone().removeAll();
72-
73-
rule.walkDecls(function(decl) {
68+
69+
if (blacklistedSelector(opts.selectorBlackList, rule.selector)) return;
70+
71+
if (opts.landscape && !rule.parent.params) {
72+
var landscapeRule = rule.clone().removeAll();
73+
74+
rule.walkDecls(function(decl) {
75+
if (decl.value.indexOf(opts.unitToConvert) === -1) return;
76+
if (!satisfyPropList(decl.prop)) return;
77+
78+
landscapeRule.append(decl.clone({
79+
value: decl.value.replace(pxRegex, createPxReplace(opts, opts.landscapeUnit, opts.landscapeWidth))
80+
}));
81+
});
82+
83+
if (landscapeRule.nodes.length > 0) {
84+
landscapeRules.push(landscapeRule);
85+
}
86+
}
87+
88+
if (!validateParams(rule.parent.params, opts.mediaQuery)) return;
89+
90+
rule.walkDecls(function(decl, i) {
7491
if (decl.value.indexOf(opts.unitToConvert) === -1) return;
7592
if (!satisfyPropList(decl.prop)) return;
76-
77-
landscapeRule.append(decl.clone({
78-
value: decl.value.replace(pxRegex, createPxReplace(opts, opts.landscapeUnit, opts.landscapeWidth))
79-
}));
80-
});
81-
82-
if (landscapeRule.nodes.length > 0) {
83-
landscapeRules.push(landscapeRule);
84-
}
85-
}
86-
87-
if (!validateParams(rule.parent.params, opts.mediaQuery)) return;
88-
89-
rule.walkDecls(function(decl, i) {
90-
if (decl.value.indexOf(opts.unitToConvert) === -1) return;
91-
if (!satisfyPropList(decl.prop)) return;
92-
93-
var prev = decl.prev();
94-
// prev declaration is ignore conversion comment at same line
95-
if (prev && prev.type === 'comment' && prev.text === ignoreNextComment) {
96-
// remove comment
97-
prev.remove();
98-
return;
99-
}
100-
var next = decl.next();
101-
// next declaration is ignore conversion comment at same line
102-
if (next && next.type === 'comment' && next.text === ignorePrevComment) {
103-
if (/\n/.test(next.raws.before)) {
104-
result.warn('Unexpected comment /* ' + ignorePrevComment + ' */ must be after declaration at same line.', { node: next });
105-
} else {
93+
94+
var prev = decl.prev();
95+
// prev declaration is ignore conversion comment at same line
96+
if (prev && prev.type === 'comment' && prev.text === ignoreNextComment) {
10697
// remove comment
107-
next.remove();
98+
prev.remove();
10899
return;
109100
}
110-
}
111-
112-
var unit;
113-
var size;
114-
var params = rule.parent.params;
115-
116-
if (opts.landscape && params && params.indexOf('landscape') !== -1) {
117-
unit = opts.landscapeUnit;
118-
size = opts.landscapeWidth;
119-
} else {
120-
unit = getUnit(decl.prop, opts);
121-
size = opts.viewportWidth;
122-
}
123-
124-
var value = decl.value.replace(pxRegex, createPxReplace(opts, unit, size));
125-
126-
if (declarationExists(decl.parent, decl.prop, value)) return;
127-
128-
if (opts.replace) {
129-
decl.value = value;
130-
} else {
131-
decl.parent.insertAfter(i, decl.clone({ value: value }));
132-
}
133-
});
134-
});
135-
136-
if (landscapeRules.length > 0) {
137-
var landscapeRoot = new postcss.atRule({ params: '(orientation: landscape)', name: 'media' });
138-
139-
landscapeRules.forEach(function(rule) {
140-
landscapeRoot.append(rule);
101+
var next = decl.next();
102+
// next declaration is ignore conversion comment at same line
103+
if (next && next.type === 'comment' && next.text === ignorePrevComment) {
104+
if (/\n/.test(next.raws.before)) {
105+
result.warn('Unexpected comment /* ' + ignorePrevComment + ' */ must be after declaration at same line.', { node: next });
106+
} else {
107+
// remove comment
108+
next.remove();
109+
return;
110+
}
111+
}
112+
113+
var unit;
114+
var size;
115+
var params = rule.parent.params;
116+
117+
if (opts.landscape && params && params.indexOf('landscape') !== -1) {
118+
unit = opts.landscapeUnit;
119+
size = opts.landscapeWidth;
120+
} else {
121+
unit = getUnit(decl.prop, opts);
122+
size = opts.viewportWidth;
123+
}
124+
125+
var value = decl.value.replace(pxRegex, createPxReplace(opts, unit, size));
126+
127+
if (declarationExists(decl.parent, decl.prop, value)) return;
128+
129+
if (opts.replace) {
130+
decl.value = value;
131+
} else {
132+
decl.parent.insertAfter(i, decl.clone({ value: value }));
133+
}
134+
});
141135
});
142-
css.append(landscapeRoot);
136+
137+
if (landscapeRules.length > 0) {
138+
var landscapeRoot = new postcss.atRule({ params: '(orientation: landscape)', name: 'media' });
139+
140+
landscapeRules.forEach(function(rule) {
141+
landscapeRoot.append(rule);
142+
});
143+
css.append(landscapeRoot);
144+
}
143145
}
144-
};
145-
});
146+
}
147+
}
148+
149+
module.exports.postcss = true
146150

147151
function getUnit(prop, opts) {
148152
return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
},
3333
"devDependencies": {
3434
"jest": "^25.4.0",
35-
"postcss": ">=5.0.2"
35+
"postcss": "^8.0.0"
3636
},
3737
"peerDependencies": {
38-
"postcss": ">=5.0.2"
38+
"postcss": "^8.0.0"
3939
}
4040
}

0 commit comments

Comments
 (0)