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