source: trip-planner-front/node_modules/icss-utils/src/extractICSS.js@ e29cc2e

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

initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;
2const balancedQuotes = /^("[^"]*"|'[^']*'|[^"']+)$/;
3
4const getDeclsObject = (rule) => {
5 const object = {};
6
7 rule.walkDecls((decl) => {
8 const before = decl.raws.before ? decl.raws.before.trim() : "";
9
10 object[before + decl.prop] = decl.value;
11 });
12
13 return object;
14};
15/**
16 *
17 * @param {string} css
18 * @param {boolean} removeRules
19 * @param {'auto' | 'rule' | 'at-rule'} mode
20 */
21const extractICSS = (css, removeRules = true, mode = "auto") => {
22 const icssImports = {};
23 const icssExports = {};
24
25 function addImports(node, path) {
26 const unquoted = path.replace(/'|"/g, "");
27 icssImports[unquoted] = Object.assign(
28 icssImports[unquoted] || {},
29 getDeclsObject(node)
30 );
31
32 if (removeRules) {
33 node.remove();
34 }
35 }
36
37 function addExports(node) {
38 Object.assign(icssExports, getDeclsObject(node));
39 if (removeRules) {
40 node.remove();
41 }
42 }
43
44 css.each((node) => {
45 if (node.type === "rule" && mode !== "at-rule") {
46 if (node.selector.slice(0, 7) === ":import") {
47 const matches = importPattern.exec(node.selector);
48
49 if (matches) {
50 addImports(node, matches[1]);
51 }
52 }
53
54 if (node.selector === ":export") {
55 addExports(node);
56 }
57 }
58
59 if (node.type === "atrule" && mode !== "rule") {
60 if (node.name === "icss-import") {
61 const matches = balancedQuotes.exec(node.params);
62
63 if (matches) {
64 addImports(node, matches[1]);
65 }
66 }
67 if (node.name === "icss-export") {
68 addExports(node);
69 }
70 }
71 });
72
73 return { icssImports, icssExports };
74};
75
76module.exports = extractICSS;
Note: See TracBrowser for help on using the repository browser.