style-zx 0.0.5 → 0.0.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/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
+ export * from './style-zx/index'
1
2
  export {}
package/dist/plugin.cjs CHANGED
@@ -1,6 +1,6 @@
1
- "use strict";const v=require("@babel/parser"),m=require("@babel/traverse"),S=require("@babel/types"),$=require("magic-string"),b=require("crypto");function w(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const r in e)if(r!=="default"){const s=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,s.get?s:{enumerable:!0,get:()=>e[r]})}}return t.default=e,Object.freeze(t)}const i=w(S),E={m:["margin"],mt:["marginTop"],mr:["marginRight"],mb:["marginBottom"],ml:["marginLeft"],mx:["marginLeft","marginRight"],my:["marginTop","marginBottom"],p:["padding"],pt:["paddingTop"],pr:["paddingRight"],pb:["paddingBottom"],pl:["paddingLeft"],px:["paddingLeft","paddingRight"],py:["paddingTop","paddingBottom"],bg:["backgroundColor"]},L=new Set(["opacity","fontWeight","lineHeight","zIndex","flex","flexGrow","flexShrink","order"]);function z(e){return e.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`)}function j(e,t){return typeof t=="number"&&!L.has(e)?`${t}px`:typeof t=="string"&&t.startsWith("$theme.")?`var(${t.replace("$theme.","--theme-").replace(/\./g,"-")})`:String(t)}function x(e,t){let r=`.${t} {`,s="";for(const o in e){const c=e[o];if(o.startsWith("&")||o.startsWith(":")||o.startsWith("@")){const l=o.replace(/&/g,`.${t}`),n=x(c,"TEMP_CLASS").replace(".TEMP_CLASS",l);s+=`
2
- ${n}`;continue}(E[o]||[o]).forEach(l=>{const n=z(l),u=j(l,c);r+=`
3
- ${n}: ${u};`})}return r+=`
4
- }`,r+s}function O(e){return b.createHash("md5").update(e).digest("hex").slice(0,8)}function y(e){if(i.isStringLiteral(e)||i.isNumericLiteral(e)||i.isBooleanLiteral(e))return e.value;if(i.isNullLiteral(e))return null;if(i.isUnaryExpression(e)&&e.operator==="-"&&i.isNumericLiteral(e.argument))return-e.argument.value;if(i.isObjectExpression(e)){const t={};return e.properties.forEach(r=>{if(i.isObjectProperty(r)){const s=i.isIdentifier(r.key)?r.key.name:i.isStringLiteral(r.key)?r.key.value:null;if(!s)throw new Error(`Unsupported key type in zx prop: ${r.key.type}`);t[s]=y(r.value)}else throw new Error(`Unsupported property type in zx prop: ${r.type}. Only static object properties are allowed.`)}),t}throw new Error(`Dynamic expressions are not allowed in zx prop. Found: ${e.type}. Use 'style' prop for dynamic values.`)}function d(e,t){if(!e.includes("zx={"))return null;const r=v.parse(e,{sourceType:"module",plugins:["typescript","jsx"]}),s=new $(e);let o=!1,c="";return(m.default||m)(r,{JSXOpeningElement(l){const n=l.node.attributes.find(u=>i.isJSXAttribute(u)&&u.name.name==="zx");if(n&&(o=!0,i.isJSXExpressionContainer(n.value)&&i.isObjectExpression(n.value.expression))){let u={};try{u=y(n.value.expression)}catch(p){throw new Error(`Error parsing zx prop in ${t}: ${p.message}`)}const g=`zx-${O(JSON.stringify(u))}`,h=x(u,g);c+=h+`
5
- `;const a=l.node.attributes.find(p=>i.isJSXAttribute(p)&&p.name.name==="className");if(n.start!=null&&n.end!=null&&s.remove(n.start,n.end),a){if(i.isStringLiteral(a.value)&&a.value.start&&a.value.end)s.overwrite(a.value.start,a.value.end,`"${a.value.value} ${g}"`);else if(i.isJSXExpressionContainer(a.value)&&a.value.expression.start&&a.value.expression.end){const p=e.slice(a.value.expression.start,a.value.expression.end);s.overwrite(a.value.expression.start,a.value.expression.end,`\`\${${p}} ${g}\``)}}else s.appendLeft(n.start,` className="${g}" `)}}}),o?(s.prepend(`import '${t}.style-zx.css';
6
- `),{code:s.toString(),map:s.generateMap({hires:!0}),css:c,hasZx:!0}):null}function k(){const e=new Map;return{name:"vite-plugin-style-zx",enforce:"pre",resolveId(t){return t.endsWith(".style-zx.css")?t:null},load(t){if(t.endsWith(".style-zx.css")){const r=t.replace(".style-zx.css","");return e.get(r)||""}return null},transform(t,r){if(!/\.(t|j)sx?$/.test(r))return null;const s=d(t,r);return s&&s.hasZx?(e.set(r,s.css),{code:s.code,map:s.map}):null},async handleHotUpdate({file:t,modules:r,read:s,server:o}){if(!/\.(t|j)sx?$/.test(t))return;const c=await s(),f=d(c,t);if(f&&f.hasZx){e.set(t,f.css);const l=`${t}.style-zx.css`,n=o.moduleGraph.getModuleById(l);if(n)return[...r,n]}}}}module.exports=k;
1
+ "use strict";const $=require("@babel/parser"),x=require("@babel/traverse"),b=require("@babel/types"),w=require("magic-string"),E=require("crypto");function L(e){const r=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const t in e)if(t!=="default"){const s=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,s.get?s:{enumerable:!0,get:()=>e[t]})}}return r.default=e,Object.freeze(r)}const a=L(b),j={m:["margin"],mt:["marginTop"],mr:["marginRight"],mb:["marginBottom"],ml:["marginLeft"],mx:["marginLeft","marginRight"],my:["marginTop","marginBottom"],p:["padding"],pt:["paddingTop"],pr:["paddingRight"],pb:["paddingBottom"],pl:["paddingLeft"],px:["paddingLeft","paddingRight"],py:["paddingTop","paddingBottom"],bg:["backgroundColor"]},z=new Set(["opacity","fontWeight","lineHeight","zIndex","flex","flexGrow","flexShrink","order"]);function O(e){return e.replace(/[A-Z]/g,r=>`-${r.toLowerCase()}`)}function M(e,r){return typeof r=="number"&&!z.has(e)?`${r}px`:typeof r=="string"&&r.startsWith("$theme.")?`var(${r.replace("$theme.","--theme-").replace(/\./g,"-")})`:String(r)}function v(e,r){let t=`.${r} {`,s="";for(const n in e){const o=e[n];if(n.startsWith("&")||n.startsWith(":")||n.startsWith("@")){const u=n.replace(/&/g,`.${r}`),l=v(o,"TEMP_CLASS").replace(".TEMP_CLASS",u);s+=`
2
+ ${l}`;continue}(j[n]||[n]).forEach(u=>{const l=O(u),c=M(u,o);t+=`
3
+ ${l}: ${c};`})}return t+=`
4
+ }`,t+s}function k(e){return E.createHash("md5").update(e).digest("hex").slice(0,8)}function S(e){if(a.isStringLiteral(e)||a.isNumericLiteral(e)||a.isBooleanLiteral(e))return e.value;if(a.isNullLiteral(e))return null;if(a.isUnaryExpression(e)&&e.operator==="-"&&a.isNumericLiteral(e.argument))return-e.argument.value;if(a.isObjectExpression(e)){const r={};return e.properties.forEach(t=>{if(a.isObjectProperty(t)){const s=a.isIdentifier(t.key)?t.key.name:a.isStringLiteral(t.key)?t.key.value:null;if(!s)throw new Error(`Unsupported key type in zx prop: ${t.key.type}`);r[s]=S(t.value)}else throw new Error(`Unsupported property type in zx prop: ${t.type}. Only static object properties are allowed.`)}),r}throw new Error(`Dynamic expressions are not allowed in zx prop. Found: ${e.type}. Use 'style' prop for dynamic values.`)}const y=new Map,d=new Map;function h(e,r){if(!e.includes("zx={"))return d.has(r)?(d.delete(r),{code:e,map:null,hasZx:!1}):null;const t=$.parse(e,{sourceType:"module",plugins:["typescript","jsx"]}),s=new w(e);let n=!1;const o=new Set;return(x.default||x)(t,{JSXOpeningElement(u){const l=u.node.attributes.find(c=>a.isJSXAttribute(c)&&c.name.name==="zx");if(l&&(n=!0,a.isJSXExpressionContainer(l.value)&&a.isObjectExpression(l.value.expression))){let c={};try{c=S(l.value.expression)}catch(p){throw new Error(`Error parsing zx prop in ${r}: ${p.message}`)}const m=k(JSON.stringify(c)),g=`zx-${m}`;if(!y.has(m)){const p=v(c,g);y.set(m,p)}o.add(m);const i=u.node.attributes.find(p=>a.isJSXAttribute(p)&&p.name.name==="className");if(l.start!=null&&l.end!=null&&s.remove(l.start,l.end),i){if(a.isStringLiteral(i.value)&&i.value.start&&i.value.end)s.overwrite(i.value.start,i.value.end,`"${i.value.value} ${g}"`);else if(a.isJSXExpressionContainer(i.value)&&i.value.expression.start&&i.value.expression.end){const p=e.slice(i.value.expression.start,i.value.expression.end);s.overwrite(i.value.expression.start,i.value.expression.end,`\`\${${p}} ${g}\``)}}else s.appendLeft(l.start,` className="${g}" `)}}}),n?(d.set(r,o),s.prepend(`import 'virtual:style-zx.css';
5
+ `),{code:s.toString(),map:s.generateMap({hires:!0}),hasZx:!0}):(d.delete(r),null)}function N(){const e="virtual:style-zx.css",r="\0"+e;return{name:"vite-plugin-style-zx",enforce:"pre",resolveId(t){return t===e?r:null},load(t){if(t===r){const s=new Set;for(const o of d.values())o.forEach(f=>s.add(f));let n="";return s.forEach(o=>{y.has(o)&&(n+=y.get(o)+`
6
+ `)}),n}return null},transform(t,s){if(!/\.(t|j)sx?$/.test(s))return null;const n=h(t,s);return n&&n.hasZx?{code:n.code,map:n.map}:null},async handleHotUpdate({file:t,modules:s,read:n,server:o}){if(!/\.(t|j)sx?$/.test(t))return;const f=await n();h(f,t);const u=o.moduleGraph.getModuleById(r);if(u)return o.moduleGraph.invalidateModule(u),[...s,u]}}}module.exports=N;
package/dist/plugin.d.ts CHANGED
@@ -1 +1,4 @@
1
1
  export {}
