@navita/engine 0.2.0 → 0.2.2

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.
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var helpers_hyphenateProperty = require('./hyphenateProperty.cjs');
3
+ var hyphenateProperty = require('./hyphenateProperty.cjs');
4
4
 
5
5
  // https://github.com/styletron/styletron/blob/b552ddc5050a8cc5eec84a46a299d937d3bb0112/packages/styletron-engine-atomic/src/css.ts#L36
6
6
  function declarationsToBlock(style) {
@@ -8,7 +8,7 @@ function declarationsToBlock(style) {
8
8
  for(const prop in style){
9
9
  const val = style[prop];
10
10
  if (typeof val === "string" || typeof val === "number") {
11
- css += `${helpers_hyphenateProperty.hyphenateProperty(prop)}:${val};`;
11
+ css += `${hyphenateProperty.hyphenateProperty(prop)}:${val};`;
12
12
  }
13
13
  }
14
14
  return css.slice(0, -1);
@@ -1,9 +1,9 @@
1
1
  'use strict';
2
2
 
3
- var identifiers_alphaIDGenerator = require('./alphaIDGenerator.cjs');
3
+ var alphaIDGenerator = require('./alphaIDGenerator.cjs');
4
4
 
5
5
  class PropertyValueIDGenerator {
6
- property = new identifiers_alphaIDGenerator.AlphaIDGenerator();
6
+ property = new alphaIDGenerator.AlphaIDGenerator();
7
7
  cache = {};
8
8
  next({ property , media ='' , support ='' , container ='' , pseudo ='' , value }) {
9
9
  const propertyKey = `m${media}s${support}c${container}ps${pseudo}p${property}`;
package/index.cjs CHANGED
@@ -3,20 +3,20 @@
3
3
  var path = require('node:path');
4
4
  var hash = require('@emotion/hash');
5
5
  var cache = require('./cache.cjs');
6
- var helpers_isObject = require('./helpers/isObject.cjs');
7
- var helpers_splitStyleBlocks = require('./helpers/splitStyleBlocks.cjs');
8
- var identifiers_IDGenerator = require('./identifiers/IDGenerator.cjs');
9
- var identifiers_alphaIDGenerator = require('./identifiers/alphaIDGenerator.cjs');
10
- var identifiers_propertyValueIDGenerator = require('./identifiers/propertyValueIDGenerator.cjs');
11
- var printers_printFontFaces = require('./printers/printFontFaces.cjs');
12
- var printers_printKeyFrames = require('./printers/printKeyFrames.cjs');
13
- var printers_printSourceMap = require('./printers/printSourceMap.cjs');
14
- var printers_printStyleBlocks = require('./printers/printStyleBlocks.cjs');
15
- var printers_sortAtRules = require('./printers/sortAtRules.cjs');
6
+ var isObject = require('./helpers/isObject.cjs');
7
+ var splitStyleBlocks = require('./helpers/splitStyleBlocks.cjs');
8
+ var IDGenerator = require('./identifiers/IDGenerator.cjs');
9
+ var alphaIDGenerator = require('./identifiers/alphaIDGenerator.cjs');
10
+ var propertyValueIDGenerator = require('./identifiers/propertyValueIDGenerator.cjs');
11
+ var printFontFaces = require('./printers/printFontFaces.cjs');
12
+ var printKeyFrames = require('./printers/printKeyFrames.cjs');
13
+ var printSourceMap = require('./printers/printSourceMap.cjs');
14
+ var printStyleBlocks = require('./printers/printStyleBlocks.cjs');
15
+ var sortAtRules = require('./printers/sortAtRules.cjs');
16
16
  var processKeyframes = require('./processKeyframes.cjs');
17
17
  var processStyles = require('./processStyles.cjs');
18
- var wrappers_classList = require('./wrappers/classList.cjs');
19
- var wrappers_static = require('./wrappers/static.cjs');
18
+ var classList = require('./wrappers/classList.cjs');
19
+ var _static = require('./wrappers/static.cjs');
20
20
 
21
21
  const defaultOptions = {
22
22
  enableSourceMaps: false,
@@ -24,11 +24,11 @@ const defaultOptions = {
24
24
  };
25
25
  class Engine {
26
26
  caches = {
27
- rule: new cache.Cache(new identifiers_propertyValueIDGenerator.PropertyValueIDGenerator()),
28
- static: new cache.Cache(new identifiers_IDGenerator.IDGenerator()),
29
- keyframes: new cache.Cache(new identifiers_alphaIDGenerator.AlphaIDGenerator()),
30
- fontFace: new cache.Cache(new identifiers_alphaIDGenerator.AlphaIDGenerator()),
31
- identifiers: new cache.Cache(new identifiers_alphaIDGenerator.AlphaIDGenerator())
27
+ rule: new cache.Cache(new propertyValueIDGenerator.PropertyValueIDGenerator()),
28
+ static: new cache.Cache(new IDGenerator.IDGenerator()),
29
+ keyframes: new cache.Cache(new alphaIDGenerator.AlphaIDGenerator()),
30
+ fontFace: new cache.Cache(new alphaIDGenerator.AlphaIDGenerator()),
31
+ identifiers: new cache.Cache(new alphaIDGenerator.AlphaIDGenerator())
32
32
  };
33
33
  usedIds = {};
34
34
  identifierCount = 0;
@@ -53,7 +53,7 @@ class Engine {
53
53
  styles,
54
54
  selector
55
55
  }).map((style)=>style.id));
56
- return new wrappers_static.Static();
56
+ return new _static.Static();
57
57
  }
58
58
  addStyle(styles) {
59
59
  const rules = processStyles.processStyles({
@@ -64,7 +64,7 @@ class Engine {
64
64
  });
65
65
  const ids = rules.map((rule)=>rule.id);
66
66
  this.addUsedIds("rule", ids);
67
- return new wrappers_classList.ClassList(ids.join(" "));
67
+ return new classList.ClassList(ids.join(" "));
68
68
  }
69
69
  addFontFace(fontFace) {
70
70
  const { id } = this.caches.fontFace.getOrStore({
@@ -135,13 +135,13 @@ class Engine {
135
135
  const { filePaths , usedIds , opinionatedLayers =false } = options || {};
136
136
  // We prioritize ids over filePaths. If neither are provided, we use all the used filePaths.
137
137
  const { keyframes: keyframesCache , fontFace: fontFaceCache , static: staticCache , rule: ruleCache } = usedIds ?? this.getCacheIds(filePaths ?? Object.keys(this.usedIds));
138
- const { atRules , lowPrioRules , rules } = helpers_splitStyleBlocks.splitStyleBlocks(this.caches.rule.items(ruleCache));
139
- const keyFrameCss = printers_printKeyFrames.printKeyFrames(this.caches.keyframes.items(keyframesCache));
140
- const fontFaceCss = printers_printFontFaces.printFontFaces(this.caches.fontFace.items(fontFaceCache));
141
- const staticCss = printers_printStyleBlocks.printStyleBlocks(this.caches.static.items(staticCache));
142
- const atRulesCss = printers_printStyleBlocks.printStyleBlocks(printers_sortAtRules.sortAtRules(atRules));
143
- const lowPrioRulesCss = printers_printStyleBlocks.printStyleBlocks(lowPrioRules);
144
- const rulesCss = printers_printStyleBlocks.printStyleBlocks(rules);
138
+ const { atRules , lowPrioRules , rules } = splitStyleBlocks.splitStyleBlocks(this.caches.rule.items(ruleCache));
139
+ const keyFrameCss = printKeyFrames.printKeyFrames(this.caches.keyframes.items(keyframesCache));
140
+ const fontFaceCss = printFontFaces.printFontFaces(this.caches.fontFace.items(fontFaceCache));
141
+ const staticCss = printStyleBlocks.printStyleBlocks(this.caches.static.items(staticCache));
142
+ const atRulesCss = printStyleBlocks.printStyleBlocks(sortAtRules.sortAtRules(atRules));
143
+ const lowPrioRulesCss = printStyleBlocks.printStyleBlocks(lowPrioRules);
144
+ const rulesCss = printStyleBlocks.printStyleBlocks(rules);
145
145
  if (opinionatedLayers) {
146
146
  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}}` : '');
147
147
  if (result.length > 0) {
@@ -155,7 +155,7 @@ class Engine {
155
155
  }
156
156
  const content = keyFrameCss + fontFaceCss + staticCss + lowPrioRulesCss + rulesCss + atRulesCss;
157
157
  if (this.options.enableSourceMaps) {
158
- return printers_printSourceMap.printSourceMap(this.sourceMapReferences, content);
158
+ return printSourceMap.printSourceMap(this.sourceMapReferences, content);
159
159
  }
160
160
  return content;
161
161
  }
@@ -173,7 +173,7 @@ class Engine {
173
173
  const data = JSON.parse(buffer.toString());
174
174
  function assign(target, source) {
175
175
  for(const key in source){
176
- if (helpers_isObject.isObject(target[key]) && helpers_isObject.isObject(source[key])) {
176
+ if (isObject.isObject(target[key]) && isObject.isObject(source[key])) {
177
177
  assign(target[key], source[key]);
178
178
  } else {
179
179
  target[key] = source[key];
@@ -236,6 +236,6 @@ class Engine {
236
236
  }
237
237
  }
238
238
 
239
- exports.ClassList = wrappers_classList.ClassList;
240
- exports.Static = wrappers_static.Static;
239
+ exports.ClassList = classList.ClassList;
240
+ exports.Static = _static.Static;
241
241
  exports.Engine = Engine;
package/index.d.ts CHANGED
@@ -1,4 +1,26 @@
1
- import { StyleRule, FontFaceRule, CSSKeyframes } from '@navita/types';
1
+ import { CSSKeyframes, FontFaceRule, StyleRule } from '@navita/types';
2
+
3
+ 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;
13
+ }
14
+ interface KeyframesBlock {
15
+ type: 'keyframes';
16
+ rule: CSSKeyframes;
17
+ id: string;
18
+ }
19
+ interface FontFaceBlock {
20
+ type: 'fontFace';
21
+ rule: FontFaceRule[];
22
+ id: string;
23
+ }
2
24
 
3
25
  declare class ClassList extends String {
4
26
  }
@@ -10,6 +32,10 @@ type CacheKeys = keyof Engine["caches"];
10
32
  type UsedIdCache = {
11
33
  [key in CacheKeys]?: (string | number)[];
12
34
  };
35
+ interface Identifier {
36
+ value: string;
37
+ id: string;
38
+ }
13
39
  type Options = {
14
40
  context?: string;
15
41
  enableSourceMaps?: boolean;
@@ -47,7 +73,23 @@ declare class Engine {
47
73
  deserialize(buffer: Buffer | string): Promise<void>;
48
74
  setFilePath(filePath: string | undefined): void;
49
75
  getUsedFilePaths(): string[];
50
- getItems(caches: UsedIdCache): {};
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
+ };
51
93
  clearUsedIds(filePath: string): void;
52
94
  private addUsedIds;
53
95
  getCacheIds(filePaths?: string[]): UsedIdCache;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@navita/engine",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Navitas CSS-in-JS engine",
5
5
  "keywords": [
6
6
  "css-in-js",
@@ -17,9 +17,9 @@
17
17
  }
18
18
  },
19
19
  "dependencies": {
20
- "sort-css-media-queries": "^2.2.0",
21
- "@emotion/hash": "^0.9.1",
22
- "source-map": "^0.7.4"
20
+ "sort-css-media-queries": "2.4.0",
21
+ "@emotion/hash": "0.9.2",
22
+ "source-map": "0.7.4"
23
23
  },
24
24
  "license": "MIT",
25
25
  "author": "Eagerpatch",
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
- var helpers_declarationsToBlock = require('../helpers/declarationsToBlock.cjs');
3
+ var declarationsToBlock = require('../helpers/declarationsToBlock.cjs');
4
4
 
5
5
  function printFontFaces(blocks) {
6
6
  let fontFaces = '';
7
7
  for (const block of blocks){
8
8
  for (const rule of block.rule){
9
- fontFaces += `@font-face{font-family:${block.id};${helpers_declarationsToBlock.declarationsToBlock(rule)}}`;
9
+ fontFaces += `@font-face{font-family:${block.id};${declarationsToBlock.declarationsToBlock(rule)}}`;
10
10
  }
11
11
  }
12
12
  return fontFaces;
@@ -1,12 +1,12 @@
1
1
  'use strict';
2
2
 
3
- var helpers_declarationsToBlock = require('../helpers/declarationsToBlock.cjs');
3
+ var declarationsToBlock = require('../helpers/declarationsToBlock.cjs');
4
4
 
5
5
  // https://github.com/styletron/styletron/blob/master/packages/styletron-engine-atomic/src/css.ts#L48
6
6
  function keyframesToBlock(keyframes) {
7
7
  let result = "";
8
8
  for(const animationState in keyframes){
9
- result += `${animationState}{${helpers_declarationsToBlock.declarationsToBlock(keyframes[animationState])}}`;
9
+ result += `${animationState}{${declarationsToBlock.declarationsToBlock(keyframes[animationState])}}`;
10
10
  }
11
11
  return result;
12
12
  }
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var helpers_getPropertyPriority = require('../helpers/getPropertyPriority.cjs');
3
+ var getPropertyPriority = require('../helpers/getPropertyPriority.cjs');
4
4
 
5
5
  function printStyleBlocks(blocks) {
6
6
  let stylesheet = '';
@@ -40,7 +40,7 @@ function printStyleBlocks(blocks) {
40
40
  stylesheet += `@container ${style.container}{`;
41
41
  }
42
42
  if (style.type === 'rule') {
43
- const className = `.${style.id}`.repeat(helpers_getPropertyPriority.getPropertyPriority(style.property));
43
+ const className = `.${style.id}`.repeat(getPropertyPriority.getPropertyPriority(style.property));
44
44
  stylesheet += `${className}${style.pseudo}{`;
45
45
  } else if (style.type === 'static' && (previousStyle?.selector !== style.selector || previousStyle?.pseudo !== style.pseudo || previousStyle?.media !== style.media || previousStyle?.support !== style.support || previousStyle?.container !== style.container)) {
46
46
  // If static, we don't add pseudo selectors currently
@@ -1,15 +1,15 @@
1
1
  'use strict';
2
2
 
3
- var helpers_isObject = require('./helpers/isObject.cjs');
4
- var helpers_transformContentProperty = require('./helpers/transformContentProperty.cjs');
3
+ var isObject = require('./helpers/isObject.cjs');
4
+ var transformContentProperty = require('./helpers/transformContentProperty.cjs');
5
5
 
6
6
  const transformValuePropertyMap = {
7
- content: helpers_transformContentProperty.transformContentProperty
7
+ content: transformContentProperty.transformContentProperty
8
8
  };
9
9
  function processKeyframes(keyframes) {
10
10
  const newKeyframes = {};
11
11
  for (const [key, value] of Object.entries(keyframes)){
12
- if (helpers_isObject.isObject(value)) {
12
+ if (isObject.isObject(value)) {
13
13
  newKeyframes[key] = processKeyframes(value);
14
14
  continue;
15
15
  }
package/processStyles.cjs CHANGED
@@ -1,28 +1,28 @@
1
1
  'use strict';
2
2
 
3
- var helpers_generateCombinedAtRules = require('./helpers/generateCombinedAtRules.cjs');
4
- var helpers_hyphenateProperty = require('./helpers/hyphenateProperty.cjs');
5
- var helpers_isContainerQuery = require('./helpers/isContainerQuery.cjs');
6
- var helpers_isMediaQuery = require('./helpers/isMediaQuery.cjs');
7
- var helpers_isNestedSelector = require('./helpers/isNestedSelector.cjs');
8
- var helpers_isObject = require('./helpers/isObject.cjs');
9
- var helpers_isSupportsQuery = require('./helpers/isSupportsQuery.cjs');
10
- var helpers_normalizeCSSVarsProperty = require('./helpers/normalizeCSSVarsProperty.cjs');
11
- var helpers_normalizeCSSVarsValue = require('./helpers/normalizeCSSVarsValue.cjs');
12
- var helpers_normalizeNestedProperty = require('./helpers/normalizeNestedProperty.cjs');
13
- var helpers_pixelifyProperties = require('./helpers/pixelifyProperties.cjs');
14
- var helpers_transformContentProperty = require('./helpers/transformContentProperty.cjs');
3
+ var generateCombinedAtRules = require('./helpers/generateCombinedAtRules.cjs');
4
+ var hyphenateProperty = require('./helpers/hyphenateProperty.cjs');
5
+ var isContainerQuery = require('./helpers/isContainerQuery.cjs');
6
+ var isMediaQuery = require('./helpers/isMediaQuery.cjs');
7
+ var isNestedSelector = require('./helpers/isNestedSelector.cjs');
8
+ var isObject = require('./helpers/isObject.cjs');
9
+ var isSupportsQuery = require('./helpers/isSupportsQuery.cjs');
10
+ var normalizeCSSVarsProperty = require('./helpers/normalizeCSSVarsProperty.cjs');
11
+ var normalizeCSSVarsValue = require('./helpers/normalizeCSSVarsValue.cjs');
12
+ var normalizeNestedProperty = require('./helpers/normalizeNestedProperty.cjs');
13
+ var pixelifyProperties = require('./helpers/pixelifyProperties.cjs');
14
+ var transformContentProperty = require('./helpers/transformContentProperty.cjs');
15
15
 
16
16
  const transformValuePropertyMap = {
17
- content: helpers_transformContentProperty.transformContentProperty
17
+ content: transformContentProperty.transformContentProperty
18
18
  };
19
19
  function processStyles({ cache , type }) {
20
20
  return function process({ styles , pseudo ="" , media ="" , support ="" , container ="" , selector ="" }) {
21
21
  const result = [];
22
22
  for (const [property, value] of Object.entries(styles)){
23
- if (helpers_isObject.isObject(value)) {
24
- if (helpers_isMediaQuery.isMediaQuery(property)) {
25
- const combinedMedia = helpers_generateCombinedAtRules.generateCombinedAtRules(media, property.slice(6).trim());
23
+ if (isObject.isObject(value)) {
24
+ if (isMediaQuery.isMediaQuery(property)) {
25
+ const combinedMedia = generateCombinedAtRules.generateCombinedAtRules(media, property.slice(6).trim());
26
26
  result.push(...process({
27
27
  styles: value,
28
28
  pseudo,
@@ -33,8 +33,8 @@ function processStyles({ cache , type }) {
33
33
  }));
34
34
  continue;
35
35
  }
36
- if (helpers_isSupportsQuery.isSupportsQuery(property)) {
37
- const combinedSupport = helpers_generateCombinedAtRules.generateCombinedAtRules(support, property.slice(9).trim());
36
+ if (isSupportsQuery.isSupportsQuery(property)) {
37
+ const combinedSupport = generateCombinedAtRules.generateCombinedAtRules(support, property.slice(9).trim());
38
38
  result.push(...process({
39
39
  styles: value,
40
40
  pseudo,
@@ -45,8 +45,8 @@ function processStyles({ cache , type }) {
45
45
  }));
46
46
  continue;
47
47
  }
48
- if (helpers_isContainerQuery.isContainerQuery(property)) {
49
- const combinedContainer = helpers_generateCombinedAtRules.generateCombinedAtRules(container, property.slice(10).trim());
48
+ if (isContainerQuery.isContainerQuery(property)) {
49
+ const combinedContainer = generateCombinedAtRules.generateCombinedAtRules(container, property.slice(10).trim());
50
50
  result.push(...process({
51
51
  styles: value,
52
52
  pseudo,
@@ -57,13 +57,13 @@ function processStyles({ cache , type }) {
57
57
  }));
58
58
  continue;
59
59
  }
60
- if (helpers_isNestedSelector.isNestedSelector(property)) {
60
+ if (isNestedSelector.isNestedSelector(property)) {
61
61
  // This is only allowed in simple pseudos currently.
62
62
  const copies = property.split(',').map((p)=>p.trim());
63
63
  for (const copy of copies){
64
64
  result.push(...process({
65
65
  styles: value,
66
- pseudo: pseudo + helpers_normalizeNestedProperty.normalizeNestedProperty(copy),
66
+ pseudo: pseudo + normalizeNestedProperty.normalizeNestedProperty(copy),
67
67
  media,
68
68
  support,
69
69
  container,
@@ -75,19 +75,19 @@ function processStyles({ cache , type }) {
75
75
  console.warn("Unknown property", property);
76
76
  continue;
77
77
  }
78
- let newProperty = helpers_normalizeCSSVarsProperty.normalizeCSSVarsProperty(property);
78
+ let newProperty = normalizeCSSVarsProperty.normalizeCSSVarsProperty(property);
79
79
  let newValue = value;
80
80
  if (typeof value === "string") {
81
81
  newValue = value.trim().replace(/;[\n\s]*$/, "");
82
- newValue = helpers_normalizeCSSVarsValue.normalizeCSSVarsValue(newValue);
82
+ newValue = normalizeCSSVarsValue.normalizeCSSVarsValue(newValue);
83
83
  }
84
84
  if (typeof value === "number") {
85
- newValue = helpers_pixelifyProperties.pixelifyProperties(newProperty, value);
85
+ newValue = pixelifyProperties.pixelifyProperties(newProperty, value);
86
86
  }
87
87
  if (transformValuePropertyMap[newProperty]) {
88
88
  newValue = transformValuePropertyMap[newProperty](value);
89
89
  }
90
- newProperty = helpers_hyphenateProperty.hyphenateProperty(newProperty);
90
+ newProperty = hyphenateProperty.hyphenateProperty(newProperty);
91
91
  // Remove trailing semicolon and new lines with regex
92
92
  result.push(cache.getOrStore({
93
93
  type,