catom 1.2.0 → 1.2.7

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.
package/README.md CHANGED
@@ -136,7 +136,7 @@ and then inject it using a template parameter.
136
136
  </head>
137
137
  ```
138
138
 
139
- it also allows you to use postCSS plugins by importing the `transformCSS` and/or `autoPrefixCSS` functions
139
+ uses parcel css under the hood
140
140
 
141
141
  # 0 Runtime
142
142
 
@@ -1,2 +1,4 @@
1
- import type { PluginItem } from "@babel/core";
2
- export default function catomBabelPlugin(): PluginItem;
1
+ import { Visitor } from "@babel/traverse";
2
+ export default function catomBabelPlugin(): {
3
+ visitor: Visitor;
4
+ };
@@ -19,51 +19,23 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- const path_1 = require("path");
23
- const promises_1 = require("fs/promises");
24
22
  const template = __importStar(require("@babel/template"));
25
- const css_1 = require("./css");
26
23
  const astObject_1 = require("./plugin/astObject");
27
- function handleExpression(path, expression, removeName, mapObj) {
24
+ function handleExpression(path, expression, removeName) {
28
25
  if (!expression)
29
26
  return;
30
27
  if (expression.type === "CallExpression") {
31
28
  return commonInject(path, expression, {
32
29
  name: removeName,
33
- mapObj,
34
30
  });
35
31
  }
36
32
  }
37
33
  function catomBabelPlugin() {
38
34
  let removeName = "css";
39
- const cssPropertyMap = new Map();
40
- const mediaQueryMap = new Map();
41
- const pseudoSelectorMap = new Map();
42
- const mapObj = {
43
- cssPropertyMap: cssPropertyMap,
44
- mediaQueryMap: mediaQueryMap,
45
- pseudoSelectorMap: pseudoSelectorMap,
46
- };
47
- let last;
48
35
  return {
49
- post() {
50
- const css = (0, css_1.emitCSS)(mapObj);
51
- if (css == last)
52
- return;
53
- if (!this.__emitFile)
54
- return;
55
- last = css;
56
- const f = (0, path_1.join)("./", this.__emitFile);
57
- (0, promises_1.writeFile)(f, css);
58
- },
59
36
  visitor: {
60
- Program(_, state) {
61
- if (!state?.opts.emitFile)
62
- throw new Error("No emit file provided!");
63
- this.__emitFile = this.__emitFile || state.opts.emitFile;
64
- },
65
37
  CallExpression(path) {
66
- return handleExpression(path, path.node, removeName, mapObj);
38
+ return handleExpression(path, path.node, removeName);
67
39
  },
68
40
  },
69
41
  };
@@ -74,14 +46,14 @@ function commonInject(path, right, options) {
74
46
  if (callee && callee.type === "Identifier") {
75
47
  if (callee.name === options.name) {
76
48
  const arg0 = right.arguments[0];
77
- return injectDependency(path, arg0, options.mapObj);
49
+ return injectDependency(path, arg0);
78
50
  }
79
51
  }
80
52
  }
81
- function injectDependency(path, arg0, mapObj) {
53
+ function injectDependency(path, arg0) {
82
54
  if (arg0.type === "ObjectExpression") {
83
55
  let retArray = [];
84
- (0, astObject_1.parseObjectExpression)(arg0, retArray, null, mapObj);
56
+ (0, astObject_1.parseObjectExpression)(arg0, retArray);
85
57
  path.replaceWith(template.statement.ast(JSON.stringify(retArray.join(" "))));
86
58
  }
87
59
  }
package/dist/css.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { MapObj } from "./plugin/constants";
1
+ /// <reference types="node" />
2
2
  import { emitCSS } from "./plugin/cssTransform";
3
- export default function transformCSS(maps: MapObj, ...plugins: any): Promise<any>;
4
- export declare function autoPrefixCSS(maps: MapObj, ...plugins: any): Promise<any>;
3
+ export default function transformCSS(css?: string, ...plugins: any): Promise<Buffer>;
4
+ export declare function autoPrefixCSS(css?: string, ...plugins: any): Promise<Buffer>;
5
5
  export { emitCSS };
package/dist/css.js CHANGED
@@ -1,19 +1,33 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.emitCSS = exports.autoPrefixCSS = void 0;
4
- const postCSS = require("postcss");
4
+ const css_1 = require("@parcel/css");
5
5
  const cssTransform_1 = require("./plugin/cssTransform");
6
6
  Object.defineProperty(exports, "emitCSS", { enumerable: true, get: function () { return cssTransform_1.emitCSS; } });
7
- async function transformCSS(maps, ...plugins) {
8
- const css = (0, cssTransform_1.emitCSS)(maps);
9
- const result = await postCSS(...plugins).process(css);
10
- result.warnings().forEach((warn) => {
11
- console.warn(warn.toString());
7
+ async function transformCSS(css, ...plugins) {
8
+ css = css || (0, cssTransform_1.emitCSS)();
9
+ const { code } = (0, css_1.transform)({
10
+ code: Buffer.from(css),
11
+ filename: "style.css",
12
+ sourceMap: false,
13
+ // browserslist hardcoded
14
+ // probably change or manually audit
15
+ targets: {
16
+ android: 6422528,
17
+ chrome: 6488064,
18
+ edge: 6488064,
19
+ firefox: 6356992,
20
+ ie: 720896,
21
+ ios_saf: 983552,
22
+ opera: 5439488,
23
+ safari: 983552,
24
+ samsung: 1048576,
25
+ },
12
26
  });
13
- return result.css;
27
+ return code;
14
28
  }
15
29
  exports.default = transformCSS;
16
- function autoPrefixCSS(maps, ...plugins) {
17
- return transformCSS(maps, ...plugins.concat(require("autoprefixer")));
30
+ function autoPrefixCSS(css, ...plugins) {
31
+ return transformCSS(css);
18
32
  }
19
33
  exports.autoPrefixCSS = autoPrefixCSS;
@@ -1,3 +1,3 @@
1
+ import { Hashable } from "./constants";
1
2
  import * as babel from "@babel/types";
2
- import { Hashable, MapObj } from "./constants";
3
- export declare function parseObjectExpression(object: babel.ObjectExpression, retArray: Array<string>, hashable: Hashable, mapObj: MapObj): any;
3
+ export declare function parseObjectExpression(object: babel.ObjectExpression, retArray: Array<string>, hashable?: Hashable): any;
@@ -2,27 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseObjectExpression = void 0;
4
4
  const cssTransform_1 = require("./cssTransform");
5
- function parseObjectExpression(object, retArray, hashable, mapObj) {
6
- return object.properties.forEach((p) => handleProperties(p, retArray, hashable, mapObj));
5
+ function parseObjectExpression(object, retArray, hashable) {
6
+ return object.properties.forEach((p) => handleProperties(p, retArray, hashable));
7
7
  }
8
8
  exports.parseObjectExpression = parseObjectExpression;
9
- // function handleSpread(
10
- // spread: babel.SpreadElement,
11
- // retArray: Array<string>,
12
- // hashable: Hashable,
13
- // mapObj: MapObj
14
- // ) {
15
- // const argument = spread.argument;
16
- // if (argument.type === "ObjectExpression") {
17
- // return parseObjectExpression(argument, retArray, hashable, mapObj);
18
- // }
19
- // throw Error(
20
- // `Cannot parse ${spread.type}. Catom compiler only accepts compile time constant values`
21
- // );
22
- // }
23
- function handleProperties(propertyOrSpread, retArray, hashable, mapObj) {
9
+ function handleSpread(spread, retArray, hashable) {
10
+ const argument = spread.argument;
11
+ if (argument.type === "ObjectExpression") {
12
+ return parseObjectExpression(argument, retArray, hashable);
13
+ }
14
+ throw Error(`Cannot parse ${spread.type}. Catom compiler only accepts compile time constant values`);
15
+ }
16
+ function handleProperties(propertyOrSpread, retArray, hashable) {
24
17
  if (propertyOrSpread.type === "SpreadElement") {
25
- return throwErr("Catom does not except spread elements");
18
+ return handleSpread(propertyOrSpread, retArray, hashable);
26
19
  }
27
20
  let { key, value } = propertyOrSpread;
28
21
  if (value.type === "TSAsExpression")
@@ -43,14 +36,14 @@ function handleProperties(propertyOrSpread, retArray, hashable, mapObj) {
43
36
  if (value.type === "StringLiteral" || value.type === "NumericLiteral") {
44
37
  if (canAcceptObjectLiteralInValue)
45
38
  throwErr("Need an object literal for media query or pseudo selector");
46
- return retArray.push((0, cssTransform_1.createValueHash)(keyName, value.value, hashable, mapObj));
39
+ return retArray.push((0, cssTransform_1.createValueHash)(keyName, value.value, hashable));
47
40
  }
48
41
  if (canAcceptObjectLiteralInValue && value.type === "ObjectExpression") {
49
- return value.properties.forEach((prop) => handleMediaOrPseudoProperties(prop, retArray, isMedia, isPseudo, mapObj));
42
+ return value.properties.forEach((prop) => handleMediaOrPseudoProperties(prop, retArray, isMedia, isPseudo));
50
43
  }
51
44
  throwErr();
52
45
  }
53
- function handleMediaOrPseudoProperties(property, retArray, isMedia, isPseudo, mapObj) {
46
+ function handleMediaOrPseudoProperties(property, retArray, isMedia, isPseudo) {
54
47
  if (property.type === "ObjectProperty") {
55
48
  const { key, value } = property;
56
49
  let keyName;
@@ -59,7 +52,7 @@ function handleMediaOrPseudoProperties(property, retArray, isMedia, isPseudo, ma
59
52
  else if (key.type === "Identifier")
60
53
  keyName = key.name;
61
54
  if (value.type === "ObjectExpression") {
62
- return parseObjectExpression(value, retArray, (isMedia && { media: keyName }) || (isPseudo && { pseudo: keyName }), mapObj);
55
+ return parseObjectExpression(value, retArray, (isMedia && { media: keyName }) || (isPseudo && { pseudo: keyName }));
63
56
  }
64
57
  }
65
58
  throwErr();
@@ -3,11 +3,6 @@ export declare type PropMap = Map<string, CSSProps>;
3
3
  export interface AtomicCSSOptions {
4
4
  transpileFunctionName: "css";
5
5
  }
6
- export interface MapObj {
7
- mediaQueryMap: Map<string, PropMap>;
8
- pseudoSelectorMap: Map<string, PropMap>;
9
- cssPropertyMap: PropMap;
10
- }
11
6
  export interface CSSProps {
12
7
  class: string;
13
8
  cssRule: string;
@@ -1,3 +1,3 @@
1
- import { Hashable, MapObj } from "./constants";
2
- export declare function createValueHash(key: string, unparsed: string | number, hashable: Hashable, { mediaQueryMap, pseudoSelectorMap, cssPropertyMap }: MapObj): string;
3
- export declare function emitCSS({ mediaQueryMap, pseudoSelectorMap, cssPropertyMap, }: MapObj): string;
1
+ import { Hashable } from "./constants";
2
+ export declare function createValueHash(key: string, unparsed: string | number, hashable?: Hashable): string;
3
+ export declare function emitCSS(): string;
@@ -3,6 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.emitCSS = exports.createValueHash = void 0;
4
4
  const constants_1 = require("./constants");
5
5
  const hash_1 = require("./hash");
6
+ const CSS_PROPERTY_MAP = new Map();
7
+ const MEDIA_QUERY_MAP = new Map();
8
+ const PSEUDO_SELECTOR_MAP = new Map();
9
+ // function clearMaps(): void {
10
+ // CSS_PROPERTY_MAP.clear();
11
+ // MEDIA_QUERY_MAP.clear();
12
+ // PSEUDO_SELECTOR_MAP.clear();
13
+ // }
6
14
  function toCSSProp(prop) {
7
15
  return prop.replace(constants_1.KEBAB_CASE_REGEXP, "$1-$2").toLowerCase();
8
16
  }
@@ -12,21 +20,18 @@ function makeCSSCompat(str) {
12
20
  return str;
13
21
  }
14
22
  const sanitizeRegExp = /([^\w]|_)/g;
15
- function createValueHash(key, unparsed, hashable, { mediaQueryMap, pseudoSelectorMap, cssPropertyMap }) {
23
+ function createValueHash(key, unparsed, hashable) {
16
24
  key = key.trim();
17
25
  const value = String(unparsed).trim();
18
- const isMediaQuery = hashable && hashable.media;
26
+ const MEDIA_QUERY = hashable && hashable.media;
19
27
  const PSEUDO_SELECTOR = hashable && hashable.pseudo;
20
- const isSpec = isMediaQuery || PSEUDO_SELECTOR;
28
+ const isSpec = MEDIA_QUERY || PSEUDO_SELECTOR;
21
29
  // example: const rawCSSRule = "margin:auto;"
22
- let prefix = "";
23
30
  const rawCSSRule = `${toCSSProp(key)}:${value};`;
24
- if (process.env.NODE_ENV !== "production")
25
- prefix = `${rawCSSRule.replace(sanitizeRegExp, "_")}____`;
26
31
  // a unique rule will be one with a different media/pseudo rule + key&value
27
- const identity = ((hashable && (isMediaQuery || PSEUDO_SELECTOR)) || "").trim() + rawCSSRule;
32
+ const identity = ((hashable && (MEDIA_QUERY || PSEUDO_SELECTOR)) || "").trim() + rawCSSRule;
28
33
  let cache;
29
- const $map = isSpec && isMediaQuery ? mediaQueryMap : pseudoSelectorMap;
34
+ const $map = isSpec && MEDIA_QUERY ? MEDIA_QUERY_MAP : PSEUDO_SELECTOR_MAP;
30
35
  let fetchMap;
31
36
  if ($map) {
32
37
  fetchMap = $map.get(isSpec);
@@ -39,17 +44,17 @@ function createValueHash(key, unparsed, hashable, { mediaQueryMap, pseudoSelecto
39
44
  cache = fetchMap.get(identity);
40
45
  }
41
46
  else {
42
- cache = cssPropertyMap.get(identity);
47
+ cache = CSS_PROPERTY_MAP.get(identity);
43
48
  }
44
49
  if (cache)
45
50
  return cache.class;
46
- const hash = prefix + makeCSSCompat((0, hash_1.murmur2)(identity));
51
+ const hash = makeCSSCompat((0, hash_1.murmur2)(identity));
47
52
  const obj = { class: hash, cssRule: rawCSSRule };
48
53
  if (isSpec) {
49
54
  fetchMap.set(identity, obj);
50
55
  }
51
56
  else {
52
- cssPropertyMap.set(identity, obj);
57
+ CSS_PROPERTY_MAP.set(identity, obj);
53
58
  }
54
59
  return hash;
55
60
  }
@@ -68,13 +73,13 @@ function mergeDuplicateRules(value, dedupedPropMap, suffix = "") {
68
73
  }
69
74
  arr.add(cls);
70
75
  }
71
- function emitCSS({ mediaQueryMap, pseudoSelectorMap, cssPropertyMap, }) {
76
+ function emitCSS() {
72
77
  const dedupedPropMap = new Map();
73
- Array.from(cssPropertyMap.values()).forEach((v) => mergeDuplicateRules(v, dedupedPropMap));
74
- Array.from(pseudoSelectorMap.entries()).forEach(([k, v]) => Array.from(v.values())
78
+ Array.from(CSS_PROPERTY_MAP.values()).forEach((v) => mergeDuplicateRules(v, dedupedPropMap));
79
+ Array.from(PSEUDO_SELECTOR_MAP.entries()).forEach(([k, v]) => Array.from(v.values())
75
80
  .sort()
76
81
  .forEach((prop) => mergeDuplicateRules(prop, dedupedPropMap, k)));
77
- const mediaQueries = Array.from(mediaQueryMap.entries())
82
+ const mediaQueries = Array.from(MEDIA_QUERY_MAP.entries())
78
83
  .map(([k, v]) => `@media ${k} {\n${toCSS(v)}\n}\n`)
79
84
  .sort()
80
85
  .join("\n");
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "catom",
3
- "version": "1.2.0",
3
+ "version": "1.2.7",
4
4
  "description": "0 runtime CSS in JS tool",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
+ "prepublishOnly": "npm run build",
7
8
  "prebuild": "rm -rf ./dist",
8
9
  "build": "tsc --build tsconfig.json"
9
10
  },
@@ -28,8 +29,7 @@
28
29
  "typescript": "^4.4.4"
29
30
  },
30
31
  "peerDependencies": {
31
- "autoprefixer": "^10.3.7",
32
- "postcss": "^8.3.9"
32
+ "@parcel/css": "^1.5.0"
33
33
  },
34
34
  "dependencies": {
35
35
  "@babel/template": "^7.15.4",