2
+ import _default from './vite-plugin-style-zx/index'
3
+ export default _default
4
+ export {}
package/dist/plugin.js CHANGED
@@ -1,9 +1,9 @@
1
- import { parse as v } from "@babel/parser";
2
- import g from "@babel/traverse";
3
- import * as i from "@babel/types";
4
- import $ from "magic-string";
5
- import S from "crypto";
6
- const w = {
1
+ import { parse as $ } from "@babel/parser";
2
+ import h from "@babel/traverse";
3
+ import * as a from "@babel/types";
4
+ import w from "magic-string";
5
+ import E from "crypto";
6
+ const b = {
7
7
  m: ["margin"],
8
8
  mt: ["marginTop"],
9
9
  mr: ["marginRight"],
@@ -19,7 +19,7 @@ const w = {
19
19
  px: ["paddingLeft", "paddingRight"],
20
20
  py: ["paddingTop", "paddingBottom"],
21
21
  bg: ["backgroundColor"]
22
- }, E = /* @__PURE__ */ new Set([
22
+ }, L = /* @__PURE__ */ new Set([
23
23
  "opacity",
24
24
  "fontWeight",
25
25
  "lineHeight",
@@ -29,135 +29,143 @@ const w = {
29
29
  "flexShrink",
30
30
  "order"
31
31
  ]);
32
- function b(e) {
32
+ function z(e) {
33
33
  return e.replace(/[A-Z]/g, (t) => `-${t.toLowerCase()}`);
34
34
  }
35
- function L(e, t) {
36
- return typeof t == "number" && !E.has(e) ? `${t}px` : typeof t == "string" && t.startsWith("$theme.") ? `var(${t.replace("$theme.", "--theme-").replace(/\./g, "-")})` : String(t);
35
+ function M(e, t) {
36
+ return typeof t == "number" && !L.has(e) ? `${t}px` : typeof t == "string" && t.startsWith("$theme.") ? `var(${t.replace("$theme.", "--theme-").replace(/\./g, "-")})` : String(t);
37
37
  }
38
- function x(e, t) {
38
+ function v(e, t) {
39
39
  let r = `.${t} {`, s = "";
40
- for (const o in e) {
41
- const p = e[o];
42
- if (o.startsWith("&") || o.startsWith(":") || o.startsWith("@")) {
43
- const l = o.replace(/&/g, `.${t}`), n = x(p, "TEMP_CLASS").replace(".TEMP_CLASS", l);
40
+ for (const n in e) {
41
+ const o = e[n];
42
+ if (n.startsWith("&") || n.startsWith(":") || n.startsWith("@")) {
43
+ const u = n.replace(/&/g, `.${t}`), l = v(o, "TEMP_CLASS").replace(".TEMP_CLASS", u);
44
44
  s += `
45
- ${n}`;
45
+ ${l}`;
46
46
  continue;
47
47
  }
48
- (w[o] || [o]).forEach((l) => {
49
- const n = b(l), u = L(l, p);
48
+ (b[n] || [n]).forEach((u) => {
49
+ const l = z(u), p = M(u, o);
50
50
  r += `
51
- ${n}: ${u};`;
51
+ ${l}: ${p};`;
52
52
  });
53
53
  }
54
54
  return r += `
55
55
  }`, r + s;
56
56
  }
57
- function z(e) {
58
- return S.createHash("md5").update(e).digest("hex").slice(0, 8);
57
+ function k(e) {
58
+ return E.createHash("md5").update(e).digest("hex").slice(0, 8);
59
59
  }
60
- function y(e) {
61
- if (i.isStringLiteral(e) || i.isNumericLiteral(e) || i.isBooleanLiteral(e)) return e.value;
62
- if (i.isNullLiteral(e)) return null;
63
- if (i.isUnaryExpression(e) && e.operator === "-" && i.isNumericLiteral(e.argument))
60
+ function S(e) {
61
+ if (a.isStringLiteral(e) || a.isNumericLiteral(e) || a.isBooleanLiteral(e)) return e.value;
62
+ if (a.isNullLiteral(e)) return null;
63
+ if (a.isUnaryExpression(e) && e.operator === "-" && a.isNumericLiteral(e.argument))
64
64
  return -e.argument.value;
65
- if (i.isObjectExpression(e)) {
65
+ if (a.isObjectExpression(e)) {
66
66
  const t = {};
67
67
  return e.properties.forEach((r) => {
68
- if (i.isObjectProperty(r)) {
69
- const s = i.isIdentifier(r.key) ? r.key.name : i.isStringLiteral(r.key) ? r.key.value : null;
68
+ if (a.isObjectProperty(r)) {
69
+ const s = a.isIdentifier(r.key) ? r.key.name : a.isStringLiteral(r.key) ? r.key.value : null;
70
70
  if (!s)
71
71
  throw new Error(`Unsupported key type in zx prop: ${r.key.type}`);
72
- t[s] = y(r.value);
72
+ t[s] = S(r.value);
73
73
  } else
74
74
  throw new Error(`Unsupported property type in zx prop: ${r.type}. Only static object properties are allowed.`);
75
75
  }), t;
76
76
  }
77
77
  throw new Error(`Dynamic expressions are not allowed in zx prop. Found: ${e.type}. Use 'style' prop for dynamic values.`);
78
78
  }
79
- function d(e, t) {
79
+ const x = /* @__PURE__ */ new Map(), m = /* @__PURE__ */ new Map();
80
+ function y(e, t) {
80
81
  if (!e.includes("zx={"))
81
- return null;
82
- const r = v(e, {
82
+ return m.has(t) ? (m.delete(t), { code: e, map: null, hasZx: !1 }) : null;
83
+ const r = $(e, {
83
84
  sourceType: "module",
84
85
  plugins: ["typescript", "jsx"]
85
- }), s = new $(e);
86
- let o = !1, p = "";
87
- return (g.default || g)(r, {
88
- JSXOpeningElement(l) {
89
- const n = l.node.attributes.find(
90
- (u) => i.isJSXAttribute(u) && u.name.name === "zx"
86
+ }), s = new w(e);
87
+ let n = !1;
88
+ const o = /* @__PURE__ */ new Set();
89
+ return (h.default || h)(r, {
90
+ JSXOpeningElement(u) {
91
+ const l = u.node.attributes.find(
92
+ (p) => a.isJSXAttribute(p) && p.name.name === "zx"
91
93
  );
92
- if (n && (o = !0, i.isJSXExpressionContainer(n.value) && i.isObjectExpression(n.value.expression))) {
93
- let u = {};
94
+ if (l && (n = !0, a.isJSXExpressionContainer(l.value) && a.isObjectExpression(l.value.expression))) {
95
+ let p = {};
94
96
  try {
95
- u = y(n.value.expression);
97
+ p = S(l.value.expression);
96
98
  } catch (c) {
97
99
  throw new Error(`Error parsing zx prop in ${t}: ${c.message}`);
98
100
  }
99
- const f = `zx-${z(JSON.stringify(u))}`, h = x(u, f);
100
- p += h + `
101
- `;
102
- const a = l.node.attributes.find(
103
- (c) => i.isJSXAttribute(c) && c.name.name === "className"
101
+ const d = k(JSON.stringify(p)), g = `zx-${d}`;
102
+ if (!x.has(d)) {
103
+ const c = v(p, g);
104
+ x.set(d, c);
105
+ }
106
+ o.add(d);
107
+ const i = u.node.attributes.find(
108
+ (c) => a.isJSXAttribute(c) && c.name.name === "className"
104
109
  );
105
- if (n.start != null && n.end != null && s.remove(n.start, n.end), a) {
106
- if (i.isStringLiteral(a.value) && a.value.start && a.value.end)
107
- s.overwrite(a.value.start, a.value.end, `"${a.value.value} ${f}"`);
108
- else if (i.isJSXExpressionContainer(a.value) && a.value.expression.start && a.value.expression.end) {
109
- const c = e.slice(a.value.expression.start, a.value.expression.end);
110
- s.overwrite(a.value.expression.start, a.value.expression.end, `\`\${${c}} ${f}\``);
110
+ if (l.start != null && l.end != null && s.remove(l.start, l.end), i) {
111
+ if (a.isStringLiteral(i.value) && i.value.start && i.value.end)
112
+ s.overwrite(i.value.start, i.value.end, `"${i.value.value} ${g}"`);
113
+ else if (a.isJSXExpressionContainer(i.value) && i.value.expression.start && i.value.expression.end) {
114
+ const c = e.slice(i.value.expression.start, i.value.expression.end);
115
+ s.overwrite(i.value.expression.start, i.value.expression.end, `\`\${${c}} ${g}\``);
111
116
  }
112
117
  } else
113
- s.appendLeft(n.start, ` className="${f}" `);
118
+ s.appendLeft(l.start, ` className="${g}" `);
114
119
  }
115
120
  }
116
- }), o ? (s.prepend(`import '${t}.style-zx.css';
121
+ }), n ? (m.set(t, o), s.prepend(`import 'virtual:style-zx.css';
117
122
  `), {
118
123
  code: s.toString(),
119
124
  map: s.generateMap({ hires: !0 }),
120
- css: p,
121
125
  hasZx: !0
122
- }) : null;
126
+ }) : (m.delete(t), null);
123
127
  }
124
- function C() {
125
- const e = /* @__PURE__ */ new Map();
128
+ function I() {
129
+ const e = "virtual:style-zx.css", t = "\0" + e;
126
130
  return {
127
131
  name: "vite-plugin-style-zx",
128
132
  enforce: "pre",
129
- resolveId(t) {
130
- return t.endsWith(".style-zx.css") ? t : null;
133
+ resolveId(r) {
134
+ return r === e ? t : null;
131
135
  },
132
- load(t) {
133
- if (t.endsWith(".style-zx.css")) {
134
- const r = t.replace(".style-zx.css", "");
135
- return e.get(r) || "";
136
+ load(r) {
137
+ if (r === t) {
138
+ const s = /* @__PURE__ */ new Set();
139
+ for (const o of m.values())
140
+ o.forEach((f) => s.add(f));
141
+ let n = "";
142
+ return s.forEach((o) => {
143
+ x.has(o) && (n += x.get(o) + `
144
+ `);
145
+ }), n;
136
146
  }
137
147
  return null;
138
148
  },
139
- transform(t, r) {
140
- if (!/\.(t|j)sx?$/.test(r))
149
+ transform(r, s) {
150
+ if (!/\.(t|j)sx?$/.test(s))
141
151
  return null;
142
- const s = d(t, r);
143
- return s && s.hasZx ? (e.set(r, s.css), {
144
- code: s.code,
145
- map: s.map
146
- }) : null;
152
+ const n = y(r, s);
153
+ return n && n.hasZx ? {
154
+ code: n.code,
155
+ map: n.map
156
+ } : null;
147
157
  },
148
- async handleHotUpdate({ file: t, modules: r, read: s, server: o }) {
149
- if (!/\.(t|j)sx?$/.test(t))
158
+ async handleHotUpdate({ file: r, modules: s, read: n, server: o }) {
159
+ if (!/\.(t|j)sx?$/.test(r))
150
160
  return;
151
- const p = await s(), m = d(p, t);
152
- if (m && m.hasZx) {
153
- e.set(t, m.css);
154
- const l = `${t}.style-zx.css`, n = o.moduleGraph.getModuleById(l);
155
- if (n)
156
- return [...r, n];
157
- }
161
+ const f = await n();
162
+ y(f, r);
163
+ const u = o.moduleGraph.getModuleById(t);
164
+ if (u)
165
+ return o.moduleGraph.invalidateModule(u), [...s, u];
158
166
  }
159
167
  };
160
168
  }
161
169
  export {
162
- C as default
170
+ I as default
163
171
  };
@@ -0,0 +1,25 @@
1
+ import { CSSProperties } from 'react';
2
+ export { createTheme, useTheme } from './theme';
3
+ export interface ZxStyle extends CSSProperties {
4
+ [key: string]: any;
5
+ m?: number | string;
6
+ mt?: number | string;
7
+ mr?: number | string;
8
+ mb?: number | string;
9
+ ml?: number | string;
10
+ mx?: number | string;
11
+ my?: number | string;
12
+ p?: number | string;
13
+ pt?: number | string;
14
+ pr?: number | string;
15
+ pb?: number | string;
16
+ pl?: number | string;
17
+ px?: number | string;
18
+ py?: number | string;
19
+ bg?: string;
20
+ }
21
+ declare module 'react' {
22
+ interface HTMLAttributes<T> {
23
+ zx?: ZxStyle;
24
+ }
25
+ }
@@ -0,0 +1,2 @@
1
+ export declare function createTheme<T extends object>(themeConfig: T): T;
2
+ export declare function useTheme<T = any>(): T;
@@ -0,0 +1 @@
1
+ export declare function compileStyle(style: any, className: string): string;
@@ -0,0 +1,2 @@
1
+ import { Plugin } from 'vite';
2
+ export default function styleZx(): Plugin;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "style-zx",
3
3
  "private": false,
4
- "version": "0.0.5",
4
+ "version": "0.0.7",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "repository": {