@mapsight/vector-style-compiler 8.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +366 -0
- package/bin/vector-style-compiler.js +2 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +83 -0
- package/dist/cli.js.map +1 -0
- package/dist/cssToRules/mapDeclaration.d.ts +16 -0
- package/dist/cssToRules/mapDeclaration.d.ts.map +1 -0
- package/dist/cssToRules/mapDeclaration.js +24 -0
- package/dist/cssToRules/mapDeclaration.js.map +1 -0
- package/dist/cssToRules/mapRule.d.ts +41 -0
- package/dist/cssToRules/mapRule.d.ts.map +1 -0
- package/dist/cssToRules/mapRule.js +60 -0
- package/dist/cssToRules/mapRule.js.map +1 -0
- package/dist/cssToRules/mapSelector.d.ts +27 -0
- package/dist/cssToRules/mapSelector.d.ts.map +1 -0
- package/dist/cssToRules/mapSelector.js +61 -0
- package/dist/cssToRules/mapSelector.js.map +1 -0
- package/dist/cssToRules/mapSelectorPart.d.ts +48 -0
- package/dist/cssToRules/mapSelectorPart.d.ts.map +1 -0
- package/dist/cssToRules/mapSelectorPart.js +106 -0
- package/dist/cssToRules/mapSelectorPart.js.map +1 -0
- package/dist/cssToRules/mapValue.d.ts +20 -0
- package/dist/cssToRules/mapValue.d.ts.map +1 -0
- package/dist/cssToRules/mapValue.js +67 -0
- package/dist/cssToRules/mapValue.js.map +1 -0
- package/dist/cssToRules.d.ts +50 -0
- package/dist/cssToRules.d.ts.map +1 -0
- package/dist/cssToRules.js +32 -0
- package/dist/cssToRules.js.map +1 -0
- package/dist/helpers/Replacer.d.ts +11 -0
- package/dist/helpers/Replacer.d.ts.map +1 -0
- package/dist/helpers/Replacer.js +28 -0
- package/dist/helpers/Replacer.js.map +1 -0
- package/dist/helpers/compileDeclarationBlock.d.ts +14 -0
- package/dist/helpers/compileDeclarationBlock.d.ts.map +1 -0
- package/dist/helpers/compileDeclarationBlock.js +38 -0
- package/dist/helpers/compileDeclarationBlock.js.map +1 -0
- package/dist/helpers/createModulesMapFromDependencies.d.ts +5 -0
- package/dist/helpers/createModulesMapFromDependencies.d.ts.map +1 -0
- package/dist/helpers/createModulesMapFromDependencies.js +31 -0
- package/dist/helpers/createModulesMapFromDependencies.js.map +1 -0
- package/dist/helpers/ensureObject.d.ts +3 -0
- package/dist/helpers/ensureObject.d.ts.map +1 -0
- package/dist/helpers/ensureObject.js +12 -0
- package/dist/helpers/ensureObject.js.map +1 -0
- package/dist/helpers/pathToExpression.d.ts +2 -0
- package/dist/helpers/pathToExpression.d.ts.map +1 -0
- package/dist/helpers/pathToExpression.js +6 -0
- package/dist/helpers/pathToExpression.js.map +1 -0
- package/dist/helpers/uniqueSerialized.d.ts +2 -0
- package/dist/helpers/uniqueSerialized.d.ts.map +1 -0
- package/dist/helpers/uniqueSerialized.js +5 -0
- package/dist/helpers/uniqueSerialized.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/rulesToTree.d.ts +20 -0
- package/dist/rulesToTree.d.ts.map +1 -0
- package/dist/rulesToTree.js +43 -0
- package/dist/rulesToTree.js.map +1 -0
- package/dist/template.d.ts +16 -0
- package/dist/template.d.ts.map +1 -0
- package/dist/template.js +22 -0
- package/dist/template.js.map +1 -0
- package/dist/template.source.d.ts +3 -0
- package/dist/template.source.d.ts.map +1 -0
- package/dist/template.source.js +50 -0
- package/dist/template.source.js.map +1 -0
- package/dist/treeToProgram.d.ts +3 -0
- package/dist/treeToProgram.d.ts.map +1 -0
- package/dist/treeToProgram.js +158 -0
- package/dist/treeToProgram.js.map +1 -0
- package/package.json +67 -0
- package/src/data/custom-css-properties.json +200 -0
- package/src/js/cli.ts +98 -0
- package/src/js/cssToRules/mapDeclaration.ts +45 -0
- package/src/js/cssToRules/mapRule.ts +98 -0
- package/src/js/cssToRules/mapSelector.ts +78 -0
- package/src/js/cssToRules/mapSelectorPart.ts +161 -0
- package/src/js/cssToRules/mapValue.ts +84 -0
- package/src/js/cssToRules.ts +49 -0
- package/src/js/helpers/Replacer.ts +50 -0
- package/src/js/helpers/compileDeclarationBlock.ts +60 -0
- package/src/js/helpers/createModulesMapFromDependencies.ts +40 -0
- package/src/js/helpers/ensureObject.ts +17 -0
- package/src/js/helpers/pathToExpression.ts +5 -0
- package/src/js/helpers/uniqueSerialized.ts +7 -0
- package/src/js/index.ts +28 -0
- package/src/js/rulesToTree.ts +83 -0
- package/src/js/template.source.js +56 -0
- package/src/js/template.ts +50 -0
- package/src/js/treeToProgram.ts +220 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DeclarationLeaf,
|
|
3
|
+
DeclarationNode,
|
|
4
|
+
} from "./cssToRules/mapDeclaration.ts";
|
|
5
|
+
import type {Check} from "./cssToRules/mapSelectorPart.ts";
|
|
6
|
+
import {
|
|
7
|
+
type BlockTree,
|
|
8
|
+
compileDeclarationBlock,
|
|
9
|
+
} from "./helpers/compileDeclarationBlock.ts";
|
|
10
|
+
import pathToExpression from "./helpers/pathToExpression.ts";
|
|
11
|
+
import type {Child, Tree, TreeLeaf} from "./rulesToTree.ts";
|
|
12
|
+
|
|
13
|
+
const dict = "abcdefghijklmnopqrstuvwxyz";
|
|
14
|
+
function aliasName(n: number): string {
|
|
15
|
+
if (n < 1) throw new Error("n must be greater than 0");
|
|
16
|
+
|
|
17
|
+
let result = "";
|
|
18
|
+
let i = n;
|
|
19
|
+
|
|
20
|
+
while (i > 0) {
|
|
21
|
+
const j = (i - 1) % dict.length;
|
|
22
|
+
|
|
23
|
+
// Prepend the character to fix the read-order direction
|
|
24
|
+
result = dict[j] + result;
|
|
25
|
+
|
|
26
|
+
// Divide instead of subtract to shift to the next base digit
|
|
27
|
+
i = Math.floor((i - 1) / dict.length);
|
|
28
|
+
}
|
|
29
|
+
return `$${result}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function indent(depth: number = 0): [string, string, string] {
|
|
33
|
+
return [
|
|
34
|
+
"\n" + "\t".repeat(depth),
|
|
35
|
+
"\n" + "\t".repeat(depth + 1),
|
|
36
|
+
"\n" + "\t".repeat(depth + 2),
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function encodeDeclarationNode(
|
|
41
|
+
node: DeclarationNode | DeclarationLeaf | null | undefined,
|
|
42
|
+
): BlockTree | null {
|
|
43
|
+
if (!node) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return Object.fromEntries(
|
|
48
|
+
Object.entries(node).map(([key, node]) => {
|
|
49
|
+
if (node === null) {
|
|
50
|
+
return ["value", null];
|
|
51
|
+
}
|
|
52
|
+
return [
|
|
53
|
+
key,
|
|
54
|
+
typeof node === "object"
|
|
55
|
+
? encodeDeclarationNode(
|
|
56
|
+
node as DeclarationNode | DeclarationLeaf,
|
|
57
|
+
)
|
|
58
|
+
: node,
|
|
59
|
+
];
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function checkToExpression(check: Check, aliases?: Map<string, string>) {
|
|
65
|
+
switch (check.type) {
|
|
66
|
+
case "js":
|
|
67
|
+
return check.expression;
|
|
68
|
+
case "geometryType":
|
|
69
|
+
return `geometryType == ${check.value}`;
|
|
70
|
+
case "value": {
|
|
71
|
+
let property = pathToExpression(check.target, check.path);
|
|
72
|
+
if (aliases) {
|
|
73
|
+
if (aliases.has(property)) {
|
|
74
|
+
property = aliases.get(property)!;
|
|
75
|
+
} else {
|
|
76
|
+
const alias = aliasName(aliases.size + 1);
|
|
77
|
+
aliases.set(property, alias);
|
|
78
|
+
property = alias;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return "value" in check
|
|
83
|
+
? `${property} == ${check.value}`
|
|
84
|
+
: property;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function checkToExpressionWithAlias(
|
|
90
|
+
check: Check,
|
|
91
|
+
aliases: Map<string, string>,
|
|
92
|
+
): string {
|
|
93
|
+
const expression = checkToExpression(check, aliases);
|
|
94
|
+
if (aliases.has(expression)) {
|
|
95
|
+
return aliases.get(expression)!;
|
|
96
|
+
}
|
|
97
|
+
const alias = aliasName(aliases.size + 1);
|
|
98
|
+
aliases.set(expression, alias);
|
|
99
|
+
return alias;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function buildConditionExpression(child: Child, aliases: Map<string, string>) {
|
|
103
|
+
const expressions = child.conditions.map((checks) => {
|
|
104
|
+
const checkExpression = checks.map((check) => {
|
|
105
|
+
const expression = checkToExpressionWithAlias(check, aliases);
|
|
106
|
+
return check.negate ? `!${expression}` : expression;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return checkExpression.length > 1
|
|
110
|
+
? `(${checkExpression.join(" && ")})`
|
|
111
|
+
: checkExpression[0];
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
return expressions.length > 1
|
|
115
|
+
? `(${expressions.join(" || ")})`
|
|
116
|
+
: expressions[0];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export default function treeToProgram(
|
|
120
|
+
tree: Tree,
|
|
121
|
+
target = "declaration",
|
|
122
|
+
baseIndent = 0,
|
|
123
|
+
) {
|
|
124
|
+
let declarationCounter = 0;
|
|
125
|
+
|
|
126
|
+
function programDeclarationBody(
|
|
127
|
+
subTree: Child | TreeLeaf,
|
|
128
|
+
depth: number,
|
|
129
|
+
aliases: Map<string, string>,
|
|
130
|
+
) {
|
|
131
|
+
const [_] = indent(depth);
|
|
132
|
+
|
|
133
|
+
let result = "";
|
|
134
|
+
declarationCounter++;
|
|
135
|
+
|
|
136
|
+
if (target === "hash") {
|
|
137
|
+
result += `${_}h(${declarationCounter});`;
|
|
138
|
+
|
|
139
|
+
if (subTree.stylePropExpressions) {
|
|
140
|
+
subTree.stylePropExpressions.forEach((stylePropExpression) => {
|
|
141
|
+
let alias: string;
|
|
142
|
+
if (aliases.has(stylePropExpression)) {
|
|
143
|
+
alias = aliases.get(stylePropExpression)!;
|
|
144
|
+
} else {
|
|
145
|
+
alias = aliasName(aliases.size + 1);
|
|
146
|
+
aliases.set(stylePropExpression, alias);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
result += `${_}h('@' + createHash(${alias}));`;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (target === "declaration") {
|
|
155
|
+
const block = encodeDeclarationNode(subTree.declarations);
|
|
156
|
+
if (block) {
|
|
157
|
+
result += compileDeclarationBlock(block, "d", _);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function stateToProgram(subTree: Tree[string][string], depth = 0) {
|
|
165
|
+
const [_] = indent(depth);
|
|
166
|
+
const aliases = new Map<string, string>();
|
|
167
|
+
let program = "";
|
|
168
|
+
if (subTree.declarations) {
|
|
169
|
+
program += programDeclarationBody(subTree, depth, aliases);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (subTree.children) {
|
|
173
|
+
const programParts = subTree.children.map((child): string => {
|
|
174
|
+
const expression = buildConditionExpression(child, aliases);
|
|
175
|
+
return `if (${expression}) {${programDeclarationBody(child, depth + 2, aliases)}${_}}`;
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
if (programParts.length) {
|
|
179
|
+
program += `${_}${programParts.join(_)}`;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const aliasesPrograms = Array.from(aliases.entries()).map(
|
|
184
|
+
([expression, alias]) =>
|
|
185
|
+
`const ${alias} = /* @__PURE__ */${expression};`,
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
if (aliasesPrograms.length) {
|
|
189
|
+
program = `${_}{${_}${aliasesPrograms.join(_)}${_}${program}${_}}`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return program;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function styleToProgram(subTree: Tree[string], depth = 0) {
|
|
196
|
+
const cases = Object.keys(subTree).filter((a) => a !== "default");
|
|
197
|
+
const [_, __, ___] = indent(depth);
|
|
198
|
+
|
|
199
|
+
return (
|
|
200
|
+
(subTree.default ? stateToProgram(subTree.default, depth) : "") +
|
|
201
|
+
(cases.length
|
|
202
|
+
? `${_}switch (state) {${cases.map((a) => `${__}case '${a}':${stateToProgram(subTree[a]!, depth + 2)}${___}break;`).join("")}${_}}`
|
|
203
|
+
: "")
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function stylesToProgram(rootTree: Tree, depth = 0) {
|
|
208
|
+
const cases = Object.keys(rootTree).filter((a) => a !== "default");
|
|
209
|
+
const [_, __, ___] = indent(depth);
|
|
210
|
+
|
|
211
|
+
return (
|
|
212
|
+
(rootTree.default ? styleToProgram(rootTree.default, depth) : "") +
|
|
213
|
+
(cases.length
|
|
214
|
+
? `${_}switch (style) {${cases.map((a) => `${__}case '${a}':${styleToProgram(rootTree[a]!, depth + 2)}${___}break;`).join("")}${_}}`
|
|
215
|
+
: "")
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return stylesToProgram(tree, baseIndent);
|
|
220
|
+
}
|