@navita/engine 0.2.2 → 3.0.0-next.1

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 (67) hide show
  1. package/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/cache.cjs +26 -26
  3. package/cache.mjs +28 -24
  4. package/helpers/declarationsToBlock.cjs +10 -14
  5. package/helpers/declarationsToBlock.mjs +12 -12
  6. package/helpers/generateCombinedAtRules.cjs +5 -7
  7. package/helpers/generateCombinedAtRules.mjs +7 -5
  8. package/helpers/getPropertyPriority.cjs +221 -260
  9. package/helpers/getPropertyPriority.mjs +223 -258
  10. package/helpers/hyphenateProperty.cjs +8 -9
  11. package/helpers/hyphenateProperty.mjs +10 -7
  12. package/helpers/isContainerQuery.cjs +4 -4
  13. package/helpers/isContainerQuery.mjs +6 -2
  14. package/helpers/isMediaQuery.cjs +4 -4
  15. package/helpers/isMediaQuery.mjs +6 -2
  16. package/helpers/isNestedSelector.cjs +5 -5
  17. package/helpers/isNestedSelector.mjs +7 -3
  18. package/helpers/isObject.cjs +4 -4
  19. package/helpers/isObject.mjs +6 -2
  20. package/helpers/isSupportsQuery.cjs +4 -4
  21. package/helpers/isSupportsQuery.mjs +6 -2
  22. package/helpers/normalizeCSSVarsProperty.cjs +7 -11
  23. package/helpers/normalizeCSSVarsProperty.mjs +9 -9
  24. package/helpers/normalizeCSSVarsValue.cjs +6 -8
  25. package/helpers/normalizeCSSVarsValue.mjs +8 -6
  26. package/helpers/normalizeNestedProperty.cjs +5 -7
  27. package/helpers/normalizeNestedProperty.mjs +7 -5
  28. package/helpers/pixelifyProperties.cjs +50 -53
  29. package/helpers/pixelifyProperties.mjs +52 -51
  30. package/helpers/splitSelectorList.cjs +55 -0
  31. package/helpers/splitSelectorList.mjs +57 -0
  32. package/helpers/splitStyleBlocks.cjs +23 -25
  33. package/helpers/splitStyleBlocks.mjs +25 -23
  34. package/helpers/transformContentProperty.cjs +4 -5
  35. package/helpers/transformContentProperty.mjs +6 -3
  36. package/identifiers/IDGenerator.cjs +9 -9
  37. package/identifiers/IDGenerator.mjs +11 -7
  38. package/identifiers/alphaIDGenerator.cjs +23 -26
  39. package/identifiers/alphaIDGenerator.mjs +25 -24
  40. package/identifiers/propertyValueIDGenerator.cjs +18 -23
  41. package/identifiers/propertyValueIDGenerator.mjs +20 -21
  42. package/index.cjs +187 -238
  43. package/index.d.ts +91 -84
  44. package/index.mjs +184 -234
  45. package/package.json +1 -1
  46. package/printers/printFontFaces.cjs +7 -12
  47. package/printers/printFontFaces.mjs +9 -10
  48. package/printers/printKeyFrames.cjs +10 -16
  49. package/printers/printKeyFrames.mjs +12 -14
  50. package/printers/printSourceMap.cjs +34 -39
  51. package/printers/printSourceMap.mjs +36 -37
  52. package/printers/printStyleBlocks.cjs +71 -70
  53. package/printers/printStyleBlocks.mjs +73 -68
  54. package/printers/sortAtRules.cjs +8 -7
  55. package/printers/sortAtRules.mjs +7 -4
  56. package/processKeyframes.cjs +16 -22
  57. package/processKeyframes.mjs +19 -20
  58. package/processStyles.cjs +99 -105
  59. package/processStyles.mjs +101 -103
  60. package/types.cjs +0 -2
  61. package/types.mjs +4 -1
  62. package/wrappers/classList.cjs +4 -5
  63. package/wrappers/classList.mjs +6 -3
  64. package/wrappers/static.cjs +4 -5
  65. package/wrappers/static.mjs +6 -3
  66. package/printers/sortStatic.cjs +0 -7
  67. package/printers/sortStatic.mjs +0 -5
