css-engine-test-pb 0.0.4 → 0.0.6

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.
@@ -4,8 +4,14 @@ interface CompiledRule {
4
4
  cssText: string;
5
5
  bucket: StyleBucket;
6
6
  }
7
+ export type CompileMode = 'atomic' | 'monolithic';
7
8
  export declare class StyleCompiler {
8
- compile(styleDefinition: StyleRule): CompiledRule[];
9
+ compile(styleDefinition: StyleRule, mode?: CompileMode): CompiledRule[];
10
+ private compileAtomic;
11
+ private compileMonolithic;
12
+ private buildCSSBlock;
13
+ private buildMonolithicPseudos;
14
+ private buildMonolithicMedia;
9
15
  private processProperties;
10
16
  private processPseudos;
11
17
  private processMediaQueries;
package/dist/compiler.js CHANGED
@@ -1,13 +1,87 @@
1
- import { createClassName } from './utils/hash';
1
+ import { StyleBucket } from './types';
2
+ import { createClassName, hashString } from './utils/hash';
2
3
  import { camelToKebab, isPseudoSelector, isMediaQuery, determineBucket } from './utils';
3
4
  export class StyleCompiler {
4
- compile(styleDefinition) {
5
+ compile(styleDefinition, mode = 'atomic') {
6
+ if (mode === 'monolithic') {
7
+ return this.compileMonolithic(styleDefinition);
8
+ }
9
+ return this.compileAtomic(styleDefinition);
10
+ }
11
+ compileAtomic(styleDefinition) {
5
12
  const rules = [];
6
13
  this.processProperties(styleDefinition, rules);
7
14
  this.processPseudos(styleDefinition, rules);
8
15
  this.processMediaQueries(styleDefinition, rules);
9
16
  return rules;
10
17
  }
18
+ compileMonolithic(styleDefinition) {
19
+ const hash = hashString(JSON.stringify(styleDefinition));
20
+ const className = 'r' + hash;
21
+ let cssText = '';
22
+ const mainStyles = this.buildCSSBlock(styleDefinition);
23
+ if (mainStyles) {
24
+ cssText += `.${className} { ${mainStyles} }\n`;
25
+ }
26
+ cssText += this.buildMonolithicPseudos(styleDefinition, className);
27
+ cssText += this.buildMonolithicMedia(styleDefinition, className);
28
+ return [{
29
+ className,
30
+ cssText: cssText.trim(),
31
+ bucket: StyleBucket.RESET
32
+ }];
33
+ }
34
+ buildCSSBlock(styles) {
35
+ let css = '';
36
+ for (const [property, value] of Object.entries(styles)) {
37
+ if (isPseudoSelector(property) || isMediaQuery(property)) {
38
+ continue;
39
+ }
40
+ if (value === undefined || value === null) {
41
+ continue;
42
+ }
43
+ const kebabProperty = camelToKebab(property);
44
+ css += `${kebabProperty}: ${value}; `;
45
+ }
46
+ return css.trim();
47
+ }
48
+ buildMonolithicPseudos(styleDefinition, className) {
49
+ let css = '';
50
+ for (const [key, value] of Object.entries(styleDefinition)) {
51
+ if (isPseudoSelector(key) && value) {
52
+ const pseudoStyles = this.buildCSSBlock(value);
53
+ if (pseudoStyles) {
54
+ css += `.${className}${key} { ${pseudoStyles} }\n`;
55
+ }
56
+ }
57
+ }
58
+ return css;
59
+ }
60
+ buildMonolithicMedia(styleDefinition, className) {
61
+ let css = '';
62
+ for (const [key, value] of Object.entries(styleDefinition)) {
63
+ if (isMediaQuery(key) && value) {
64
+ const nestedStyles = value;
65
+ let mediaCss = '';
66
+ const mainStyles = this.buildCSSBlock(nestedStyles);
67
+ if (mainStyles) {
68
+ mediaCss += `.${className} { ${mainStyles} }\n`;
69
+ }
70
+ for (const [nestedKey, nestedValue] of Object.entries(nestedStyles)) {
71
+ if (isPseudoSelector(nestedKey) && nestedValue) {
72
+ const pseudoStyles = this.buildCSSBlock(nestedValue);
73
+ if (pseudoStyles) {
74
+ mediaCss += `.${className}${nestedKey} { ${pseudoStyles} }\n`;
75
+ }
76
+ }
77
+ }
78
+ if (mediaCss) {
79
+ css += `${key} {\n${mediaCss}}\n`;
80
+ }
81
+ }
82
+ }
83
+ return css;
84
+ }
11
85
  processProperties(styles, rules, pseudo, media) {
12
86
  Object.entries(styles).forEach(([property, value]) => {
13
87
  if (isPseudoSelector(property) || isMediaQuery(property)) {
package/dist/engine.d.ts CHANGED
@@ -4,7 +4,7 @@ export declare class StyleEngine {
4
4
  private insertion;
5
5
  private cache;
6
6
  constructor();
7
- createStyle(styleDefinition: StyleRule): string;
7
+ createStyle(styleDefinition: StyleRule, mode?: 'atomic' | 'monolithic'): string;
8
8
  createStyles(slots: StyleSlots): GeneratedClasses;
9
9
  getCSSString(): string;
10
10
  clear(): void;
package/dist/engine.js CHANGED
@@ -6,12 +6,12 @@ export class StyleEngine {
6
6
  this.insertion = new InsertionFactory();
7
7
  this.cache = new Map();
8
8
  }
9
- createStyle(styleDefinition) {
10
- const cacheKey = JSON.stringify(styleDefinition);
9
+ createStyle(styleDefinition, mode = 'atomic') {
10
+ const cacheKey = `${mode}:${JSON.stringify(styleDefinition)}`;
11
11
  if (this.cache.has(cacheKey)) {
12
12
  return this.cache.get(cacheKey);
13
13
  }
14
- const rules = this.compiler.compile(styleDefinition);
14
+ const rules = this.compiler.compile(styleDefinition, mode);
15
15
  rules.forEach(rule => {
16
16
  this.insertion.insertRule(rule.cssText, rule.bucket);
17
17
  });
@@ -25,7 +25,7 @@ export class StyleEngine {
25
25
  const resolvedStyle = typeof styleDefinition === 'function'
26
26
  ? styleDefinition()
27
27
  : styleDefinition;
28
- classes[slotName] = this.createStyle(resolvedStyle);
28
+ classes[slotName] = this.createStyle(resolvedStyle, 'atomic');
29
29
  });
30
30
  return classes;
31
31
  }
@@ -1,2 +1,2 @@
1
1
  import { StyleRule } from './types';
2
- export declare function makeResetStyles(styleDefinition: StyleRule): string;
2
+ export declare function makeResetStyles(styleDefinition: StyleRule): () => string;
@@ -1,5 +1,6 @@
1
1
  import { getGlobalEngine } from './engine';
2
2
  export function makeResetStyles(styleDefinition) {
3
3
  const engine = getGlobalEngine();
4
- return engine.createStyle(styleDefinition);
4
+ const className = engine.createStyle(styleDefinition, 'monolithic');
5
+ return () => className;
5
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-engine-test-pb",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",