zss-engine 0.2.38 → 0.2.40

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 (43) hide show
  1. package/dist/cjs/index.js +22 -0
  2. package/dist/cjs/types/common/css-properties.js +2 -0
  3. package/dist/cjs/types/common/css-property.js +2 -0
  4. package/dist/cjs/types/main/create.js +2 -0
  5. package/dist/cjs/types/main/global.js +2 -0
  6. package/dist/cjs/types/main/vars.js +2 -0
  7. package/dist/cjs/utils/build.js +27 -0
  8. package/dist/cjs/utils/hash.js +34 -0
  9. package/dist/cjs/utils/helper.js +27 -0
  10. package/dist/cjs/utils/inject-client-css.js +51 -0
  11. package/dist/cjs/utils/inject-client-global-css.js +18 -0
  12. package/dist/cjs/utils/inject-server-css.js +11 -0
  13. package/dist/cjs/utils/transpiler.js +125 -0
  14. package/dist/esm/index.js +8 -0
  15. package/dist/esm/types/common/css-properties.js +1 -0
  16. package/dist/esm/types/common/css-property.js +1 -0
  17. package/dist/esm/types/main/create.js +1 -0
  18. package/dist/esm/types/main/global.js +1 -0
  19. package/dist/esm/types/main/vars.js +1 -0
  20. package/dist/esm/utils/build.js +23 -0
  21. package/dist/esm/utils/hash.js +31 -0
  22. package/dist/esm/utils/helper.js +22 -0
  23. package/dist/esm/utils/inject-client-css.js +47 -0
  24. package/dist/esm/utils/inject-client-global-css.js +15 -0
  25. package/dist/esm/utils/inject-server-css.js +7 -0
  26. package/dist/esm/utils/transpiler.js +122 -0
  27. package/package.json +13 -10
  28. package/types/index.d.ts +12 -0
  29. package/types/types/common/css-properties.d.ts +46 -0
  30. package/types/types/common/css-property.d.ts +5 -0
  31. package/types/types/main/create.d.ts +10 -0
  32. package/types/types/main/global.d.ts +37 -0
  33. package/types/types/main/vars.d.ts +8 -0
  34. package/types/utils/build.d.ts +1 -0
  35. package/types/utils/hash.d.ts +2 -0
  36. package/types/utils/helper.d.ts +5 -0
  37. package/types/utils/inject-client-css.d.ts +2 -0
  38. package/types/utils/inject-client-global-css.d.ts +1 -0
  39. package/types/utils/inject-server-css.d.ts +2 -0
  40. package/types/utils/transpiler.d.ts +4 -0
  41. package/dist/index.d.ts +0 -111
  42. package/dist/index.js +0 -258
  43. package/dist/index.mjs +0 -247
