source: trip-planner-front/node_modules/postcss-custom-media/index.es.mjs@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 22.1 KB
RevLine 
[6a3a178]1import postcss from 'postcss';
2import fs from 'fs';
3import path from 'path';
4
5function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
6 try {
7 var info = gen[key](arg);
8 var value = info.value;
9 } catch (error) {
10 reject(error);
11 return;
12 }
13
14 if (info.done) {
15 resolve(value);
16 } else {
17 Promise.resolve(value).then(_next, _throw);
18 }
19}
20
21function _asyncToGenerator(fn) {
22 return function () {
23 var self = this,
24 args = arguments;
25 return new Promise(function (resolve, reject) {
26 var gen = fn.apply(self, args);
27
28 function _next(value) {
29 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
30 }
31
32 function _throw(err) {
33 asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
34 }
35
36 _next(undefined);
37 });
38 };
39}
40
41function _defineProperty(obj, key, value) {
42 if (key in obj) {
43 Object.defineProperty(obj, key, {
44 value: value,
45 enumerable: true,
46 configurable: true,
47 writable: true
48 });
49 } else {
50 obj[key] = value;
51 }
52
53 return obj;
54}
55
56function _objectSpread(target) {
57 for (var i = 1; i < arguments.length; i++) {
58 var source = arguments[i] != null ? arguments[i] : {};
59 var ownKeys = Object.keys(source);
60
61 if (typeof Object.getOwnPropertySymbols === 'function') {
62 ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) {
63 return Object.getOwnPropertyDescriptor(source, sym).enumerable;
64 }));
65 }
66
67 ownKeys.forEach(function (key) {
68 _defineProperty(target, key, source[key]);
69 });
70 }
71
72 return target;
73}
74
75function _slicedToArray(arr, i) {
76 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
77}
78
79function _arrayWithHoles(arr) {
80 if (Array.isArray(arr)) return arr;
81}
82
83function _iterableToArrayLimit(arr, i) {
84 var _arr = [];
85 var _n = true;
86 var _d = false;
87 var _e = undefined;
88
89 try {
90 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
91 _arr.push(_s.value);
92
93 if (i && _arr.length === i) break;
94 }
95 } catch (err) {
96 _d = true;
97 _e = err;
98 } finally {
99 try {
100 if (!_n && _i["return"] != null) _i["return"]();
101 } finally {
102 if (_d) throw _e;
103 }
104 }
105
106 return _arr;
107}
108
109function _nonIterableRest() {
110 throw new TypeError("Invalid attempt to destructure non-iterable instance");
111}
112
113function parse(string, splitByAnd) {
114 const array = [];
115 let buffer = '';
116 let split = false;
117 let func = 0;
118 let i = -1;
119
120 while (++i < string.length) {
121 const char = string[i];
122
123 if (char === '(') {
124 func += 1;
125 } else if (char === ')') {
126 if (func > 0) {
127 func -= 1;
128 }
129 } else if (func === 0) {
130 if (splitByAnd && andRegExp.test(buffer + char)) {
131 split = true;
132 } else if (!splitByAnd && char === ',') {
133 split = true;
134 }
135 }
136
137 if (split) {
138 array.push(splitByAnd ? new MediaExpression(buffer + char) : new MediaQuery(buffer));
139 buffer = '';
140 split = false;
141 } else {
142 buffer += char;
143 }
144 }
145
146 if (buffer !== '') {
147 array.push(splitByAnd ? new MediaExpression(buffer) : new MediaQuery(buffer));
148 }
149
150 return array;
151}
152
153class MediaQueryList {
154 constructor(string) {
155 this.nodes = parse(string);
156 }
157
158 invert() {
159 this.nodes.forEach(node => {
160 node.invert();
161 });
162 return this;
163 }
164
165 clone() {
166 return new MediaQueryList(String(this));
167 }
168
169 toString() {
170 return this.nodes.join(',');
171 }
172
173}
174
175class MediaQuery {
176 constructor(string) {
177 const _string$match = string.match(spaceWrapRegExp),
178 _string$match2 = _slicedToArray(_string$match, 4),
179 before = _string$match2[1],
180 media = _string$match2[2],
181 after = _string$match2[3];
182
183 const _ref = media.match(mediaRegExp) || [],
184 _ref2 = _slicedToArray(_ref, 9),
185 _ref2$ = _ref2[1],
186 modifier = _ref2$ === void 0 ? '' : _ref2$,
187 _ref2$2 = _ref2[2],
188 afterModifier = _ref2$2 === void 0 ? ' ' : _ref2$2,
189 _ref2$3 = _ref2[3],
190 type = _ref2$3 === void 0 ? '' : _ref2$3,
191 _ref2$4 = _ref2[4],
192 beforeAnd = _ref2$4 === void 0 ? '' : _ref2$4,
193 _ref2$5 = _ref2[5],
194 and = _ref2$5 === void 0 ? '' : _ref2$5,
195 _ref2$6 = _ref2[6],
196 beforeExpression = _ref2$6 === void 0 ? '' : _ref2$6,
197 _ref2$7 = _ref2[7],
198 expression1 = _ref2$7 === void 0 ? '' : _ref2$7,
199 _ref2$8 = _ref2[8],
200 expression2 = _ref2$8 === void 0 ? '' : _ref2$8;
201
202 const raws = {
203 before,
204 after,
205 afterModifier,
206 originalModifier: modifier || '',
207 beforeAnd,
208 and,
209 beforeExpression
210 };
211 const nodes = parse(expression1 || expression2, true);
212 Object.assign(this, {
213 modifier,
214 type,
215 raws,
216 nodes
217 });
218 }
219
220 clone(overrides) {
221 const instance = new MediaQuery(String(this));
222 Object.assign(instance, overrides);
223 return instance;
224 }
225
226 invert() {
227 this.modifier = this.modifier ? '' : this.raws.originalModifier;
228 return this;
229 }
230
231 toString() {
232 const raws = this.raws;
233 return `${raws.before}${this.modifier}${this.modifier ? `${raws.afterModifier}` : ''}${this.type}${raws.beforeAnd}${raws.and}${raws.beforeExpression}${this.nodes.join('')}${this.raws.after}`;
234 }
235
236}
237
238class MediaExpression {
239 constructor(string) {
240 const _ref3 = string.match(andRegExp) || [null, string],
241 _ref4 = _slicedToArray(_ref3, 5),
242 value = _ref4[1],
243 _ref4$ = _ref4[2],
244 after = _ref4$ === void 0 ? '' : _ref4$,
245 _ref4$2 = _ref4[3],
246 and = _ref4$2 === void 0 ? '' : _ref4$2,
247 _ref4$3 = _ref4[4],
248 afterAnd = _ref4$3 === void 0 ? '' : _ref4$3;
249
250 const raws = {
251 after,
252 and,
253 afterAnd
254 };
255 Object.assign(this, {
256 value,
257 raws
258 });
259 }
260
261 clone(overrides) {
262 const instance = new MediaExpression(String(this));
263 Object.assign(instance, overrides);
264 return instance;
265 }
266
267 toString() {
268 const raws = this.raws;
269 return `${this.value}${raws.after}${raws.and}${raws.afterAnd}`;
270 }
271
272}
273
274const modifierRE = '(not|only)';
275const typeRE = '(all|print|screen|speech)';
276const noExpressionRE = '([\\W\\w]*)';
277const expressionRE = '([\\W\\w]+)';
278const noSpaceRE = '(\\s*)';
279const spaceRE = '(\\s+)';
280const andRE = '(?:(\\s+)(and))';
281const andRegExp = new RegExp(`^${expressionRE}(?:${andRE}${spaceRE})$`, 'i');
282const spaceWrapRegExp = new RegExp(`^${noSpaceRE}${noExpressionRE}${noSpaceRE}$`);
283const mediaRegExp = new RegExp(`^(?:${modifierRE}${spaceRE})?(?:${typeRE}(?:${andRE}${spaceRE}${expressionRE})?|${expressionRE})$`, 'i');
284var mediaASTFromString = (string => new MediaQueryList(string));
285
286var getCustomMediaFromRoot = ((root, opts) => {
287 // initialize custom selectors
288 const customMedias = {}; // for each custom selector atrule that is a child of the css root
289
290 root.nodes.slice().forEach(node => {
291 if (isCustomMedia(node)) {
292 // extract the name and selectors from the params of the custom selector
293 const _node$params$match = node.params.match(customMediaParamsRegExp),
294 _node$params$match2 = _slicedToArray(_node$params$match, 3),
295 name = _node$params$match2[1],
296 selectors = _node$params$match2[2]; // write the parsed selectors to the custom selector
297
298
299 customMedias[name] = mediaASTFromString(selectors); // conditionally remove the custom selector atrule
300
301 if (!Object(opts).preserve) {
302 node.remove();
303 }
304 }
305 });
306 return customMedias;
307}); // match the custom selector name
308
309const customMediaNameRegExp = /^custom-media$/i; // match the custom selector params
310
311const customMediaParamsRegExp = /^(--[A-z][\w-]*)\s+([\W\w]+)\s*$/; // whether the atrule is a custom selector
312
313const isCustomMedia = node => node.type === 'atrule' && customMediaNameRegExp.test(node.name) && customMediaParamsRegExp.test(node.params);
314
315/* Get Custom Media from CSS File
316/* ========================================================================== */
317
318function getCustomMediaFromCSSFile(_x) {
319 return _getCustomMediaFromCSSFile.apply(this, arguments);
320}
321/* Get Custom Media from Object
322/* ========================================================================== */
323
324
325function _getCustomMediaFromCSSFile() {
326 _getCustomMediaFromCSSFile = _asyncToGenerator(function* (from) {
327 const css = yield readFile(from);
328 const root = postcss.parse(css, {
329 from
330 });
331 return getCustomMediaFromRoot(root, {
332 preserve: true
333 });
334 });
335 return _getCustomMediaFromCSSFile.apply(this, arguments);
336}
337
338function getCustomMediaFromObject(object) {
339 const customMedia = Object.assign({}, Object(object).customMedia, Object(object)['custom-media']);
340
341 for (const key in customMedia) {
342 customMedia[key] = mediaASTFromString(customMedia[key]);
343 }
344
345 return customMedia;
346}
347/* Get Custom Media from JSON file
348/* ========================================================================== */
349
350
351function getCustomMediaFromJSONFile(_x2) {
352 return _getCustomMediaFromJSONFile.apply(this, arguments);
353}
354/* Get Custom Media from JS file
355/* ========================================================================== */
356
357
358function _getCustomMediaFromJSONFile() {
359 _getCustomMediaFromJSONFile = _asyncToGenerator(function* (from) {
360 const object = yield readJSON(from);
361 return getCustomMediaFromObject(object);
362 });
363 return _getCustomMediaFromJSONFile.apply(this, arguments);
364}
365
366function getCustomMediaFromJSFile(_x3) {
367 return _getCustomMediaFromJSFile.apply(this, arguments);
368}
369/* Get Custom Media from Sources
370/* ========================================================================== */
371
372
373function _getCustomMediaFromJSFile() {
374 _getCustomMediaFromJSFile = _asyncToGenerator(function* (from) {
375 const object = yield import(from);
376 return getCustomMediaFromObject(object);
377 });
378 return _getCustomMediaFromJSFile.apply(this, arguments);
379}
380
381function getCustomMediaFromSources(sources) {
382 return sources.map(source => {
383 if (source instanceof Promise) {
384 return source;
385 } else if (source instanceof Function) {
386 return source();
387 } // read the source as an object
388
389
390 const opts = source === Object(source) ? source : {
391 from: String(source)
392 }; // skip objects with custom media
393
394 if (Object(opts).customMedia || Object(opts)['custom-media']) {
395 return opts;
396 } // source pathname
397
398
399 const from = path.resolve(String(opts.from || '')); // type of file being read from
400
401 const type = (opts.type || path.extname(from).slice(1)).toLowerCase();
402 return {
403 type,
404 from
405 };
406 }).reduce(
407 /*#__PURE__*/
408 function () {
409 var _ref = _asyncToGenerator(function* (customMedia, source) {
410 const _ref2 = yield source,
411 type = _ref2.type,
412 from = _ref2.from;
413
414 if (type === 'css' || type === 'pcss') {
415 return Object.assign((yield customMedia), (yield getCustomMediaFromCSSFile(from)));
416 }
417
418 if (type === 'js') {
419 return Object.assign((yield customMedia), (yield getCustomMediaFromJSFile(from)));
420 }
421
422 if (type === 'json') {
423 return Object.assign((yield customMedia), (yield getCustomMediaFromJSONFile(from)));
424 }
425
426 return Object.assign((yield customMedia), getCustomMediaFromObject((yield source)));
427 });
428
429 return function (_x4, _x5) {
430 return _ref.apply(this, arguments);
431 };
432 }(), {});
433}
434/* Helper utilities
435/* ========================================================================== */
436
437const readFile = from => new Promise((resolve, reject) => {
438 fs.readFile(from, 'utf8', (error, result) => {
439 if (error) {
440 reject(error);
441 } else {
442 resolve(result);
443 }
444 });
445});
446
447const readJSON =
448/*#__PURE__*/
449function () {
450 var _ref3 = _asyncToGenerator(function* (from) {
451 return JSON.parse((yield readFile(from)));
452 });
453
454 return function readJSON(_x6) {
455 return _ref3.apply(this, arguments);
456 };
457}();
458
459// return transformed medias, replacing custom pseudo medias with custom medias
460function transformMediaList(mediaList, customMedias) {
461 let index = mediaList.nodes.length - 1;
462
463 while (index >= 0) {
464 const transformedMedias = transformMedia(mediaList.nodes[index], customMedias);
465
466 if (transformedMedias.length) {
467 mediaList.nodes.splice(index, 1, ...transformedMedias);
468 }
469
470 --index;
471 }
472
473 return mediaList;
474} // return custom pseudo medias replaced with custom medias
475
476function transformMedia(media, customMedias) {
477 const transpiledMedias = [];
478
479 for (const index in media.nodes) {
480 const _media$nodes$index = media.nodes[index],
481 value = _media$nodes$index.value,
482 nodes = _media$nodes$index.nodes;
483 const key = value.replace(customPseudoRegExp, '$1');
484
485 if (key in customMedias) {
486 var _iteratorNormalCompletion = true;
487 var _didIteratorError = false;
488 var _iteratorError = undefined;
489
490 try {
491 for (var _iterator = customMedias[key].nodes[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
492 const replacementMedia = _step.value;
493 // use the first available modifier unless they cancel each other out
494 const modifier = media.modifier !== replacementMedia.modifier ? media.modifier || replacementMedia.modifier : '';
495 const mediaClone = media.clone({
496 modifier,
497 // conditionally use the raws from the first available modifier
498 raws: !modifier || media.modifier ? _objectSpread({}, media.raws) : _objectSpread({}, replacementMedia.raws),
499 type: media.type || replacementMedia.type
500 }); // conditionally include more replacement raws when the type is present
501
502 if (mediaClone.type === replacementMedia.type) {
503 Object.assign(mediaClone.raws, {
504 and: replacementMedia.raws.and,
505 beforeAnd: replacementMedia.raws.beforeAnd,
506 beforeExpression: replacementMedia.raws.beforeExpression
507 });
508 }
509
510 mediaClone.nodes.splice(index, 1, ...replacementMedia.clone().nodes.map(node => {
511 // use raws and spacing from the current usage
512 if (media.nodes[index].raws.and) {
513 node.raws = _objectSpread({}, media.nodes[index].raws);
514 }
515
516 node.spaces = _objectSpread({}, media.nodes[index].spaces);
517 return node;
518 })); // remove the currently transformed key to prevent recursion
519
520 const nextCustomMedia = getCustomMediasWithoutKey(customMedias, key);
521 const retranspiledMedias = transformMedia(mediaClone, nextCustomMedia);
522
523 if (retranspiledMedias.length) {
524 transpiledMedias.push(...retranspiledMedias);
525 } else {
526 transpiledMedias.push(mediaClone);
527 }
528 }
529 } catch (err) {
530 _didIteratorError = true;
531 _iteratorError = err;
532 } finally {
533 try {
534 if (!_iteratorNormalCompletion && _iterator.return != null) {
535 _iterator.return();
536 }
537 } finally {
538 if (_didIteratorError) {
539 throw _iteratorError;
540 }
541 }
542 }
543
544 return transpiledMedias;
545 } else if (nodes && nodes.length) {
546 transformMediaList(media.nodes[index], customMedias);
547 }
548 }
549
550 return transpiledMedias;
551}
552
553const customPseudoRegExp = /\((--[A-z][\w-]*)\)/;
554
555const getCustomMediasWithoutKey = (customMedias, key) => {
556 const nextCustomMedias = Object.assign({}, customMedias);
557 delete nextCustomMedias[key];
558 return nextCustomMedias;
559};
560
561var transformAtrules = ((root, customMedia, opts) => {
562 root.walkAtRules(mediaAtRuleRegExp, atrule => {
563 if (customPseudoRegExp$1.test(atrule.params)) {
564 const mediaAST = mediaASTFromString(atrule.params);
565 const params = String(transformMediaList(mediaAST, customMedia));
566
567 if (opts.preserve) {
568 atrule.cloneBefore({
569 params
570 });
571 } else {
572 atrule.params = params;
573 }
574 }
575 });
576});
577const mediaAtRuleRegExp = /^media$/i;
578const customPseudoRegExp$1 = /\(--[A-z][\w-]*\)/;
579
580/* Write Custom Media from CSS File
581/* ========================================================================== */
582
583function writeCustomMediaToCssFile(_x, _x2) {
584 return _writeCustomMediaToCssFile.apply(this, arguments);
585}
586/* Write Custom Media from JSON file
587/* ========================================================================== */
588
589
590function _writeCustomMediaToCssFile() {
591 _writeCustomMediaToCssFile = _asyncToGenerator(function* (to, customMedia) {
592 const cssContent = Object.keys(customMedia).reduce((cssLines, name) => {
593 cssLines.push(`@custom-media ${name} ${customMedia[name]};`);
594 return cssLines;
595 }, []).join('\n');
596 const css = `${cssContent}\n`;
597 yield writeFile(to, css);
598 });
599 return _writeCustomMediaToCssFile.apply(this, arguments);
600}
601
602function writeCustomMediaToJsonFile(_x3, _x4) {
603 return _writeCustomMediaToJsonFile.apply(this, arguments);
604}
605/* Write Custom Media from Common JS file
606/* ========================================================================== */
607
608
609function _writeCustomMediaToJsonFile() {
610 _writeCustomMediaToJsonFile = _asyncToGenerator(function* (to, customMedia) {
611 const jsonContent = JSON.stringify({
612 'custom-media': customMedia
613 }, null, ' ');
614 const json = `${jsonContent}\n`;
615 yield writeFile(to, json);
616 });
617 return _writeCustomMediaToJsonFile.apply(this, arguments);
618}
619
620function writeCustomMediaToCjsFile(_x5, _x6) {
621 return _writeCustomMediaToCjsFile.apply(this, arguments);
622}
623/* Write Custom Media from Module JS file
624/* ========================================================================== */
625
626
627function _writeCustomMediaToCjsFile() {
628 _writeCustomMediaToCjsFile = _asyncToGenerator(function* (to, customMedia) {
629 const jsContents = Object.keys(customMedia).reduce((jsLines, name) => {
630 jsLines.push(`\t\t'${escapeForJS(name)}': '${escapeForJS(customMedia[name])}'`);
631 return jsLines;
632 }, []).join(',\n');
633 const js = `module.exports = {\n\tcustomMedia: {\n${jsContents}\n\t}\n};\n`;
634 yield writeFile(to, js);
635 });
636 return _writeCustomMediaToCjsFile.apply(this, arguments);
637}
638
639function writeCustomMediaToMjsFile(_x7, _x8) {
640 return _writeCustomMediaToMjsFile.apply(this, arguments);
641}
642/* Write Custom Media to Exports
643/* ========================================================================== */
644
645
646function _writeCustomMediaToMjsFile() {
647 _writeCustomMediaToMjsFile = _asyncToGenerator(function* (to, customMedia) {
648 const mjsContents = Object.keys(customMedia).reduce((mjsLines, name) => {
649 mjsLines.push(`\t'${escapeForJS(name)}': '${escapeForJS(customMedia[name])}'`);
650 return mjsLines;
651 }, []).join(',\n');
652 const mjs = `export const customMedia = {\n${mjsContents}\n};\n`;
653 yield writeFile(to, mjs);
654 });
655 return _writeCustomMediaToMjsFile.apply(this, arguments);
656}
657
658function writeCustomMediaToExports(customMedia, destinations) {
659 return Promise.all(destinations.map(
660 /*#__PURE__*/
661 function () {
662 var _ref = _asyncToGenerator(function* (destination) {
663 if (destination instanceof Function) {
664 yield destination(defaultCustomMediaToJSON(customMedia));
665 } else {
666 // read the destination as an object
667 const opts = destination === Object(destination) ? destination : {
668 to: String(destination)
669 }; // transformer for custom media into a JSON-compatible object
670
671 const toJSON = opts.toJSON || defaultCustomMediaToJSON;
672
673 if ('customMedia' in opts) {
674 // write directly to an object as customMedia
675 opts.customMedia = toJSON(customMedia);
676 } else if ('custom-media' in opts) {
677 // write directly to an object as custom-media
678 opts['custom-media'] = toJSON(customMedia);
679 } else {
680 // destination pathname
681 const to = String(opts.to || ''); // type of file being written to
682
683 const type = (opts.type || path.extname(to).slice(1)).toLowerCase(); // transformed custom media
684
685 const customMediaJSON = toJSON(customMedia);
686
687 if (type === 'css') {
688 yield writeCustomMediaToCssFile(to, customMediaJSON);
689 }
690
691 if (type === 'js') {
692 yield writeCustomMediaToCjsFile(to, customMediaJSON);
693 }
694
695 if (type === 'json') {
696 yield writeCustomMediaToJsonFile(to, customMediaJSON);
697 }
698
699 if (type === 'mjs') {
700 yield writeCustomMediaToMjsFile(to, customMediaJSON);
701 }
702 }
703 }
704 });
705
706 return function (_x9) {
707 return _ref.apply(this, arguments);
708 };
709 }()));
710}
711/* Helper utilities
712/* ========================================================================== */
713
714const defaultCustomMediaToJSON = customMedia => {
715 return Object.keys(customMedia).reduce((customMediaJSON, key) => {
716 customMediaJSON[key] = String(customMedia[key]);
717 return customMediaJSON;
718 }, {});
719};
720
721const writeFile = (to, text) => new Promise((resolve, reject) => {
722 fs.writeFile(to, text, error => {
723 if (error) {
724 reject(error);
725 } else {
726 resolve();
727 }
728 });
729});
730
731const escapeForJS = string => string.replace(/\\([\s\S])|(')/g, '\\$1$2').replace(/\n/g, '\\n').replace(/\r/g, '\\r');
732
733var index = postcss.plugin('postcss-custom-media', opts => {
734 // whether to preserve custom media and at-rules using them
735 const preserve = 'preserve' in Object(opts) ? Boolean(opts.preserve) : false; // sources to import custom media from
736
737 const importFrom = [].concat(Object(opts).importFrom || []); // destinations to export custom media to
738
739 const exportTo = [].concat(Object(opts).exportTo || []); // promise any custom media are imported
740
741 const customMediaPromise = getCustomMediaFromSources(importFrom);
742 return (
743 /*#__PURE__*/
744 function () {
745 var _ref = _asyncToGenerator(function* (root) {
746 const customMedia = Object.assign((yield customMediaPromise), getCustomMediaFromRoot(root, {
747 preserve
748 }));
749 yield writeCustomMediaToExports(customMedia, exportTo);
750 transformAtrules(root, customMedia, {
751 preserve
752 });
753 });
754
755 return function (_x) {
756 return _ref.apply(this, arguments);
757 };
758 }()
759 );
760});
761
762export default index;
763//# sourceMappingURL=index.es.mjs.map
Note: See TracBrowser for help on using the repository browser.