extractICSS.js
1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
const importPattern = /^:import\(("[^"]*"|'[^']*'|[^"']+)\)$/;
const getDeclsObject = rule => {
const object = {};
rule.walkDecls(decl => {
const before = decl.raws.before ? decl.raws.before.trim() : "";
object[before + decl.prop] = decl.value;
});
return object;
};
const extractICSS = (css, removeRules = true) => {
const icssImports = {};
const icssExports = {};
css.each(node => {
if (node.type === "rule") {
if (node.selector.slice(0, 7) === ":import") {
const matches = importPattern.exec(node.selector);
if (matches) {
const path = matches[1].replace(/'|"/g, "");
icssImports[path] = Object.assign(icssImports[path] || {}, getDeclsObject(node));
if (removeRules) {
node.remove();
}
}
}
if (node.selector === ":export") {
Object.assign(icssExports, getDeclsObject(node));
if (removeRules) {
node.remove();
}
}
}
});
return {
icssImports,
icssExports
};
};
var _default = extractICSS;
exports.default = _default;