index.cjs.js 22 KB

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