package/dist/index.mjs DELETED
@@ -1,247 +0,0 @@
1
- //#region src/utils/helper.ts
2
- const isWindowDefined = typeof window !== "undefined";
3
- const isDocumentDefined = typeof document !== "undefined";
4
- const isServer = !isWindowDefined || !isDocumentDefined;
5
- const isDevelopment = process.env.NODE_ENV === "development";
6
- const isDevAndTest = process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test";
7
- const exception = [
8
- "line-height",
9
- "font-weight",
10
- "opacity",
11
- "scale",
12
- "z-index",
13
- "column-count",
14
- "order",
15
- "orphans",
16
- "widows"
17
- ];
18
- const applyCssValue = (value, cssProp) => {
19
- if (typeof value === "number") return exception.includes(cssProp) ? value.toString() : value + "px";
20
- return value;
21
- };
22
- const camelToKebabCase = (property) => {
23
- if (/^(ms|Moz|Webkit)/.test(property)) property = "-" + property;
24
- return property.replace(/([A-Z]+)([0-9]+)/g, "$1$2").replace(/([a-z0-9])([A-Z])/g, "$1-$2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1-$2").toLowerCase();
25
- };
26
-
27
- //#endregion
28
- //#region src/utils/hash.ts
29
- const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
30
- function simpleHash(str) {
31
- let hash = 0;
32
- for (let i = 0; i < str.length; i++) {
33
- const char = str.charCodeAt(i);
34
- hash = (hash << 5) - hash + char;
35
- }
36
- return Math.abs(hash);
37
- }
38
- function encodeBase36(num) {
39
- let result = "";
40
- do {
41
- result = chars[num % 36] + result;
42
- num = Math.floor(num / 36);
43
- } while (num > 0);
44
- return result;
45
- }
46
- function getStartingChar(hash) {
47
- const chars$1 = "abcdefghijklmnopqrstuvwxyz";
48
- return chars$1[hash % chars$1.length];
49
- }
50
- function genBase36Hash(object, n) {
51
- const serialized = JSON.stringify(object);
52
- const hash = simpleHash(serialized);
53
- let base36Hash = encodeBase36(hash);
54
- const startingChar = getStartingChar(hash);
55
- while (base36Hash.length < n - 1) base36Hash = chars[hash % chars.length] + base36Hash;
56
- return startingChar + base36Hash.slice(0, n - 1);
57
- }
58
-
59
- //#endregion
60
- //#region src/utils/transpiler.ts
61
- const createKeyframes = (property, content) => {
62
- let keyframesRules = `${property} {\n`;
63
- for (const key in content) if (Object.prototype.hasOwnProperty.call(content, key)) {
64
- const keyframeValue = content[key];
65
- keyframesRules += ` ${key} {\n`;
66
- for (const prop in keyframeValue) if (Object.prototype.hasOwnProperty.call(keyframeValue, prop)) {
67
- const CSSProp = camelToKebabCase(prop);
68
- const value = keyframeValue[prop];
69
- if (typeof value === "string" || typeof value === "number") {
70
- const applyValue = applyCssValue(value, CSSProp);
71
- keyframesRules += ` ${CSSProp}: ${applyValue};\n`;
72
- }
73
- }
74
- keyframesRules += ` }\n`;
75
- }
76
- keyframesRules += `}\n`;
77
- return keyframesRules;
78
- };
79
- function transpiler(object, base36Hash, core) {
80
- let styleSheet = "";
81
- const mediaQueries = [];
82
- const classNameType = (property) => {
83
- return core === "--global" ? property : `.${property}_${base36Hash}`;
84
- };
85
- const rules = (indent, rulesValue, property) => {
86
- if (typeof property !== "string") return "";
87
- const value = rulesValue[property];
88
- const cssProp = camelToKebabCase(property);
89
- return `${indent}${cssProp}: ${value};\n`;
90
- };
91
- const stringConverter = (className, properties, indentLevel = 0) => {
92
- const classSelector = {};
93
- const indent = "".repeat(indentLevel);
94
- const innerIndent = " ".repeat(indentLevel + 1);
95
- let cssRule = "";
96
- for (const property in properties) if (Object.prototype.hasOwnProperty.call(properties, property)) {
97
- const value = properties[property];
98
- if (typeof value === "string" || typeof value === "number") {
99
- let CSSProp = camelToKebabCase(property);
100
- if (property.startsWith("ms")) CSSProp = `-${CSSProp}`;
101
- const applyValue = applyCssValue(value, CSSProp);
102
- cssRule += ` ${CSSProp}: ${applyValue};\n`;
103
- } else if (!property.startsWith("@")) {
104
- const kebabPseudoSelector = camelToKebabCase(property.replace("&", ""));
105
- const styles = stringConverter(className + kebabPseudoSelector, value, indentLevel);
106
- Object.assign(classSelector, styles);
107
- } else if (property.startsWith("@media") || property.startsWith("@container")) {
108
- const mediaRule = property;
109
- let nestedRules = "";
110
- let regularRules = "";
111
- for (const mediaProp in value) if (Object.prototype.hasOwnProperty.call(value, mediaProp)) {
112
- const mediaValue = value[mediaProp];
113
- const isColon = mediaProp.startsWith(":");
114
- const isAnd = mediaProp.startsWith("&");
115
- if (isColon || isAnd) {
116
- const kebabMediaProp = camelToKebabCase(mediaProp.replace("&", ""));
117
- let pseudoClassRule = "";
118
- if (typeof mediaValue === "object" && mediaValue !== null) {
119
- for (const pseudoProp in mediaValue) if (Object.prototype.hasOwnProperty.call(mediaValue, pseudoProp)) {
120
- const CSSProp = camelToKebabCase(pseudoProp);
121
- const applyValue = applyCssValue(mediaValue[pseudoProp], CSSProp);
122
- pseudoClassRule += rules(innerIndent + " ", { [pseudoProp]: applyValue }, pseudoProp);
123
- }
124
- }
125
- nestedRules += `${innerIndent}${className}${kebabMediaProp} {\n${pseudoClassRule}${innerIndent}}\n`;
126
- } else {
127
- const CSSProp = camelToKebabCase(mediaProp);
128
- const applyValue = applyCssValue(mediaValue, CSSProp);
129
- regularRules += rules(innerIndent + " ", { [mediaProp]: applyValue }, mediaProp);
130
- }
131
- }
132
- if (regularRules) mediaQueries.push({
133
- media: mediaRule,
134
- css: `${mediaRule} {\n${innerIndent}${className} {\n${regularRules} }\n${nestedRules}${indent}}${indent}\n`
135
- });
136
- else mediaQueries.push({
137
- media: mediaRule,
138
- css: `${mediaRule} {\n${nestedRules}${indent}}\n`
139
- });
140
- }
141
- }
142
- classSelector[className] = cssRule;
143
- return classSelector;
144
- };
145
- for (const property in object) {
146
- if (property.startsWith("@keyframes")) {
147
- const keyframesContent = object[property];
148
- styleSheet += createKeyframes(property, keyframesContent);
149
- }
150
- const classSelectors = stringConverter(classNameType(property), object[property], 1);
151
- for (const selector in classSelectors) if (!selector.startsWith("@keyframes") && classSelectors[selector]) styleSheet += selector + " {\n" + classSelectors[selector] + "}\n";
152
- }
153
- mediaQueries.forEach(({ css }) => {
154
- styleSheet += css;
155
- });
156
- return { styleSheet };
157
- }
158
-
159
- //#endregion
160
- //#region src/utils/build.ts
161
- const build = async (styleSheet, filePath, global) => {
162
- if (!isServer) return;
163
- const fs = await import("fs");
164
- const { styleText } = await import("util");
165
- const message = global === "--global" ? styleText("underline", `✅Generated global CSS\n\n`) : styleText("underline", `✅Generated create CSS\n\n`);
166
- try {
167
- if (fs.existsSync(filePath)) {
168
- const cssData = fs.readFileSync(filePath, "utf-8");
169
- if (!cssData.includes(styleSheet)) {
170
- fs.appendFileSync(filePath, styleSheet, "utf-8");
171
- if (process.argv.includes("--view")) console.log(message + styleSheet);
172
- }
173
- }
174
- return;
175
- } catch (error) {
176
- console.error("Error writing to file:", error);
177
- }
178
- };
179
-
180
- //#endregion
181
- //#region src/utils/inject-client-css.ts
182
- const styleSheets$1 = {};
183
- const hashCache = {};
184
- function createStyleElement(hash) {
185
- if (document.getElementById(hash)) return null;
186
- const styleElement = document.createElement("style");
187
- styleElement.setAttribute("id", hash);
188
- styleElement.setAttribute("type", "text/css");
189
- styleSheets$1[hash] = styleElement;
190
- document.head.appendChild(styleElement);
191
- return styleSheets$1[hash];
192
- }
193
- function injectClientCSS(hash, sheet) {
194
- if (isServer) return;
195
- requestAnimationFrame(() => {
196
- styleCleanUp();
197
- });
198
- hashCache[hash] = hash;
199
- const styleElement = createStyleElement(hash);
200
- if (styleElement == null) return;
201
- styleElement.textContent = sheet;
202
- }
203
- function styleCleanUp() {
204
- requestAnimationFrame(() => {
205
- for (const hash in hashCache) {
206
- const classElements = document.querySelectorAll(`[class*="${hash}"]`);
207
- if (classElements.length === 0) removeStyleElement(hashCache[hash]);
208
- }
209
- });
210
- }
211
- function removeStyleElement(hash) {
212
- if (styleSheets$1[hash]) {
213
- delete styleSheets$1[hash];
214
- if (hashCache.hasOwnProperty.call(hashCache, hash)) delete hashCache[hash];
215
- const styleElement = document.getElementById(hash);
216
- if (styleElement) document.head.removeChild(styleElement);
217
- }
218
- }
219
-
220
- //#endregion
221
- //#region src/utils/inject-client-global-css.ts
222
- function injectClientGlobalCSS(sheet) {
223
- if (isServer) return;
224
- const existingStyleElement = document.querySelector(`[data-scope="global"]`);
225
- if (existingStyleElement instanceof HTMLStyleElement) {
226
- existingStyleElement.textContent += sheet;
227
- return;
228
- }
229
- const styleElement = document.createElement("style");
230
- styleElement.setAttribute("data-scope", "global");
231
- styleElement.setAttribute("type", "text/css");
232
- styleElement.textContent = sheet;
233
- document.head.appendChild(styleElement);
234
- }
235
-
236
- //#endregion
237
- //#region src/utils/inject-server-css.ts
238
- const styleSheets = {};
239
- function injectServerCSS(hash, sheet) {
240
- styleSheets[hash] = sheet;
241
- }
242
- function getServerCSS() {
243
- return Object.values(styleSheets).join("\n");
244
- }
245
-
246
- //#endregion
247
- export { build, camelToKebabCase, genBase36Hash, getServerCSS, injectClientCSS, injectClientGlobalCSS, injectServerCSS, isDevAndTest, isDevelopment, isServer, transpiler };