1 | "use strict";
|
2 |
|
3 | var _cssTree = _interopRequireDefault(require("css-tree"));
|
4 |
|
5 | var _cssMediaquery = _interopRequireDefault(require("css-mediaquery"));
|
6 |
|
7 | var _debug = _interopRequireDefault(require("debug"));
|
8 |
|
9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
10 |
|
11 | const debuglog = (0, _debug.default)('penthouse:preformatting:nonMatchingMediaQueryRemover');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function _isMatchingMediaQuery(mediaQuery, matchConfig) {
|
18 |
|
19 | let mediaAST;
|
20 |
|
21 | try {
|
22 | mediaAST = _cssMediaquery.default.parse(mediaQuery);
|
23 | } catch (e) {
|
24 |
|
25 | return false;
|
26 | }
|
27 |
|
28 | var keep = mediaAST.some(function (mq) {
|
29 |
|
30 |
|
31 |
|
32 | const isInverse = mq.inverse || mq.type === 'not';
|
33 |
|
34 |
|
35 |
|
36 | if (mq.expressions.length === 0) {
|
37 |
|
38 | if (!isInverse && mq.type === 'print' || isInverse && mq.type === 'screen') {
|
39 | return false;
|
40 | }
|
41 |
|
42 | return true;
|
43 | }
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | return mq.expressions.some(function ({
|
54 | modifier,
|
55 | feature,
|
56 | value
|
57 | }) {
|
58 | if (modifier === 'min') {
|
59 | const constructedQuery = `(min-${feature}: ${value})`;
|
60 |
|
61 | if (!isInverse) {
|
62 | return _cssMediaquery.default.match(constructedQuery, matchConfig);
|
63 | } else {
|
64 | return !_cssMediaquery.default.match(constructedQuery, matchConfig);
|
65 | }
|
66 | } else {
|
67 | if (mq.expressions.length > 1) {
|
68 | const constructedQuery = mq.expressions.map(({
|
69 | modifier,
|
70 | feature,
|
71 | value
|
72 | }) => `(${modifier}-${feature}: ${value})`).join(' and ');
|
73 |
|
74 | if (!isInverse) {
|
75 | return _cssMediaquery.default.match(constructedQuery, matchConfig);
|
76 | } else {
|
77 | return !_cssMediaquery.default.match(constructedQuery, matchConfig);
|
78 | }
|
79 | } else {
|
80 | return true;
|
81 | }
|
82 | }
|
83 | });
|
84 | });
|
85 | return keep;
|
86 | }
|
87 |
|
88 | function nonMatchingMediaQueryRemover(ast, width, height, keepLargerMediaQueries = false) {
|
89 | debuglog('BEFORE');
|
90 | const matchConfig = {
|
91 | type: 'screen',
|
92 | width: width + 'px',
|
93 | height: height + 'px'
|
94 | };
|
95 | const matchLargerMQConfig = {
|
96 | type: 'screen',
|
97 | width: '99999px',
|
98 | height: '99999px'
|
99 | };
|
100 | debuglog('matchConfig: ' + JSON.stringify(matchConfig, null, 2) + '\n' + 'keepLargerMediaQueries: ' + keepLargerMediaQueries);
|
101 |
|
102 | _cssTree.default.walk(ast, {
|
103 | visit: 'Atrule',
|
104 | enter: (atrule, item, list) => {
|
105 |
|
106 | if (_cssTree.default.keyword(atrule.name).name !== 'media') {
|
107 | return;
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 | if (!atrule.prelude) {
|
113 | return;
|
114 | }
|
115 |
|
116 | const mediaQuery = _cssTree.default.generate(atrule.prelude);
|
117 |
|
118 |
|
119 |
|
120 | const isMatching = keepLargerMediaQueries ? _isMatchingMediaQuery(mediaQuery, matchConfig) || _isMatchingMediaQuery(mediaQuery, matchLargerMQConfig) : _isMatchingMediaQuery(mediaQuery, matchConfig);
|
121 |
|
122 | if (!isMatching) {
|
123 | debuglog('DROP: ' + `(${mediaQuery}), `);
|
124 | list.remove(item);
|
125 | } else {
|
126 | debuglog('KEEP: ' + `(${mediaQuery}), `);
|
127 | }
|
128 | }
|
129 | });
|
130 |
|
131 | return ast;
|
132 | }
|
133 |
|
134 | module.exports = nonMatchingMediaQueryRemover; |
\ | No newline at end of file |