package/index.d.ts CHANGED
@@ -1,98 +1,105 @@
1
- import { CSSKeyframes, FontFaceRule, StyleRule } from '@navita/types';
1
+ import { CSSKeyframes, FontFaceRule, StyleRule } from "@navita/types";
2
2
 
3
+ //#region src/types.d.ts
3
4
  interface StyleBlock {
4
- type: 'rule' | 'static';
5
- selector: string;
6
- property: string;
7
- value: string;
8
- pseudo: string;
9
- media: string;
10
- support: string;
11
- container: string;
12
- id: string | number;
5
+ type: "rule" | "static";
6
+ selector: string;
7
+ property: string;
8
+ value: string;
9
+ pseudo: string;
10
+ media: string;
11
+ support: string;
12
+ container: string;
13
+ id: string | number;
13
14
  }
14
15
  interface KeyframesBlock {
15
- type: 'keyframes';
16
- rule: CSSKeyframes;
17
- id: string;
16
+ type: "keyframes";
17
+ rule: CSSKeyframes;
18
+ id: string;
18
19
  }
19
20
  interface FontFaceBlock {
20
- type: 'fontFace';
21
- rule: FontFaceRule[];
22
- id: string;
21
+ type: "fontFace";
22
+ rule: FontFaceRule[];
23
+ id: string;
23
24
  }
24
-
25
- declare class ClassList extends String {
26
- }
27
-
28
- declare class Static {
29
- }
30
-
25
+ //#endregion
26
+ //#region src/wrappers/classList.d.ts
27
+ declare class ClassList extends String {}
28
+ //#endregion
29
+ //#region src/wrappers/static.d.ts
30
+ declare class Static {}
31
+ //#endregion
32
+ //#region src/index.d.ts
31
33
  type CacheKeys = keyof Engine["caches"];
32
- type UsedIdCache = {
33
- [key in CacheKeys]?: (string | number)[];
34
- };
34
+ type UsedIdCache = { [key in CacheKeys]?: (string | number)[] };
35
35
  interface Identifier {
36
- value: string;
37
- id: string;
36
+ value: string;
37
+ id: string;
38
38
  }
39
39
  type Options = {
40
- context?: string;
41
- enableSourceMaps?: boolean;
42
- enableDebugIdentifiers?: boolean;
40
+ context?: string;
41
+ enableSourceMaps?: boolean;
42
+ enableDebugIdentifiers?: boolean;
43
43
  };
