cbvirtua 1.0.29 → 1.0.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/.postcssrc.js +18 -0
  2. package/p2.js +334 -0
  3. package/package.json +1 -1
package/.postcssrc.js ADDED
@@ -0,0 +1,18 @@
1
+ const p2 = require("./p2");
2
+
3
+ module.exports = ({ file }) => {
4
+ return {
5
+ plugins: [
6
+ p2({
7
+ unitToConvert: 'px',
8
+ viewportWidth: 320,
9
+ viewportHeight: 568, // not now used; TODO: need for different units and math for different properties
10
+ unitPrecision: 5,
11
+ viewportUnit: 'vw',
12
+ fontViewportUnit: 'vw', // vmin is more suitable.
13
+ selectorBlackList: [],
14
+ propList: ['*']
15
+ }),
16
+ ],
17
+ };
18
+ };
package/p2.js ADDED
@@ -0,0 +1,334 @@
1
+ 'use strict';
2
+
3
+ var postcss = require('postcss');
4
+ var objectAssign = require('object-assign');
5
+ function getUnitRegexp(unit) {
6
+ return new RegExp('"[^"]+"|\'[^\']+\'|url\\([^\\)]+\\)|(\\d*\\.?\\d+)' + unit, 'g');
7
+ }
8
+
9
+ var defaults = {
10
+ unitToConvert: 'px',
11
+ viewportWidth: 320,
12
+ viewportHeight: 568, // not now used; TODO: need for different units and math for different properties
13
+ unitPrecision: 5,
14
+ viewportUnit: 'vw',
15
+ fontViewportUnit: 'vw', // vmin is more suitable.
16
+ selectorBlackList: [],
17
+ propList: ['*'],
18
+ minPixelValue: 1,
19
+ mediaQuery: false,
20
+ replace: true,
21
+ landscape: false,
22
+ landscapeUnit: 'vw',
23
+ landscapeWidth: 568
24
+ };
25
+
26
+ var ignoreNextComment = 'px-to-viewport-ignore-next';
27
+ var ignorePrevComment = 'px-to-viewport-ignore';
28
+
29
+ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
30
+ var opts = Object.assign({}, defaults, options);
31
+
32
+ checkRegExpOrArray(opts, 'exclude');
33
+ checkRegExpOrArray(opts, 'include');
34
+
35
+ var pxRegex = getUnitRegexp(opts.unitToConvert);
36
+ var nRule = [];
37
+
38
+ return function (css, result) {
39
+ console.log('css222222', css.source.input.file)
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
+ console.log('file-------', file)
44
+ const _rule = rule.clone()
45
+ _rule.selector = 'div'
46
+ _rule.walkDecls(function (decl, i) {
47
+ var unit;
48
+ var size;
49
+ unit = getUnit(decl.prop, opts);
50
+ size = opts.viewportWidth;
51
+
52
+ var value = decl.value.replace(pxRegex, createPxReplace(opts, unit, size));
53
+
54
+ if (declarationExists(decl.parent, decl.prop, value)) return;
55
+
56
+ if (opts.replace) {
57
+ decl.value = value;
58
+ } else {
59
+ decl.parent.insertAfter(i, decl.clone({ value: value }));
60
+ }
61
+ });
62
+ nRule.push(_rule)
63
+
64
+ });
65
+ nRule.forEach((rule) => {
66
+ css.append(rule)
67
+ })
68
+ };
69
+ });
70
+
71
+ function getUnit(prop, opts) {
72
+ return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit;
73
+ }
74
+
75
+ function createPxReplace(opts, viewportUnit, viewportSize) {
76
+ return function (m, $1) {
77
+ if (!$1) return m;
78
+ var pixels = parseFloat($1);
79
+ if (pixels <= opts.minPixelValue) return m;
80
+ var parsedVal = toFixed((pixels / viewportSize * 100), opts.unitPrecision);
81
+ return parsedVal === 0 ? '0' : parsedVal + viewportUnit;
82
+ };
83
+ }
84
+
85
+ function error(decl, message) {
86
+ throw decl.error(message, { plugin: 'postcss-px-to-viewport' });
87
+ }
88
+
89
+ function checkRegExpOrArray(options, optionName) {
90
+ var option = options[optionName];
91
+ if (!option) return;
92
+ if (Object.prototype.toString.call(option) === '[object RegExp]') return;
93
+ if (Object.prototype.toString.call(option) === '[object Array]') {
94
+ var bad = false;
95
+ for (var i = 0; i < option.length; i++) {
96
+ if (Object.prototype.toString.call(option[i]) !== '[object RegExp]') {
97
+ bad = true;
98
+ break;
99
+ }
100
+ }
101
+ if (!bad) return;
102
+ }
103
+ throw new Error('options.' + optionName + ' should be RegExp or Array of RegExp.');
104
+ }
105
+
106
+ function toFixed(number, precision) {
107
+ var multiplier = Math.pow(10, precision + 1),
108
+ wholeNumber = Math.floor(number * multiplier);
109
+ return Math.round(wholeNumber / 10) * 10 / multiplier;
110
+ }
111
+
112
+ function blacklistedSelector(blacklist, selector) {
113
+ if (typeof selector !== 'string') return;
114
+ return blacklist.some(function (regex) {
115
+ if (typeof regex === 'string') return selector.indexOf(regex) !== -1;
116
+ return selector.match(regex);
117
+ });
118
+ }
119
+
120
+ function declarationExists(decls, prop, value) {
121
+ return decls.some(function (decl) {
122
+ return (decl.prop === prop && decl.value === value);
123
+ });
124
+ }
125
+
126
+ function validateParams(params, mediaQuery) {
127
+ return !params || (params && mediaQuery);
128
+ }
129
+
130
+
131
+ 'use strict';
132
+
133
+ var postcss = require('postcss');
134
+ var objectAssign = require('object-assign');
135
+ var { createPropListMatcher } = require('./src/prop-list-matcher');
136
+ var { getUnitRegexp } = require('./src/pixel-unit-regexp');
137
+
138
+ var defaults = {
139
+ unitToConvert: 'px',
140
+ viewportWidth: 320,
141
+ viewportHeight: 568, // not now used; TODO: need for different units and math for different properties
142
+ unitPrecision: 5,
143
+ viewportUnit: 'vw',
144
+ fontViewportUnit: 'vw', // vmin is more suitable.
145
+ selectorBlackList: [],
146
+ propList: ['*'],
147
+ minPixelValue: 1,
148
+ mediaQuery: false,
149
+ replace: true,
150
+ landscape: false,
151
+ landscapeUnit: 'vw',
152
+ landscapeWidth: 568
153
+ };
154
+
155
+ var ignoreNextComment = 'px-to-viewport-ignore-next';
156
+ var ignorePrevComment = 'px-to-viewport-ignore';
157
+
158
+ module.exports = postcss.plugin('postcss-px-to-viewport', function (options) {
159
+ var opts = objectAssign({}, defaults, options);
160
+
161
+ checkRegExpOrArray(opts, 'exclude');
162
+ checkRegExpOrArray(opts, 'include');
163
+
164
+ var pxRegex = getUnitRegexp(opts.unitToConvert);
165
+ var satisfyPropList = createPropListMatcher(opts.propList);
166
+ var landscapeRules = [];
167
+
168
+ return function (css, result) {
169
+ css.walkRules(function (rule) {
170
+ // Add exclude option to ignore some files like 'node_modules'
171
+ var file = rule.source && rule.source.input.file;
172
+
173
+ if (opts.include && file) {
174
+ if (Object.prototype.toString.call(opts.include) === '[object RegExp]') {
175
+ if (!opts.include.test(file)) return;
176
+ } else if (Object.prototype.toString.call(opts.include) === '[object Array]') {
177
+ var flag = false;
178
+ for (var i = 0; i < opts.include.length; i++) {
179
+ if (opts.include[i].test(file)) {
180
+ flag = true;
181
+ break;
182
+ }
183
+ }
184
+ if (!flag) return;
185
+ }
186
+ }
187
+
188
+ if (opts.exclude && file) {
189
+ if (Object.prototype.toString.call(opts.exclude) === '[object RegExp]') {
190
+ if (opts.exclude.test(file)) return;
191
+ } else if (Object.prototype.toString.call(opts.exclude) === '[object Array]') {
192
+ for (var i = 0; i < opts.exclude.length; i++) {
193
+ if (opts.exclude[i].test(file)) return;
194
+ }
195
+ }
196
+ }
197
+
198
+ if (blacklistedSelector(opts.selectorBlackList, rule.selector)) return;
199
+
200
+ if (opts.landscape && !rule.parent.params) {
201
+ var landscapeRule = rule.clone().removeAll();
202
+
203
+ rule.walkDecls(function(decl) {
204
+ if (decl.value.indexOf(opts.unitToConvert) === -1) return;
205
+ if (!satisfyPropList(decl.prop)) return;
206
+
207
+ landscapeRule.append(decl.clone({
208
+ value: decl.value.replace(pxRegex, createPxReplace(opts, opts.landscapeUnit, opts.landscapeWidth))
209
+ }));
210
+ });
211
+
212
+ if (landscapeRule.nodes.length > 0) {
213
+ landscapeRules.push(landscapeRule);
214
+ }
215
+ }
216
+
217
+ if (!validateParams(rule.parent.params, opts.mediaQuery)) return;
218
+
219
+ rule.walkDecls(function(decl, i) {
220
+ if (decl.value.indexOf(opts.unitToConvert) === -1) return;
221
+ if (!satisfyPropList(decl.prop)) return;
222
+
223
+ var prev = decl.prev();
224
+ // prev declaration is ignore conversion comment at same line
225
+ if (prev && prev.type === 'comment' && prev.text === ignoreNextComment) {
226
+ // remove comment
227
+ prev.remove();
228
+ return;
229
+ }
230
+ var next = decl.next();
231
+ // next declaration is ignore conversion comment at same line
232
+ if (next && next.type === 'comment' && next.text === ignorePrevComment) {
233
+ if (/\n/.test(next.raws.before)) {
234
+ result.warn('Unexpected comment /* ' + ignorePrevComment + ' */ must be after declaration at same line.', { node: next });
235
+ } else {
236
+ // remove comment
237
+ next.remove();
238
+ return;
239
+ }
240
+ }
241
+
242
+ var unit;
243
+ var size;
244
+ var params = rule.parent.params;
245
+
246
+ if (opts.landscape && params && params.indexOf('landscape') !== -1) {
247
+ unit = opts.landscapeUnit;
248
+ size = opts.landscapeWidth;
249
+ } else {
250
+ unit = getUnit(decl.prop, opts);
251
+ size = opts.viewportWidth;
252
+ }
253
+
254
+ var value = decl.value.replace(pxRegex, createPxReplace(opts, unit, size));
255
+
256
+ if (declarationExists(decl.parent, decl.prop, value)) return;
257
+
258
+ if (opts.replace) {
259
+ decl.value = value;
260
+ } else {
261
+ decl.parent.insertAfter(i, decl.clone({ value: value }));
262
+ }
263
+ });
264
+ });
265
+
266
+ if (landscapeRules.length > 0) {
267
+ var landscapeRoot = new postcss.atRule({ params: '(orientation: landscape)', name: 'media' });
268
+
269
+ landscapeRules.forEach(function(rule) {
270
+ landscapeRoot.append(rule);
271
+ });
272
+ css.append(landscapeRoot);
273
+ }
274
+ };
275
+ });
276
+
277
+ function getUnit(prop, opts) {
278
+ return prop.indexOf('font') === -1 ? opts.viewportUnit : opts.fontViewportUnit;
279
+ }
280
+
281
+ function createPxReplace(opts, viewportUnit, viewportSize) {
282
+ return function (m, $1) {
283
+ if (!$1) return m;
284
+ var pixels = parseFloat($1);
285
+ if (pixels <= opts.minPixelValue) return m;
286
+ var parsedVal = toFixed((pixels / viewportSize * 100), opts.unitPrecision);
287
+ return parsedVal === 0 ? '0' : parsedVal + viewportUnit;
288
+ };
289
+ }
290
+
291
+ function error(decl, message) {
292
+ throw decl.error(message, { plugin: 'postcss-px-to-viewport' });
293
+ }
294
+
295
+ function checkRegExpOrArray(options, optionName) {
296
+ var option = options[optionName];
297
+ if (!option) return;
298
+ if (Object.prototype.toString.call(option) === '[object RegExp]') return;
299
+ if (Object.prototype.toString.call(option) === '[object Array]') {
300
+ var bad = false;
301
+ for (var i = 0; i < option.length; i++) {
302
+ if (Object.prototype.toString.call(option[i]) !== '[object RegExp]') {
303
+ bad = true;
304
+ break;
305
+ }
306
+ }
307
+ if (!bad) return;
308
+ }
309
+ throw new Error('options.' + optionName + ' should be RegExp or Array of RegExp.');
310
+ }
311
+
312
+ function toFixed(number, precision) {
313
+ var multiplier = Math.pow(10, precision + 1),
314
+ wholeNumber = Math.floor(number * multiplier);
315
+ return Math.round(wholeNumber / 10) * 10 / multiplier;
316
+ }
317
+
318
+ function blacklistedSelector(blacklist, selector) {
319
+ if (typeof selector !== 'string') return;
320
+ return blacklist.some(function (regex) {
321
+ if (typeof regex === 'string') return selector.indexOf(regex) !== -1;
322
+ return selector.match(regex);
323
+ });
324
+ }
325
+
326
+ function declarationExists(decls, prop, value) {
327
+ return decls.some(function (decl) {
328
+ return (decl.prop === prop && decl.value === value);
329
+ });
330
+ }
331
+
332
+ function validateParams(params, mediaQuery) {
333
+ return !params || (params && mediaQuery);
334
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cbvirtua",
3
- "version": "1.0.29",
3
+ "version": "1.0.30",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {