@mapsight/vector-style-compiler 8.0.3 → 10.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.
Files changed (66) hide show
  1. package/README.md +33 -3
  2. package/dist/cssToRules/mapDeclaration.d.ts +1 -1
  3. package/dist/cssToRules/mapDeclaration.d.ts.map +1 -1
  4. package/dist/cssToRules/mapDeclaration.js +2 -2
  5. package/dist/cssToRules/mapDeclaration.js.map +1 -1
  6. package/dist/cssToRules/mapRule.d.ts +3 -2
  7. package/dist/cssToRules/mapRule.d.ts.map +1 -1
  8. package/dist/cssToRules/mapRule.js +5 -3
  9. package/dist/cssToRules/mapRule.js.map +1 -1
  10. package/dist/cssToRules/mapValue.d.ts +3 -0
  11. package/dist/cssToRules/mapValue.d.ts.map +1 -1
  12. package/dist/cssToRules/mapValue.js +15 -9
  13. package/dist/cssToRules/mapValue.js.map +1 -1
  14. package/dist/cssToRules.d.ts +2 -0
  15. package/dist/cssToRules.d.ts.map +1 -1
  16. package/dist/cssToRules.js +4 -3
  17. package/dist/cssToRules.js.map +1 -1
  18. package/dist/helpers/Replacer.d.ts.map +1 -1
  19. package/dist/helpers/Replacer.js +53 -9
  20. package/dist/helpers/Replacer.js.map +1 -1
  21. package/dist/helpers/createHelperImportsFromProgram.d.ts +2 -0
  22. package/dist/helpers/createHelperImportsFromProgram.d.ts.map +1 -0
  23. package/dist/helpers/createHelperImportsFromProgram.js +9 -0
  24. package/dist/helpers/createHelperImportsFromProgram.js.map +1 -0
  25. package/dist/helpers/volatileCalcHelpers.d.ts +3 -0
  26. package/dist/helpers/volatileCalcHelpers.d.ts.map +1 -0
  27. package/dist/helpers/volatileCalcHelpers.js +5 -0
  28. package/dist/helpers/volatileCalcHelpers.js.map +1 -0
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +3 -0
  31. package/dist/index.js.map +1 -1
  32. package/dist/rulesToTree.d.ts +2 -0
  33. package/dist/rulesToTree.d.ts.map +1 -1
  34. package/dist/rulesToTree.js +3 -0
  35. package/dist/rulesToTree.js.map +1 -1
  36. package/dist/template.d.ts +2 -1
  37. package/dist/template.d.ts.map +1 -1
  38. package/dist/template.js +11 -2
  39. package/dist/template.js.map +1 -1
  40. package/dist/template.source.js +61 -41
  41. package/dist/treeToProgram.d.ts.map +1 -1
  42. package/dist/treeToProgram.js +12 -4
  43. package/dist/treeToProgram.js.map +1 -1
  44. package/package.json +16 -13
  45. package/dist/template.source.d.ts +0 -3
  46. package/dist/template.source.d.ts.map +0 -1
  47. package/dist/template.source.js.map +0 -1
  48. package/src/data/custom-css-properties.json +0 -200
  49. package/src/js/cli.ts +0 -98
  50. package/src/js/cssToRules/mapDeclaration.ts +0 -45
  51. package/src/js/cssToRules/mapRule.ts +0 -98
  52. package/src/js/cssToRules/mapSelector.ts +0 -78
  53. package/src/js/cssToRules/mapSelectorPart.ts +0 -161
  54. package/src/js/cssToRules/mapValue.ts +0 -84
  55. package/src/js/cssToRules.ts +0 -49
  56. package/src/js/helpers/Replacer.ts +0 -50
  57. package/src/js/helpers/compileDeclarationBlock.ts +0 -60
  58. package/src/js/helpers/createModulesMapFromDependencies.ts +0 -40
  59. package/src/js/helpers/ensureObject.ts +0 -17
  60. package/src/js/helpers/pathToExpression.ts +0 -5
  61. package/src/js/helpers/uniqueSerialized.ts +0 -7
  62. package/src/js/index.ts +0 -28
  63. package/src/js/rulesToTree.ts +0 -83
  64. package/src/js/template.source.js +0 -56
  65. package/src/js/template.ts +0 -50
  66. package/src/js/treeToProgram.ts +0 -220