44
44
  declare class Engine {
45
- private readonly caches;
46
- private readonly usedIds;
47
- private filePath;
48
- private identifierCount;
49
- private readonly options;
50
- private sourceMapReferences;
51
- constructor(options?: Options);
52
- addStatic(selector: string, styles: StyleRule): Static;
53
- addStyle(styles: StyleRule): ClassList;
54
- addFontFace(fontFace: FontFaceRule | FontFaceRule[]): string;
55
- addKeyframes(keyframes: CSSKeyframes): string;
56
- addSourceMapReference({ filePath, identifier, classList, line, column, index }: {
57
- index: number;
58
- identifier: string;
59
- classList: ClassList;
60
- filePath: string;
61
- line: number;
62
- column: number;
63
- }): string | false;
64
- private clearSourceMapReferences;
65
- clearCache(filePath: string): void;
66
- generateIdentifier(value: unknown): string;
67
- renderCssToString(options?: {
68
- filePaths?: string[];
69
- usedIds?: UsedIdCache;
70
- opinionatedLayers?: boolean;
71
- }): string;
72
- serialize(): string;
73
- deserialize(buffer: Buffer | string): Promise<void>;
74
- setFilePath(filePath: string | undefined): void;
75
- getUsedFilePaths(): string[];
76
- getItems(caches: UsedIdCache): {
77
- rule?: (StyleBlock & {
78
- id: string | number;
79
- })[];
80
- static?: (StyleBlock & {
81
- id: string | number;
82
- })[];
83
- keyframes?: (KeyframesBlock & {
84
- id: string | number;
85
- })[];
86
- fontFace?: (FontFaceBlock & {
87
- id: string | number;
88
- })[];
89
- identifiers?: (Identifier & {
90
- id: string | number;
91
- })[];
92
- };
93
- clearUsedIds(filePath: string): void;
94
- private addUsedIds;
95
- getCacheIds(filePaths?: string[]): UsedIdCache;
45
+ private readonly caches;
46
+ private readonly usedIds;
47
+ private filePath;
48
+ private identifierCount;
49
+ private readonly options;
50
+ private sourceMapReferences;
51
+ constructor(options?: Options);
52
+ addStatic(selector: string, styles: StyleRule): Static;
53
+ addStyle(styles: StyleRule): ClassList;
54
+ addFontFace(fontFace: FontFaceRule | FontFaceRule[]): string;
55
+ addKeyframes(keyframes: CSSKeyframes): string;
56
+ addSourceMapReference({
57
+ filePath,
58
+ identifier,
59
+ classList,
60
+ line,
61
+ column,
62
+ index
63
+ }: {
64
+ index: number;
65
+ identifier: string;
66
+ classList: ClassList;
67
+ filePath: string;
68
+ line: number;
69
+ column: number;
70
+ }): string | false;
71
+ private clearSourceMapReferences;
72
+ clearCache(filePath: string): void;
73
+ generateIdentifier(value: unknown): string;
74
+ renderCssToString(options?: {
75
+ filePaths?: string[];
76
+ usedIds?: UsedIdCache;
77
+ opinionatedLayers?: boolean;
78
+ }): string;
79
+ serialize(): string;
80
+ deserialize(buffer: Buffer | string): Promise<void>;
81
+ setFilePath(filePath: string | undefined): void;
82
+ getUsedFilePaths(): string[];
83
+ getItems(caches: UsedIdCache): {
84
+ rule?: (StyleBlock & {
85
+ id: string | number;
86
+ })[];
87
+ static?: (StyleBlock & {
88
+ id: string | number;
89
+ })[];
90
+ keyframes?: (KeyframesBlock & {
91
+ id: string | number;
92
+ })[];
93
+ fontFace?: (FontFaceBlock & {
94
+ id: string | number;
95
+ })[];
96
+ identifiers?: (Identifier & {
97
+ id: string | number;
98
+ })[];
99
+ };
100
+ clearUsedIds(filePath: string): void;
101
+ private addUsedIds;
102
+ getCacheIds(filePaths?: string[]): UsedIdCache;
96
103
  }
