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