@@ -1,83 +0,0 @@
1
- import deepExtend from "@mapsight/lib-js/object/deep-extend";
2
-
3
- import type {Rules} from "./cssToRules.ts";
4
- import type {DeclarationNode} from "./cssToRules/mapDeclaration.ts";
5
- import type {Check} from "./cssToRules/mapSelectorPart.ts";
6
-
7
- export type TreeLeaf = {
8
- declarations?: DeclarationNode;
9
- children: Child[];
10
- stylePropExpressions?: Array<string>;
11
- };
12
-
13
- export type Child = {
14
- conditions: Array<Array<Check>>;
15
- declarations?: DeclarationNode;
16
- stylePropExpressions?: Array<string>;
17
- };
18
-
19
- export type Tree = {
20
- [style: string]: {
21
- [state: string]: TreeLeaf;
22
- };
23
- };
24
-
25
- export default function rulesToTree(rules: Rules["rules"]) {
26
- const tree: Tree = {};
27
-
28
- rules.forEach((rule) => {
29
- const ruleTree: Record<
30
- string,
31
- Record<string, Array<Array<Check>>>
32
- > = {};
33
-
34
- rule.conditions.forEach((condition) => {
35
- const style = condition.style || "default";
36
- const state = condition.state || "default";
37
-
38
- ruleTree[style] ??= {};
39
- ruleTree[style][state] ??= [];
40
-
41
- if (condition.checks) {
42
- ruleTree[style][state].push(condition.checks);
43
- }
44
- });
45
-
46
- Object.keys(ruleTree).forEach((style) => {
47
- const sub = ruleTree[style];
48
- if (!sub) {
49
- return;
50
- }
51
-
52
- const styleTree = tree[style] || {};
53
- Object.keys(sub).forEach((state) => {
54
- const stateTree: Tree[string][string] = styleTree[state] ?? {
55
- children: [],
56
- };
57
-
58
- const conditions = sub[state];
59
- if (conditions?.length) {
60
- stateTree.children.push({
61
- conditions: conditions,
62
- declarations: rule.declarations,
63
- stylePropExpressions: rule.__meta.stylePropExpressions,
64
- });
65
- return;
66
- }
67
- stateTree.stylePropExpressions = (
68
- stateTree.stylePropExpressions || []
69
- ).concat(rule.__meta.stylePropExpressions);
70
-
71
- stateTree.declarations = deepExtend(
72
- stateTree.declarations || {},
73
- rule.declarations,
74
- );
75
-
76
- styleTree[state] = stateTree;
77
- });
78
- tree[style] = styleTree;
79
- });
80
- });
81
-
82
- return tree;
83
- }
@@ -1,56 +0,0 @@
1
- import get from "@mapsight/lib-js/object/getPath";
2
- import replace from "@mapsight/lib-js/string/replaceRegex";
3
- import createCachedStyleFunction from "@mapsight/lib-ol/style/createCachedStyleFunction";
4
- /* styleImports-start: */
5
- import __vectorStyle_circle from "ol/style/Circle";
6
-
7
- /* :styleImports-end */
8
-
9
- /** @type {(value: unknown) => string} */
10
- const createHash = /* @__PURE__ */ (value) =>
11
- /* @__PURE__ */ JSON.stringify(value);
12
- /** @type {import('@mapsight/lib-ol/style/createCachedStyleFunction').StyleFunctionOptions} */
13
- const styleOptions = {
14
- constructorsMap: /* constructorMap-start: */ {
15
- circle: __vectorStyle_circle,
16
- } /* :constructorMap-end */,
17
- allowedStyles: /* styleNames-start: */ [] /* :styleNames-end */,
18
- allowedProps: /* styleProps-start: */ ["test"] /* :styleProps-end */,
19
- declarationHashFunction: (env, props, hashPrefix, geometryType, style) => {
20
- /** @type {Array<string|number>} */
21
- const hash = [hashPrefix, geometryType];
22
- // noinspection UnnecessaryLocalVariableJS
23
- /** @type {(a: string|number) => void} */
24
- const h = (a) => {
25
- hash.push(a);
26
- };
27
-
28
- /* program1-start: */
29
- h(1);
30
- h("@" + createHash(style));
31
- h("@" + createHash(props["test"]));
32
- h("@" + createHash(env["test"]));
33
- h("@" + createHash(get(props, ["path", "to", "test"])));
34
- /* :program1-end */
35
-
36
- return hash.join("|");
37
- },
38
- declarationFunction: (env, props, geometryType, style) => {
39
- /** @type {Record<string, unknown>} */
40
- const d = {};
41
-
42
- /* program2-start: */
43
- d.zIndex = 1;
44
- d.calcTest = {value: "" + replace("a", "e", "hallo") + ""};
45
- d.a = env.a;
46
- d.b = props.b;
47
- d.geometryType = geometryType;
48
- d.style = style;
49
- d.test = "foo";
50
- /* :program2-end */
51
-
52
- return d;
53
- },
54
- };
55
-
56
- export default createCachedStyleFunction(styleOptions);
@@ -1,50 +0,0 @@
1
- import {readFileSync} from "node:fs";
2
- import {resolve} from "node:path";
3
-
4
- import createModulesMapFromDependencies from "./helpers/createModulesMapFromDependencies.ts";
5
-
6
- export type TemplateArgs<
7
- TAdditionalData extends Record<string, unknown> = Record<string, unknown>,
8
- > = TAdditionalData & {
9
- __meta: {
10
- styleNames: string[];
11
- stateNames: string[];
12
- groupNames: string[];
13
- declarationNames: string[];
14
- styleProps: string[];
15
- stylePropExpressions: string[];
16
- };
17
- program1: string;
18
- program2: string;
19
- };
20
- export type Template<
21
- TAdditionalData extends Record<string, unknown> = Record<string, unknown>,
22
- > = (args: TemplateArgs<TAdditionalData>) => string;
23
-
24
- const templateSource = readFileSync(
25
- resolve(import.meta.dirname, "template.source.js"),
26
- "utf8",
27
- );
28
-
29
- const replaceTag = (content: string, tag: string, value: string) => {
30
- const pattern = `\\/\\*\\s*${tag}-start:\\s*\\*\\/[\\s\\S]*?\\/\\*\\s*:${tag}-end\\s*\\*\\/`;
31
- return content.replace(new RegExp(pattern, "g"), () => value);
32
- };
33
-
34
- const defaultTemplate = ({__meta, program1, program2}: TemplateArgs) => {
35
- const {declarationNames, styleProps, styleNames} = __meta;
36
- const {map: constructorMap, imports: styleImports} =
37
- createModulesMapFromDependencies(declarationNames);
38
-
39
- let result = "// @ts-nocheck\n" + templateSource;
40
- result = replaceTag(result, "styleImports", styleImports);
41
- result = replaceTag(result, "constructorMap", constructorMap);
42
- result = replaceTag(result, "styleNames", JSON.stringify(styleNames));
43
- result = replaceTag(result, "styleProps", JSON.stringify(styleProps));
44
- result = replaceTag(result, "program1", program1);
45
- result = replaceTag(result, "program2", program2);
46
-
47
- return result;
48
- };
49
-
50
- export default defaultTemplate;
@@ -1,220 +0,0 @@
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
- }