97
-
98
- export { ClassList, Engine, Options, Static, UsedIdCache };
104
+ //#endregion
105
+ export { ClassList, Engine, Options, Static, UsedIdCache };
package/index.mjs CHANGED
@@ -1,237 +1,187 @@
1
- import path from 'node:path';
2
- import hash from '@emotion/hash';
3
- import { Cache } from './cache.mjs';
4
- import { isObject } from './helpers/isObject.mjs';
5
- import { splitStyleBlocks } from './helpers/splitStyleBlocks.mjs';
6
- import { IDGenerator } from './identifiers/IDGenerator.mjs';
7
- import { AlphaIDGenerator } from './identifiers/alphaIDGenerator.mjs';
8
- import { PropertyValueIDGenerator } from './identifiers/propertyValueIDGenerator.mjs';
9
- import { printFontFaces } from './printers/printFontFaces.mjs';
10
- import { printKeyFrames } from './printers/printKeyFrames.mjs';
11
- import { printSourceMap } from './printers/printSourceMap.mjs';
12
- import { printStyleBlocks } from './printers/printStyleBlocks.mjs';
13
- import { sortAtRules } from './printers/sortAtRules.mjs';
14
- import { processKeyframes } from './processKeyframes.mjs';
15
- import { processStyles } from './processStyles.mjs';
16
- import { ClassList } from './wrappers/classList.mjs';
17
- import { Static } from './wrappers/static.mjs';
18
-
1
+ import "node:path";
2
+ import "node:url";
3
+ import.meta.url;
4
+ import { Cache } from "./cache.mjs";
5
+ import { isObject } from "./helpers/isObject.mjs";
6
+ import { splitStyleBlocks } from "./helpers/splitStyleBlocks.mjs";
7
+ import { AlphaIDGenerator } from "./identifiers/alphaIDGenerator.mjs";
8
+ import { IDGenerator } from "./identifiers/IDGenerator.mjs";
9
+ import { PropertyValueIDGenerator } from "./identifiers/propertyValueIDGenerator.mjs";
10
+ import { printFontFaces } from "./printers/printFontFaces.mjs";
11
+ import { printKeyFrames } from "./printers/printKeyFrames.mjs";
12
+ import { printSourceMap } from "./printers/printSourceMap.mjs";
13
+ import { printStyleBlocks } from "./printers/printStyleBlocks.mjs";
14
+ import { sortAtRules } from "./printers/sortAtRules.mjs";
15
+ import { processKeyframes } from "./processKeyframes.mjs";
16
+ import { processStyles } from "./processStyles.mjs";
17
+ import { ClassList } from "./wrappers/classList.mjs";
18
+ import { Static } from "./wrappers/static.mjs";
19
+ import path from "node:path";
20
+ import hash from "@emotion/hash";
21
+ //#region src/index.ts
19
22
  const defaultOptions = {
20
- enableSourceMaps: false,
21
- enableDebugIdentifiers: false
23
+ enableSourceMaps: false,
24
+ enableDebugIdentifiers: false
22
25
  };
