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
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getServerCSS = exports.injectServerCSS = exports.injectClientGlobalCSS = exports.injectClientCSS = exports.camelToKebabCase = exports.build = exports.transpiler = exports.genBase36Hash = exports.isDevAndTest = exports.isDevelopment = exports.isServer = void 0;
4
+ var helper_js_1 = require("./utils/helper.js");
5
+ Object.defineProperty(exports, "isServer", { enumerable: true, get: function () { return helper_js_1.isServer; } });
6
+ Object.defineProperty(exports, "isDevelopment", { enumerable: true, get: function () { return helper_js_1.isDevelopment; } });
7
+ Object.defineProperty(exports, "isDevAndTest", { enumerable: true, get: function () { return helper_js_1.isDevAndTest; } });
8
+ var hash_js_1 = require("./utils/hash.js");
9
+ Object.defineProperty(exports, "genBase36Hash", { enumerable: true, get: function () { return hash_js_1.genBase36Hash; } });
10
+ var transpiler_js_1 = require("./utils/transpiler.js");
11
+ Object.defineProperty(exports, "transpiler", { enumerable: true, get: function () { return transpiler_js_1.transpiler; } });
12
+ var build_js_1 = require("./utils/build.js");
13
+ Object.defineProperty(exports, "build", { enumerable: true, get: function () { return build_js_1.build; } });
14
+ var helper_js_2 = require("./utils/helper.js");
15
+ Object.defineProperty(exports, "camelToKebabCase", { enumerable: true, get: function () { return helper_js_2.camelToKebabCase; } });
16
+ var inject_client_css_js_1 = require("./utils/inject-client-css.js");
17
+ Object.defineProperty(exports, "injectClientCSS", { enumerable: true, get: function () { return inject_client_css_js_1.injectClientCSS; } });
18
+ var inject_client_global_css_js_1 = require("./utils/inject-client-global-css.js");
19
+ Object.defineProperty(exports, "injectClientGlobalCSS", { enumerable: true, get: function () { return inject_client_global_css_js_1.injectClientGlobalCSS; } });
20
+ var inject_server_css_js_1 = require("./utils/inject-server-css.js");
21
+ Object.defineProperty(exports, "injectServerCSS", { enumerable: true, get: function () { return inject_server_css_js_1.injectServerCSS; } });
22
+ Object.defineProperty(exports, "getServerCSS", { enumerable: true, get: function () { return inject_server_css_js_1.getServerCSS; } });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,27 @@
1
+ 'use server';
2
+ "use strict";
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.build = void 0;
5
+ const helper_js_1 = require("./helper.js");
6
+ const build = async (styleSheet, filePath, global) => {
7
+ if (!helper_js_1.isServer)
8
+ return;
9
+ const fs = await import('fs');
10
+ const { styleText } = await import('util');
11
+ const message = global === '--global' ? styleText('underline', `✅Generated global CSS\n\n`) : styleText('underline', `✅Generated create CSS\n\n`);
12
+ try {
13
+ if (fs.existsSync(filePath)) {
14
+ const cssData = fs.readFileSync(filePath, 'utf-8');
15
+ if (!cssData.includes(styleSheet)) {
16
+ fs.appendFileSync(filePath, styleSheet, 'utf-8');
17
+ if (process.argv.includes('--view'))
18
+ console.log(message + styleSheet);
19
+ }
20
+ }
21
+ return;
22
+ }
23
+ catch (error) {
24
+ console.error('Error writing to file:', error);
25
+ }
26
+ };
27
+ exports.build = build;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.genBase36Hash = genBase36Hash;
4
+ const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
5
+ function simpleHash(str) {
6
+ let hash = 0;
7
+ for (let i = 0; i < str.length; i++) {
8
+ const char = str.charCodeAt(i);
9
+ hash = (hash << 5) - hash + char;
10
+ }
11
+ return Math.abs(hash);
12
+ }
13
+ function encodeBase36(num) {
14
+ let result = '';
15
+ do {
16
+ result = chars[num % 36] + result;
17
+ num = Math.floor(num / 36);
18
+ } while (num > 0);
19
+ return result;
20
+ }
21
+ function getStartingChar(hash) {
22
+ const chars = 'abcdefghijklmnopqrstuvwxyz';
23
+ return chars[hash % chars.length];
24
+ }
25
+ function genBase36Hash(object, n) {
26
+ const serialized = JSON.stringify(object);
27
+ const hash = simpleHash(serialized);
28
+ let base36Hash = encodeBase36(hash);
29
+ const startingChar = getStartingChar(hash);
30
+ while (base36Hash.length < n - 1) {
31
+ base36Hash = chars[hash % chars.length] + base36Hash;
32
+ }
33
+ return startingChar + base36Hash.slice(0, n - 1);
34
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.camelToKebabCase = exports.applyCssValue = exports.isDevAndTest = exports.isDevelopment = exports.isServer = void 0;
4
+ const isWindowDefined = typeof window !== 'undefined';
5
+ const isDocumentDefined = typeof document !== 'undefined';
6
+ exports.isServer = !isWindowDefined || !isDocumentDefined;
7
+ exports.isDevelopment = process.env.NODE_ENV === 'development';
8
+ exports.isDevAndTest = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
9
+ const exception = ['line-height', 'font-weight', 'opacity', 'scale', 'z-index', 'column-count', 'order', 'orphans', 'widows'];
10
+ const applyCssValue = (value, cssProp) => {
11
+ if (typeof value === 'number') {
12
+ return exception.includes(cssProp) ? value.toString() : value + 'px';
13
+ }
14
+ return value;
15
+ };
16
+ exports.applyCssValue = applyCssValue;
17
+ const camelToKebabCase = (property) => {
18
+ if (/^(ms|Moz|Webkit)/.test(property)) {
19
+ property = '-' + property;
20
+ }
21
+ return (property
22
+ .replace(/([A-Z]+)([0-9]+)/g, '$1$2')
23
+ .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
24
+ .replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')
25
+ .toLowerCase());
26
+ };
27
+ exports.camelToKebabCase = camelToKebabCase;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createStyleElement = createStyleElement;
4
+ exports.injectClientCSS = injectClientCSS;
5
+ const index_js_1 = require("../index.js");
6
+ const styleSheets = {};
7
+ const hashCache = {};
8
+ function createStyleElement(hash) {
9
+ if (document.getElementById(hash))
10
+ return null;
11
+ const styleElement = document.createElement('style');
12
+ styleElement.setAttribute('id', hash);
13
+ styleElement.setAttribute('type', 'text/css');
14
+ styleSheets[hash] = styleElement;
15
+ document.head.appendChild(styleElement);
16
+ return styleSheets[hash];
17
+ }
18
+ function injectClientCSS(hash, sheet) {
19
+ if (index_js_1.isServer)
20
+ return;
21
+ requestAnimationFrame(() => {
22
+ styleCleanUp();
23
+ });
24
+ hashCache[hash] = hash;
25
+ const styleElement = createStyleElement(hash);
26
+ if (styleElement == null)
27
+ return;
28
+ styleElement.textContent = sheet;
29
+ }
30
+ function styleCleanUp() {
31
+ requestAnimationFrame(() => {
32
+ for (const hash in hashCache) {
33
+ const classElements = document.querySelectorAll(`[class*="${hash}"]`);
34
+ if (classElements.length === 0) {
35
+ removeStyleElement(hashCache[hash]);
36
+ }
37
+ }
38
+ });
39
+ }
40
+ function removeStyleElement(hash) {
41
+ if (styleSheets[hash]) {
42
+ delete styleSheets[hash];
43
+ if (hashCache.hasOwnProperty.call(hashCache, hash)) {
44
+ delete hashCache[hash];
45
+ }
46
+ const styleElement = document.getElementById(hash);
47
+ if (styleElement) {
48
+ document.head.removeChild(styleElement);
49
+ }
50
+ }
51
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.injectClientGlobalCSS = injectClientGlobalCSS;
4
+ const index_js_1 = require("../index.js");
5
+ function injectClientGlobalCSS(sheet) {
6
+ if (index_js_1.isServer)
7
+ return;
8
+ const existingStyleElement = document.querySelector(`[data-scope="global"]`);
9
+ if (existingStyleElement instanceof HTMLStyleElement) {
10
+ existingStyleElement.textContent += sheet;
11
+ return;
12
+ }
13
+ const styleElement = document.createElement('style');
14
+ styleElement.setAttribute('data-scope', 'global');
15
+ styleElement.setAttribute('type', 'text/css');
16
+ styleElement.textContent = sheet;
17
+ document.head.appendChild(styleElement);
18
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.injectServerCSS = injectServerCSS;
4
+ exports.getServerCSS = getServerCSS;
5
+ const styleSheets = {};
6
+ function injectServerCSS(hash, sheet) {
7
+ styleSheets[hash] = sheet;
8
+ }
9
+ function getServerCSS() {
10
+ return Object.values(styleSheets).join('\n');
11
+ }
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.transpiler = transpiler;
4
+ const helper_js_1 = require("./helper.js");
5
+ const createKeyframes = (property, content) => {
6
+ let keyframesRules = `${property} {\n`;
7
+ for (const key in content) {
8
+ if (Object.prototype.hasOwnProperty.call(content, key)) {
9
+ const keyframeValue = content[key];
10
+ keyframesRules += ` ${key} {\n`;
11
+ for (const prop in keyframeValue) {
12
+ if (Object.prototype.hasOwnProperty.call(keyframeValue, prop)) {
13
+ const CSSProp = (0, helper_js_1.camelToKebabCase)(prop);
14
+ const value = keyframeValue[prop];
15
+ if (typeof value === 'string' || typeof value === 'number') {
16
+ const applyValue = (0, helper_js_1.applyCssValue)(value, CSSProp);
17
+ keyframesRules += ` ${CSSProp}: ${applyValue};\n`;
18
+ }
19
+ }
20
+ }
21
+ keyframesRules += ` }\n`;
22
+ }
23
+ }
24
+ keyframesRules += `}\n`;
25
+ return keyframesRules;
26
+ };
27
+ function transpiler(object, base36Hash, core) {
28
+ let styleSheet = '';
29
+ const mediaQueries = [];
30
+ const classNameType = (property) => {
31
+ return core === '--global' ? property : `.${property}_${base36Hash}`;
32
+ };
33
+ const rules = (indent, rulesValue, property) => {
34
+ if (typeof property !== 'string')
35
+ return '';
36
+ const value = rulesValue[property];
37
+ const cssProp = (0, helper_js_1.camelToKebabCase)(property);
38
+ return `${indent}${cssProp}: ${value};\n`;
39
+ };
40
+ const stringConverter = (className, properties, indentLevel = 0) => {
41
+ const classSelector = {};
42
+ const indent = ''.repeat(indentLevel);
43
+ const innerIndent = ' '.repeat(indentLevel + 1);
44
+ let cssRule = '';
45
+ for (const property in properties) {
46
+ if (Object.prototype.hasOwnProperty.call(properties, property)) {
47
+ const value = properties[property];
48
+ if (typeof value === 'string' || typeof value === 'number') {
49
+ let CSSProp = (0, helper_js_1.camelToKebabCase)(property);
50
+ if (property.startsWith('ms')) {
51
+ CSSProp = `-${CSSProp}`;
52
+ }
53
+ const applyValue = (0, helper_js_1.applyCssValue)(value, CSSProp);
54
+ cssRule += ` ${CSSProp}: ${applyValue};\n`;
55
+ }
56
+ else if (!property.startsWith('@')) {
57
+ const kebabPseudoSelector = (0, helper_js_1.camelToKebabCase)(property.replace('&', ''));
58
+ const styles = stringConverter(className + kebabPseudoSelector, value, indentLevel);
59
+ Object.assign(classSelector, styles);
60
+ }
61
+ else if (property.startsWith('@media') || property.startsWith('@container')) {
62
+ const mediaRule = property;
63
+ let nestedRules = '';
64
+ let regularRules = '';
65
+ for (const mediaProp in value) {
66
+ if (Object.prototype.hasOwnProperty.call(value, mediaProp)) {
67
+ const mediaValue = value[mediaProp];
68
+ const isColon = mediaProp.startsWith(':');
69
+ const isAnd = mediaProp.startsWith('&');
70
+ if (isColon || isAnd) {
71
+ const kebabMediaProp = (0, helper_js_1.camelToKebabCase)(mediaProp.replace('&', ''));
72
+ let pseudoClassRule = '';
73
+ if (typeof mediaValue === 'object' && mediaValue !== null) {
74
+ for (const pseudoProp in mediaValue) {
75
+ if (Object.prototype.hasOwnProperty.call(mediaValue, pseudoProp)) {
76
+ const CSSProp = (0, helper_js_1.camelToKebabCase)(pseudoProp);
77
+ const applyValue = (0, helper_js_1.applyCssValue)(mediaValue[pseudoProp], CSSProp);
78
+ pseudoClassRule += rules(innerIndent + ' ', { [pseudoProp]: applyValue }, pseudoProp);
79
+ }
80
+ }
81
+ }
82
+ nestedRules += `${innerIndent}${className}${kebabMediaProp} {\n${pseudoClassRule}${innerIndent}}\n`;
83
+ }
84
+ else {
85
+ const CSSProp = (0, helper_js_1.camelToKebabCase)(mediaProp);
86
+ const applyValue = (0, helper_js_1.applyCssValue)(mediaValue, CSSProp);
87
+ regularRules += rules(innerIndent + ' ', { [mediaProp]: applyValue }, mediaProp);
88
+ }
89
+ }
90
+ }
91
+ if (regularRules) {
92
+ mediaQueries.push({
93
+ media: mediaRule,
94
+ css: `${mediaRule} {\n${innerIndent}${className} {\n${regularRules} }\n${nestedRules}${indent}}${indent}\n`,
95
+ });
96
+ }
97
+ else {
98
+ mediaQueries.push({
99
+ media: mediaRule,
100
+ css: `${mediaRule} {\n${nestedRules}${indent}}\n`,
101
+ });
102
+ }
103
+ }
104
+ }
105
+ }
106
+ classSelector[className] = cssRule;
107
+ return classSelector;
108
+ };
109
+ for (const property in object) {
110
+ if (property.startsWith('@keyframes')) {
111
+ const keyframesContent = object[property];
112
+ styleSheet += createKeyframes(property, keyframesContent);
113
+ }
114
+ const classSelectors = stringConverter(classNameType(property), object[property], 1);
115
+ for (const selector in classSelectors) {
116
+ if (!selector.startsWith('@keyframes') && classSelectors[selector]) {
117
+ styleSheet += selector + ' {\n' + classSelectors[selector] + '}\n';
118
+ }
119
+ }
120
+ }
121
+ mediaQueries.forEach(({ css }) => {
122
+ styleSheet += css;
123
+ });
124
+ return { styleSheet };
125
+ }
@@ -0,0 +1,8 @@
1
+ export { isServer, isDevelopment, isDevAndTest } from './utils/helper.js';
2
+ export { genBase36Hash } from './utils/hash.js';
3
+ export { transpiler } from './utils/transpiler.js';
4
+ export { build } from './utils/build.js';
5
+ export { camelToKebabCase } from './utils/helper.js';
6
+ export { injectClientCSS } from './utils/inject-client-css.js';
7
+ export { injectClientGlobalCSS } from './utils/inject-client-global-css.js';
8
+ export { injectServerCSS, getServerCSS } from './utils/inject-server-css.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,23 @@
1
+ 'use server';
2
+ import { isServer } from './helper.js';
3
+ export const build = async (styleSheet, filePath, global) => {
4
+ if (!isServer)
5
+ return;
6
+ const fs = await import('fs');
7
+ const { styleText } = await import('util');
8
+ const message = global === '--global' ? styleText('underline', `✅Generated global CSS\n\n`) : styleText('underline', `✅Generated create CSS\n\n`);
9
+ try {
10
+ if (fs.existsSync(filePath)) {
11
+ const cssData = fs.readFileSync(filePath, 'utf-8');
12
+ if (!cssData.includes(styleSheet)) {
13
+ fs.appendFileSync(filePath, styleSheet, 'utf-8');
14
+ if (process.argv.includes('--view'))
15
+ console.log(message + styleSheet);
16
+ }
17
+ }
18
+ return;
19
+ }
20
+ catch (error) {
21
+ console.error('Error writing to file:', error);
22
+ }
23
+ };
@@ -0,0 +1,31 @@
1
+ const chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
2
+ function simpleHash(str) {
3
+ let hash = 0;
4
+ for (let i = 0; i < str.length; i++) {
5
+ const char = str.charCodeAt(i);
6
+ hash = (hash << 5) - hash + char;
7
+ }
8
+ return Math.abs(hash);
9
+ }
10
+ function encodeBase36(num) {
11
+ let result = '';
12
+ do {
13
+ result = chars[num % 36] + result;
14
+ num = Math.floor(num / 36);
15
+ } while (num > 0);
16
+ return result;
17
+ }
18
+ function getStartingChar(hash) {
19
+ const chars = 'abcdefghijklmnopqrstuvwxyz';
20
+ return chars[hash % chars.length];
21
+ }
22
+ export function genBase36Hash(object, n) {
23
+ const serialized = JSON.stringify(object);
24
+ const hash = simpleHash(serialized);
25
+ let base36Hash = encodeBase36(hash);
26
+ const startingChar = getStartingChar(hash);
27
+ while (base36Hash.length < n - 1) {
28
+ base36Hash = chars[hash % chars.length] + base36Hash;
29
+ }
30
+ return startingChar + base36Hash.slice(0, n - 1);
31
+ }
@@ -0,0 +1,22 @@
1
+ const isWindowDefined = typeof window !== 'undefined';
2
+ const isDocumentDefined = typeof document !== 'undefined';
3
+ export const isServer = !isWindowDefined || !isDocumentDefined;
4
+ export const isDevelopment = process.env.NODE_ENV === 'development';
5
+ export const isDevAndTest = process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
6
+ const exception = ['line-height', 'font-weight', 'opacity', 'scale', 'z-index', 'column-count', 'order', 'orphans', 'widows'];
7
+ export const applyCssValue = (value, cssProp) => {
8
+ if (typeof value === 'number') {
9
+ return exception.includes(cssProp) ? value.toString() : value + 'px';
10
+ }
11
+ return value;
12
+ };
13
+ export const camelToKebabCase = (property) => {
14
+ if (/^(ms|Moz|Webkit)/.test(property)) {
15
+ property = '-' + property;
16
+ }
17
+ return (property
18
+ .replace(/([A-Z]+)([0-9]+)/g, '$1$2')
19
+ .replace(/([a-z0-9])([A-Z])/g, '$1-$2')
20
+ .replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2')
21
+ .toLowerCase());
22
+ };
@@ -0,0 +1,47 @@
1
+ import { isServer } from '../index.js';
2
+ const styleSheets = {};
3
+ const hashCache = {};
4
+ export function createStyleElement(hash) {
5
+ if (document.getElementById(hash))
6
+ return null;
7
+ const styleElement = document.createElement('style');
8
+ styleElement.setAttribute('id', hash);
9
+ styleElement.setAttribute('type', 'text/css');
10
+ styleSheets[hash] = styleElement;
11
+ document.head.appendChild(styleElement);
12
+ return styleSheets[hash];
13
+ }
14
+ export function injectClientCSS(hash, sheet) {
15
+ if (isServer)
16
+ return;
17
+ requestAnimationFrame(() => {
18
+ styleCleanUp();
19
+ });
20
+ hashCache[hash] = hash;
21
+ const styleElement = createStyleElement(hash);
22
+ if (styleElement == null)
23
+ return;
24
+ styleElement.textContent = sheet;
25
+ }
26
+ function styleCleanUp() {
27
+ requestAnimationFrame(() => {
28
+ for (const hash in hashCache) {
29
+ const classElements = document.querySelectorAll(`[class*="${hash}"]`);
30
+ if (classElements.length === 0) {
31
+ removeStyleElement(hashCache[hash]);
32
+ }
33
+ }
34
+ });
35
+ }
36
+ function removeStyleElement(hash) {
37
+ if (styleSheets[hash]) {
38
+ delete styleSheets[hash];
39
+ if (hashCache.hasOwnProperty.call(hashCache, hash)) {
40
+ delete hashCache[hash];
41
+ }
42
+ const styleElement = document.getElementById(hash);
43
+ if (styleElement) {
44
+ document.head.removeChild(styleElement);
45
+ }
46
+ }
47
+ }
@@ -0,0 +1,15 @@
1
+ import { isServer } from '../index.js';
2
+ export function injectClientGlobalCSS(sheet) {
3
+ if (isServer)
4
+ return;
5
+ const existingStyleElement = document.querySelector(`[data-scope="global"]`);
6
+ if (existingStyleElement instanceof HTMLStyleElement) {
7
+ existingStyleElement.textContent += sheet;
8
+ return;
9
+ }
10
+ const styleElement = document.createElement('style');
11
+ styleElement.setAttribute('data-scope', 'global');
12
+ styleElement.setAttribute('type', 'text/css');
13
+ styleElement.textContent = sheet;
14
+ document.head.appendChild(styleElement);
15
+ }
@@ -0,0 +1,7 @@
1
+ const styleSheets = {};
2
+ export function injectServerCSS(hash, sheet) {
3
+ styleSheets[hash] = sheet;
4
+ }
5
+ export function getServerCSS() {
6
+ return Object.values(styleSheets).join('\n');
7
+ }
@@ -0,0 +1,122 @@
1
+ import { camelToKebabCase, applyCssValue } from './helper.js';
2
+ const createKeyframes = (property, content) => {
3
+ let keyframesRules = `${property} {\n`;
4
+ for (const key in content) {
5
+ if (Object.prototype.hasOwnProperty.call(content, key)) {
6
+ const keyframeValue = content[key];
7
+ keyframesRules += ` ${key} {\n`;
8
+ for (const prop in keyframeValue) {
9
+ if (Object.prototype.hasOwnProperty.call(keyframeValue, prop)) {
10
+ const CSSProp = camelToKebabCase(prop);
11
+ const value = keyframeValue[prop];
12
+ if (typeof value === 'string' || typeof value === 'number') {
13
+ const applyValue = applyCssValue(value, CSSProp);
14
+ keyframesRules += ` ${CSSProp}: ${applyValue};\n`;
15
+ }
16
+ }
17
+ }
18
+ keyframesRules += ` }\n`;
19
+ }
20
+ }
21
+ keyframesRules += `}\n`;
22
+ return keyframesRules;
23
+ };
24
+ export function transpiler(object, base36Hash, core) {
25
+ let styleSheet = '';
26
+ const mediaQueries = [];
27
+ const classNameType = (property) => {
28
+ return core === '--global' ? property : `.${property}_${base36Hash}`;
29
+ };
30
+ const rules = (indent, rulesValue, property) => {
31
+ if (typeof property !== 'string')
32
+ return '';
33
+ const value = rulesValue[property];
34
+ const cssProp = camelToKebabCase(property);
35
+ return `${indent}${cssProp}: ${value};\n`;
36
+ };
37
+ const stringConverter = (className, properties, indentLevel = 0) => {
38
+ const classSelector = {};
39
+ const indent = ''.repeat(indentLevel);
40
+ const innerIndent = ' '.repeat(indentLevel + 1);
41
+ let cssRule = '';
42
+ for (const property in properties) {
43
+ if (Object.prototype.hasOwnProperty.call(properties, property)) {
44
+ const value = properties[property];
45
+ if (typeof value === 'string' || typeof value === 'number') {
46
+ let CSSProp = camelToKebabCase(property);
47
+ if (property.startsWith('ms')) {
48
+ CSSProp = `-${CSSProp}`;
49
+ }
50
+ const applyValue = applyCssValue(value, CSSProp);
51
+ cssRule += ` ${CSSProp}: ${applyValue};\n`;
52
+ }
53
+ else if (!property.startsWith('@')) {
54
+ const kebabPseudoSelector = camelToKebabCase(property.replace('&', ''));
55
+ const styles = stringConverter(className + kebabPseudoSelector, value, indentLevel);
56
+ Object.assign(classSelector, styles);
57
+ }
58
+ else if (property.startsWith('@media') || property.startsWith('@container')) {
59
+ const mediaRule = property;
60
+ let nestedRules = '';
61
+ let regularRules = '';
62
+ for (const mediaProp in value) {
63
+ if (Object.prototype.hasOwnProperty.call(value, mediaProp)) {
64
+ const mediaValue = value[mediaProp];
65
+ const isColon = mediaProp.startsWith(':');
66
+ const isAnd = mediaProp.startsWith('&');
67
+ if (isColon || isAnd) {
68
+ const kebabMediaProp = camelToKebabCase(mediaProp.replace('&', ''));
69
+ let pseudoClassRule = '';
70
+ if (typeof mediaValue === 'object' && mediaValue !== null) {
71
+ for (const pseudoProp in mediaValue) {
72
+ if (Object.prototype.hasOwnProperty.call(mediaValue, pseudoProp)) {
73
+ const CSSProp = camelToKebabCase(pseudoProp);
74
+ const applyValue = applyCssValue(mediaValue[pseudoProp], CSSProp);
75
+ pseudoClassRule += rules(innerIndent + ' ', { [pseudoProp]: applyValue }, pseudoProp);
76
+ }
77
+ }
78
+ }
79
+ nestedRules += `${innerIndent}${className}${kebabMediaProp} {\n${pseudoClassRule}${innerIndent}}\n`;
80
+ }
81
+ else {
82
+ const CSSProp = camelToKebabCase(mediaProp);
83
+ const applyValue = applyCssValue(mediaValue, CSSProp);
84
+ regularRules += rules(innerIndent + ' ', { [mediaProp]: applyValue }, mediaProp);
85
+ }
86
+ }
87
+ }
88
+ if (regularRules) {
89
+ mediaQueries.push({
90
+ media: mediaRule,
91
+ css: `${mediaRule} {\n${innerIndent}${className} {\n${regularRules} }\n${nestedRules}${indent}}${indent}\n`,
92
+ });
93
+ }
94
+ else {
95
+ mediaQueries.push({
96
+ media: mediaRule,
97
+ css: `${mediaRule} {\n${nestedRules}${indent}}\n`,
98
+ });
99
+ }
100
+ }
101
+ }
102
+ }
103
+ classSelector[className] = cssRule;
104
+ return classSelector;
105
+ };
106
+ for (const property in object) {
107
+ if (property.startsWith('@keyframes')) {
108
+ const keyframesContent = object[property];
109
+ styleSheet += createKeyframes(property, keyframesContent);
110
+ }
111
+ const classSelectors = stringConverter(classNameType(property), object[property], 1);
112
+ for (const selector in classSelectors) {
113
+ if (!selector.startsWith('@keyframes') && classSelectors[selector]) {
114
+ styleSheet += selector + ' {\n' + classSelectors[selector] + '}\n';
115
+ }
116
+ }
117
+ }
118
+ mediaQueries.forEach(({ css }) => {
119
+ styleSheet += css;
120
+ });
121
+ return { styleSheet };
122
+ }