index.es.mjs 22 KB

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