23
- class Engine {
24
- caches = {
25
- rule: new Cache(new PropertyValueIDGenerator()),
26
- static: new Cache(new IDGenerator()),
27
- keyframes: new Cache(new AlphaIDGenerator()),
28
- fontFace: new Cache(new AlphaIDGenerator()),
29
- identifiers: new Cache(new AlphaIDGenerator())
30
- };
31
- usedIds = {};
32
- identifierCount = 0;
33
- options = {};
34
- sourceMapReferences = {};
35
- constructor(options = {}){
36
- this.options = {
37
- ...defaultOptions,
38
- ...Object.keys(options).reduce((acc, key)=>{
39
- if (options[key] !== undefined) {
40
- acc[key] = options[key];
41
- }
42
- return acc;
43
- }, {})
44
- };
45
- }
46
- addStatic(selector, styles) {
47
- this.addUsedIds("static", processStyles({
48
- type: "static",
49
- cache: this.caches.static
50
- })({
51
- styles,
52
- selector
53
- }).map((style)=>style.id));
54
- return new Static();
55
- }
56
- addStyle(styles) {
57
- const rules = processStyles({
58
- type: "rule",
59
- cache: this.caches.rule
60
- })({
61
- styles
62
- });
63
- const ids = rules.map((rule)=>rule.id);
64
- this.addUsedIds("rule", ids);
65
- return new ClassList(ids.join(" "));
66
- }
67
- addFontFace(fontFace) {
68
- const { id } = this.caches.fontFace.getOrStore({
69
- type: "fontFace",
70
- rule: Array.isArray(fontFace) ? fontFace : [
71
- fontFace
72
- ]
73
- });
74
- this.addUsedIds("fontFace", [
75
- id
76
- ]);
77
- return id;
78
- }
79
- addKeyframes(keyframes) {
80
- const { id } = this.caches.keyframes.getOrStore({
81
- type: "keyframes",
82
- rule: processKeyframes(keyframes)
83
- });
84
- this.addUsedIds("keyframes", [
85
- id
86
- ]);
87
- return id;
88
- }
89
- addSourceMapReference({ filePath , identifier , classList , line , column , index }) {
90
- if (!this.options.enableSourceMaps) {
91
- return false;
92
- }
93
- const { name } = path.parse(filePath);
94
- const extraClass = this.options.enableDebugIdentifiers ? `${name}_${identifier}` : '';
95
- const selector = '.' + classList.toString().split(' ').concat(extraClass).filter(Boolean).join('.');
96
- const newFilePath = path.relative(this.options.context || process.cwd(), filePath);
97
- if (index === 0 || !this.sourceMapReferences[newFilePath]) {
98
- this.sourceMapReferences[newFilePath] = [];
99
- }
100
- this.sourceMapReferences[newFilePath][index] = {
101
- selector,
102
- line,
103
- column
104
- };
105
- return extraClass;
106
- }
107
- clearSourceMapReferences(filePath) {
108
- const newFilePath = path.relative(this.options.context || process.cwd(), filePath);
109
- this.sourceMapReferences[newFilePath] = [];
110
- }
111
- clearCache(filePath) {
112
- this.clearUsedIds(filePath);
113
- this.clearSourceMapReferences(filePath);
114
- }
115
- generateIdentifier(value) {
116
- if (typeof value === 'undefined') {
117
- let identifier = hash((this.identifierCount++).toString(36));
118
- if (identifier.match(/^\d/)) {
119
- identifier = `_${identifier}`;
120
- }
121
- return identifier;
122
- }
123
- const newValue = JSON.stringify(value);
124
- const { id } = this.caches.identifiers.getOrStore({
125
- value: newValue
126
- });
127
- this.addUsedIds("identifiers", [
128
- id
129
- ]);
130
- return `_${id}`;
131
- }
132
- renderCssToString(options) {
133
- const { filePaths , usedIds , opinionatedLayers =false } = options || {};
134
- // We prioritize ids over filePaths. If neither are provided, we use all the used filePaths.
135
- const { keyframes: keyframesCache , fontFace: fontFaceCache , static: staticCache , rule: ruleCache } = usedIds ?? this.getCacheIds(filePaths ?? Object.keys(this.usedIds));
136
- const { atRules , lowPrioRules , rules } = splitStyleBlocks(this.caches.rule.items(ruleCache));
137
- const keyFrameCss = printKeyFrames(this.caches.keyframes.items(keyframesCache));
138
- const fontFaceCss = printFontFaces(this.caches.fontFace.items(fontFaceCache));
139
- const staticCss = printStyleBlocks(this.caches.static.items(staticCache));
140
- const atRulesCss = printStyleBlocks(sortAtRules(atRules));
141
- const lowPrioRulesCss = printStyleBlocks(lowPrioRules);
142
- const rulesCss = printStyleBlocks(rules);
143
- if (opinionatedLayers) {
144
- const result = `${keyFrameCss}${fontFaceCss}` + (staticCss.length > 0 ? `@layer s{${staticCss}}` : '') + (lowPrioRulesCss.length > 0 ? `@layer lpr{${lowPrioRulesCss}}` : '') + (rulesCss.length > 0 ? `@layer r{${rulesCss}}` : '') + (atRulesCss.length > 0 ? `@layer at{${atRulesCss}}` : '');
145
- if (result.length > 0) {
146
- // s - static
147
- // lpr - low priority rules
148
- // r - rules
149
- // at - at rules
150
- return `@layer s,lpr,r,at;${result}`;
151
- }
152
- return '';
153
- }
154
- const content = keyFrameCss + fontFaceCss + staticCss + lowPrioRulesCss + rulesCss + atRulesCss;
155
- if (this.options.enableSourceMaps) {
156
- return printSourceMap(this.sourceMapReferences, content);
157
- }
158
- return content;
159
- }
160
- serialize() {
161
- const { caches , usedIds , identifierCount , sourceMapReferences: sourceMapReferencesData } = this;
162
- const sourceMapReferences = this.options.enableSourceMaps ? sourceMapReferencesData : undefined;
163
- return JSON.stringify({
164
- caches,
165
- usedIds,
166
- identifierCount,
167
- sourceMapReferences
168
- });
169
- }
170
- async deserialize(buffer) {
171
- const data = JSON.parse(buffer.toString());
172
- function assign(target, source) {
173
- for(const key in source){
174
- if (isObject(target[key]) && isObject(source[key])) {
175
- assign(target[key], source[key]);
176
- } else {
177
- target[key] = source[key];
178
- }
179
- }
180
- return target;
181
- }
182
- assign(this, data);
183
- }
184
- setFilePath(filePath) {
185
- this.filePath = filePath;
186
- }
187
- getUsedFilePaths() {
188
- return Object.keys(this.usedIds);
189
- }
190
- getItems(caches) {
191
- return Object.keys(caches).reduce((acc, key)=>({
192
- ...acc,
193
- [key]: this.caches[key].items(caches[key])
194
- }), {});
195
- }
196
- clearUsedIds(filePath) {
197
- if (filePath === undefined) {
198
- return;
199
- }
200
- this.usedIds[filePath] = {};
201
- }
202
- addUsedIds(cacheType, identifiers) {
203
- const { filePath } = this;
204
- if (filePath === undefined) {
205
- return;
206
- }
207
- if (this.usedIds[filePath] === undefined) {
208
- this.usedIds[filePath] = {};
209
- }
210
- if (this.usedIds[filePath][cacheType] === undefined) {
211
- this.usedIds[filePath][cacheType] = [];
212
- }
213
- this.usedIds[filePath][cacheType] = [
214
- ...new Set([
215
- ...this.usedIds[filePath][cacheType],
216
- ...identifiers
217
- ])
218
- ];
219
- }
220
- getCacheIds(filePaths = []) {
221
- return filePaths.reduce((acc, filePath)=>({
222
- ...acc,
223
- ...Object.keys(this.usedIds[filePath] || []).reduce((cache, key)=>({
224
- ...cache,
225
- [key]: [
226
- ...acc[key] || [],
227
- ...this.usedIds[filePath][key] || []
228
- ]
229
- }), {})
230
- }), Object.keys(this.caches).reduce((acc, key)=>({
231
- ...acc,
232
- [key]: []
233
- }), {}));
234
- }
235
- }
236
-
26
+ var Engine = class {
27
+ caches = {
28
+ rule: new Cache(new PropertyValueIDGenerator()),
29
+ static: new Cache(new IDGenerator()),
30
+ keyframes: new Cache(new AlphaIDGenerator()),
31
+ fontFace: new Cache(new AlphaIDGenerator()),
32
+ identifiers: new Cache(new AlphaIDGenerator())
33
+ };
34
+ usedIds = {};
35
+ filePath;
36
+ identifierCount = 0;
37
+ options = {};
38
+ sourceMapReferences = {};
39
+ constructor(options = {}) {
40
+ this.options = {
41
+ ...defaultOptions,
42
+ ...Object.keys(options).reduce((acc, key) => {
43
+ if (options[key] !== void 0) acc[key] = options[key];
44
+ return acc;
45
+ }, {})
46
+ };
47
+ }
48
+ addStatic(selector, styles) {
49
+ this.addUsedIds("static", processStyles({
50
+ type: "static",
51
+ cache: this.caches.static
52
+ })({
53
+ styles,
54
+ selector
55
+ }).map((style) => style.id));
56
+ return new Static();
57
+ }
58
+ addStyle(styles) {
59
+ const ids = processStyles({
60
+ type: "rule",
61
+ cache: this.caches.rule
62
+ })({ styles }).map((rule) => rule.id);
63
+ this.addUsedIds("rule", ids);
64
+ return new ClassList(ids.join(" "));
65
+ }
66
+ addFontFace(fontFace) {
67
+ const { id } = this.caches.fontFace.getOrStore({
68
+ type: "fontFace",
69
+ rule: Array.isArray(fontFace) ? fontFace : [fontFace]
70
+ });
71
+ this.addUsedIds("fontFace", [id]);
72
+ return id;
73
+ }
74
+ addKeyframes(keyframes) {
75
+ const { id } = this.caches.keyframes.getOrStore({
76
+ type: "keyframes",
77
+ rule: processKeyframes(keyframes)
78
+ });
79
+ this.addUsedIds("keyframes", [id]);
80
+ return id;
81
+ }
82
+ addSourceMapReference({ filePath, identifier, classList, line, column, index }) {
83
+ if (!this.options.enableSourceMaps) return false;
84
+ const { name } = path.parse(filePath);
85
+ const extraClass = this.options.enableDebugIdentifiers ? `${name}_${identifier}` : "";
86
+ const selector = "." + classList.toString().split(" ").concat(extraClass).filter(Boolean).join(".");
87
+ const newFilePath = path.relative(this.options.context || process.cwd(), filePath);
88
+ if (index === 0 || !this.sourceMapReferences[newFilePath]) this.sourceMapReferences[newFilePath] = [];
89
+ this.sourceMapReferences[newFilePath][index] = {
90
+ selector,
91
+ line,
92
+ column
93
+ };
94
+ return extraClass;
95
+ }
96
+ clearSourceMapReferences(filePath) {
97
+ const newFilePath = path.relative(this.options.context || process.cwd(), filePath);
98
+ this.sourceMapReferences[newFilePath] = [];
99
+ }
100
+ clearCache(filePath) {
101
+ this.clearUsedIds(filePath);
102
+ this.clearSourceMapReferences(filePath);
103
+ }
104
+ generateIdentifier(value) {
105
+ if (typeof value === "undefined") {
106
+ let identifier = hash((this.identifierCount++).toString(36));
107
+ if (identifier.match(/^\d/)) identifier = `_${identifier}`;
108
+ return identifier;
109
+ }
110
+ const newValue = JSON.stringify(value);
111
+ const { id } = this.caches.identifiers.getOrStore({ value: newValue });
112
+ this.addUsedIds("identifiers", [id]);
113
+ return `_${id}`;
114
+ }
115
+ renderCssToString(options) {
116
+ const { filePaths, usedIds, opinionatedLayers = false } = options || {};
117
+ const { keyframes: keyframesCache, fontFace: fontFaceCache, static: staticCache, rule: ruleCache } = usedIds ?? this.getCacheIds(filePaths ?? Object.keys(this.usedIds));
118
+ const { atRules, lowPrioRules, rules } = splitStyleBlocks(this.caches.rule.items(ruleCache));
119
+ const keyFrameCss = printKeyFrames(this.caches.keyframes.items(keyframesCache));
120
+ const fontFaceCss = printFontFaces(this.caches.fontFace.items(fontFaceCache));
121
+ const staticCss = printStyleBlocks(this.caches.static.items(staticCache));
122
+ const atRulesCss = printStyleBlocks(sortAtRules(atRules));
123
+ const lowPrioRulesCss = printStyleBlocks(lowPrioRules);
124
+ const rulesCss = printStyleBlocks(rules);
125
+ if (opinionatedLayers) {
126
+ const result = `${keyFrameCss}${fontFaceCss}` + (staticCss.length > 0 ? `@layer s{${staticCss}}` : "") + (lowPrioRulesCss.length > 0 ? `@layer lpr{${lowPrioRulesCss}}` : "") + (rulesCss.length > 0 ? `@layer r{${rulesCss}}` : "") + (atRulesCss.length > 0 ? `@layer at{${atRulesCss}}` : "");
127
+ if (result.length > 0) return `@layer s,lpr,r,at;${result}`;
128
+ return "";
129
+ }
130
+ const content = keyFrameCss + fontFaceCss + staticCss + lowPrioRulesCss + rulesCss + atRulesCss;
131
+ if (this.options.enableSourceMaps) return printSourceMap(this.sourceMapReferences, content);
132
+ return content;
133
+ }
134
+ serialize() {
135
+ const { caches, usedIds, identifierCount, sourceMapReferences: sourceMapReferencesData } = this;
136
+ const sourceMapReferences = this.options.enableSourceMaps ? sourceMapReferencesData : void 0;
137
+ return JSON.stringify({
138
+ caches,
139
+ usedIds,
140
+ identifierCount,
141
+ sourceMapReferences
142
+ });
143
+ }
144
+ async deserialize(buffer) {
145
+ const data = JSON.parse(buffer.toString());
146
+ function assign(target, source) {
147
+ for (const key in source) if (isObject(target[key]) && isObject(source[key])) assign(target[key], source[key]);
148
+ else target[key] = source[key];
149
+ return target;
150
+ }
151
+ assign(this, data);
152
+ }
153
+ setFilePath(filePath) {
154
+ this.filePath = filePath;
155
+ }
156
+ getUsedFilePaths() {
157
+ return Object.keys(this.usedIds);
158
+ }
159
+ getItems(caches) {
160
+ const result = {};
161
+ for (const key of Object.keys(caches)) result[key] = this.caches[key].items(caches[key]);
162
+ return result;
163
+ }
164
+ clearUsedIds(filePath) {
165
+ if (filePath === void 0) return;
166
+ this.usedIds[filePath] = {};
167
+ }
168
+ addUsedIds(cacheType, identifiers) {
169
+ const { filePath } = this;
170
+ if (filePath === void 0) return;
171
+ if (this.usedIds[filePath] === void 0) this.usedIds[filePath] = {};
172
+ if (this.usedIds[filePath][cacheType] === void 0) this.usedIds[filePath][cacheType] = [];
173
+ this.usedIds[filePath][cacheType] = [.../* @__PURE__ */ new Set([...this.usedIds[filePath][cacheType], ...identifiers])];
174
+ }
175
+ getCacheIds(filePaths = []) {
176
+ const result = {};
177
+ for (const key of Object.keys(this.caches)) result[key] = [];
178
+ for (const filePath of filePaths) {
179
+ const used = this.usedIds[filePath];
180
+ if (!used) continue;
181
+ for (const key of Object.keys(used)) result[key] = [...result[key] || [], ...used[key] || []];
182
+ }
183
+ return result;
184
+ }
185
+ };
186
+ //#endregion
237
187
  export { ClassList, Engine, Static };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navita/engine",
3
- "version": "0.2.2",
3
+ "version": "3.0.0-next.1",
4
4
  "description": "Navitas CSS-in-JS engine",
5
5
  "keywords": [
6
6
  "css-in-js",
@@ -1,15 +1,10 @@
1
- 'use strict';
2
-
3
- var declarationsToBlock = require('../helpers/declarationsToBlock.cjs');
4
-
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_helpers_declarationsToBlock = require("../helpers/declarationsToBlock.cjs");
3
+ //#region src/printers/printFontFaces.ts
5
4
  function printFontFaces(blocks) {
6
- let fontFaces = '';
7
- for (const block of blocks){
8
- for (const rule of block.rule){
9
- fontFaces += `@font-face{font-family:${block.id};${declarationsToBlock.declarationsToBlock(rule)}}`;
10
- }
11
- }
12
- return fontFaces;
5
+ let fontFaces = "";
6
+ for (const block of blocks) for (const rule of block.rule) fontFaces += `@font-face{font-family:${block.id};${require_helpers_declarationsToBlock.declarationsToBlock(rule)}}`;
7
+ return fontFaces;
13
8
  }
14
-
9
+ //#endregion
15
10
  exports.printFontFaces = printFontFaces;