style-zx 0.0.6 → 0.0.8

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/plugin.cjs CHANGED
@@ -1,6 +1,8 @@
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 b=require("@babel/parser"),g=require("@babel/traverse"),S=require("@babel/types"),$=require("magic-string"),w=require("crypto");function z(e){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(e){for(const n in e)if(n!=="default"){const r=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(t,n,r.get?r:{enumerable:!0,get:()=>e[n]})}}return t.default=e,Object.freeze(t)}const i=z(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 j(e){return e.replace(/[A-Z]/g,t=>`-${t.toLowerCase()}`)}function N(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 n=`.${t} {`,r="";for(const s in e){const p=e[s];if(s.startsWith("&")||s.startsWith(":")||s.startsWith("@")){const u=s.replace(/&/g,`.${t}`),l=x(p,"TEMP_CLASS").replace(".TEMP_CLASS",u);r+=`
2
+ ${l}`;continue}(E[s]||[s]).forEach(u=>{const l=j(u),c=N(u,p);n+=`
3
+ ${l}: ${c};`})}return n+=`
4
+ }`,n+r}function O(e){return w.createHash("md5").update(e).digest("hex").slice(0,8)}function h(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(n=>{if(i.isObjectProperty(n)){const r=i.isIdentifier(n.key)?n.key.name:i.isStringLiteral(n.key)?n.key.value:null;if(!r)throw new Error(`Unsupported key type in zx prop: ${n.key.type}`);t[r]=h(n.value)}else throw new Error(`Unsupported property type in zx prop: ${n.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 y(e,t,n){if(!e.includes("zx={"))return null;const r=b.parse(e,{sourceType:"module",plugins:["typescript","jsx"]}),s=new $(e);let p=!1,a="";return(g.default||g)(r,{JSXOpeningElement(l){const c=l.node.attributes.find(f=>i.isJSXAttribute(f)&&f.name.name==="zx");if(c&&(p=!0,i.isJSXExpressionContainer(c.value)&&i.isObjectExpression(c.value.expression))){let f={};try{f=h(c.value.expression)}catch(m){throw new Error(`Error parsing zx prop in ${t}: ${m.message}`)}const d=`zx-${O(JSON.stringify(f))}`,v=x(f,d);a+=v+`
5
+ `;const o=l.node.attributes.find(m=>i.isJSXAttribute(m)&&m.name.name==="className");if(c.start!=null&&c.end!=null&&s.remove(c.start,c.end),o){if(i.isStringLiteral(o.value)&&o.value.start&&o.value.end)s.overwrite(o.value.start,o.value.end,`"${o.value.value} ${d}"`);else if(i.isJSXExpressionContainer(o.value)&&o.value.expression.start&&o.value.expression.end){const m=e.slice(o.value.expression.start,o.value.expression.end);s.overwrite(o.value.expression.start,o.value.expression.end,`\`\${${m}} ${d}\``)}}else s.appendLeft(c.start,` className="${d}" `)}}}),p?(n&&s.prepend(`import '${t}.style-zx.css';
6
+ `),{code:s.toString(),map:s.generateMap({hires:!0}),css:a,hasZx:!0}):null}function k(){const e=new Map;let t;const n=new Set;return[{name:"vite-plugin-style-zx",enforce:"pre",configResolved(r){t=r},resolveId(r){return r.endsWith(".style-zx.css")?r:null},load(r){if(r.endsWith(".style-zx.css")){const s=r.replace(".style-zx.css","");return e.get(s)||""}return null},transform(r,s){if(!/\.(t|j)sx?$/.test(s))return null;const p=t.command==="serve",a=y(r,s,p);return a&&a.hasZx?(e.set(s,a.css),t.command==="build"&&n.add(a.css),{code:a.code,map:a.map}):null},async handleHotUpdate({file:r,modules:s,read:p,server:a}){if(!/\.(t|j)sx?$/.test(r))return;const u=await p(),l=y(u,r,!0);if(l&&l.hasZx){e.set(r,l.css);const c=`${r}.style-zx.css`,f=a.moduleGraph.getModuleById(c);if(f)return[...s,f]}}},{name:"vite-plugin-style-zx-post",enforce:"post",generateBundle(r,s){if(t.command==="build"&&n.size>0){const p=this.emitFile({type:"asset",name:"style-zx.css",source:Array.from(n).join(`
7
+ `)}),a=this.getFileName(p);for(const u of Object.values(s))if(u.type==="asset"&&u.fileName.endsWith(".html")){const l=u.source;l.includes("</head>")&&(u.source=l.replace("</head>",`<link rel="stylesheet" href="/${a}">
8
+ </head>`))}}}}]}module.exports=k;
package/dist/plugin.js CHANGED
@@ -1,8 +1,8 @@
1
- import { parse as v } from "@babel/parser";
1
+ import { parse as $ } from "@babel/parser";
2
2
  import g from "@babel/traverse";
3
3
  import * as i from "@babel/types";
4
- import $ from "magic-string";
5
- import S from "crypto";
4
+ import S from "magic-string";
5
+ import b from "crypto";
6
6
  const w = {
7
7
  m: ["margin"],
8
8
  mt: ["marginTop"],
@@ -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
+ }, z = /* @__PURE__ */ new Set([
23
23
  "opacity",
24
24
  "fontWeight",
25
25
  "lineHeight",
@@ -29,135 +29,165 @@ const w = {
29
29
  "flexShrink",
30
30
  "order"
31
31
  ]);
32
- function b(e) {
33
- return e.replace(/[A-Z]/g, (t) => `-${t.toLowerCase()}`);
32
+ function E(e) {
33
+ return e.replace(/[A-Z]/g, (r) => `-${r.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 L(e, r) {
36
+ return typeof r == "number" && !z.has(e) ? `${r}px` : typeof r == "string" && r.startsWith("$theme.") ? `var(${r.replace("$theme.", "--theme-").replace(/\./g, "-")})` : String(r);
37
37
  }
38
- function x(e, t) {
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);
38
+ function y(e, r) {
39
+ let n = `.${r} {`, s = "";
40
+ for (const t in e) {
41
+ const p = e[t];
42
+ if (t.startsWith("&") || t.startsWith(":") || t.startsWith("@")) {
43
+ const u = t.replace(/&/g, `.${r}`), l = y(p, "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);
50
- r += `
51
- ${n}: ${u};`;
48
+ (w[t] || [t]).forEach((u) => {
49
+ const l = E(u), c = L(u, p);
50
+ n += `
51
+ ${l}: ${c};`;
52
52
  });
53
53
  }
54
- return r += `
55
- }`, r + s;
54
+ return n += `
55
+ }`, n + s;
56
56
  }
57
- function z(e) {
58
- return S.createHash("md5").update(e).digest("hex").slice(0, 8);
57
+ function N(e) {
58
+ return b.createHash("md5").update(e).digest("hex").slice(0, 8);
59
59
  }
60
- function y(e) {
60
+ function h(e) {
61
61
  if (i.isStringLiteral(e) || i.isNumericLiteral(e) || i.isBooleanLiteral(e)) return e.value;
62
62
  if (i.isNullLiteral(e)) return null;
63
63
  if (i.isUnaryExpression(e) && e.operator === "-" && i.isNumericLiteral(e.argument))
64
64
  return -e.argument.value;
65
65
  if (i.isObjectExpression(e)) {
66
- const t = {};
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;
66
+ const r = {};
67
+ return e.properties.forEach((n) => {
68
+ if (i.isObjectProperty(n)) {
69
+ const s = i.isIdentifier(n.key) ? n.key.name : i.isStringLiteral(n.key) ? n.key.value : null;
70
70
  if (!s)
71
- throw new Error(`Unsupported key type in zx prop: ${r.key.type}`);
72
- t[s] = y(r.value);
71
+ throw new Error(`Unsupported key type in zx prop: ${n.key.type}`);
72
+ r[s] = h(n.value);
73
73
  } else
74
- throw new Error(`Unsupported property type in zx prop: ${r.type}. Only static object properties are allowed.`);
75
- }), t;
74
+ throw new Error(`Unsupported property type in zx prop: ${n.type}. Only static object properties are allowed.`);
75
+ }), r;
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
+ function x(e, r, n) {
80
80
  if (!e.includes("zx={"))
81
81
  return null;
82
- const r = v(e, {
82
+ const s = $(e, {
83
83
  sourceType: "module",
84
84
  plugins: ["typescript", "jsx"]
85
- }), s = new $(e);
86
- let o = !1, p = "";
87
- return (g.default || g)(r, {
85
+ }), t = new S(e);
86
+ let p = !1, a = "";
87
+ return (g.default || g)(s, {
88
88
  JSXOpeningElement(l) {
89
- const n = l.node.attributes.find(
90
- (u) => i.isJSXAttribute(u) && u.name.name === "zx"
89
+ const c = l.node.attributes.find(
90
+ (m) => i.isJSXAttribute(m) && m.name.name === "zx"
91
91
  );
92
- if (n && (o = !0, i.isJSXExpressionContainer(n.value) && i.isObjectExpression(n.value.expression))) {
93
- let u = {};
92
+ if (c && (p = !0, i.isJSXExpressionContainer(c.value) && i.isObjectExpression(c.value.expression))) {
93
+ let m = {};
94
94
  try {
95
- u = y(n.value.expression);
96
- } catch (c) {
97
- throw new Error(`Error parsing zx prop in ${t}: ${c.message}`);
95
+ m = h(c.value.expression);
96
+ } catch (f) {
97
+ throw new Error(`Error parsing zx prop in ${r}: ${f.message}`);
98
98
  }
99
- const f = `zx-${z(JSON.stringify(u))}`, h = x(u, f);
100
- p += h + `
99
+ const d = `zx-${N(JSON.stringify(m))}`, v = y(m, d);
100
+ a += v + `
101
101
  `;
102
- const a = l.node.attributes.find(
103
- (c) => i.isJSXAttribute(c) && c.name.name === "className"
102
+ const o = l.node.attributes.find(
103
+ (f) => i.isJSXAttribute(f) && f.name.name === "className"
104
104
  );
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}\``);
105
+ if (c.start != null && c.end != null && t.remove(c.start, c.end), o) {
106
+ if (i.isStringLiteral(o.value) && o.value.start && o.value.end)
107
+ t.overwrite(o.value.start, o.value.end, `"${o.value.value} ${d}"`);
108
+ else if (i.isJSXExpressionContainer(o.value) && o.value.expression.start && o.value.expression.end) {
109
+ const f = e.slice(o.value.expression.start, o.value.expression.end);
110
+ t.overwrite(o.value.expression.start, o.value.expression.end, `\`\${${f}} ${d}\``);
111
111
  }
112
112
  } else
113
- s.appendLeft(n.start, ` className="${f}" `);
113
+ t.appendLeft(c.start, ` className="${d}" `);
114
114
  }
115
115
  }
116
- }), o ? (s.prepend(`import '${t}.style-zx.css';
116
+ }), p ? (n && t.prepend(`import '${r}.style-zx.css';
117
117
  `), {
118
- code: s.toString(),
119
- map: s.generateMap({ hires: !0 }),
120
- css: p,
118
+ code: t.toString(),
119
+ map: t.generateMap({ hires: !0 }),
120
+ css: a,
121
121
  hasZx: !0
122
122
  }) : null;
123
123
  }
124
- function C() {
124
+ function I() {
125
125
  const e = /* @__PURE__ */ new Map();
126
- return {
127
- name: "vite-plugin-style-zx",
128
- enforce: "pre",
129
- resolveId(t) {
130
- return t.endsWith(".style-zx.css") ? t : null;
131
- },
132
- load(t) {
133
- if (t.endsWith(".style-zx.css")) {
134
- const r = t.replace(".style-zx.css", "");
135
- return e.get(r) || "";
136
- }
137
- return null;
138
- },
139
- transform(t, r) {
140
- if (!/\.(t|j)sx?$/.test(r))
126
+ let r;
127
+ const n = /* @__PURE__ */ new Set();
128
+ return [
129
+ {
130
+ name: "vite-plugin-style-zx",
131
+ enforce: "pre",
132
+ configResolved(s) {
133
+ r = s;
134
+ },
135
+ resolveId(s) {
136
+ return s.endsWith(".style-zx.css") ? s : null;
137
+ },
138
+ load(s) {
139
+ if (s.endsWith(".style-zx.css")) {
140
+ const t = s.replace(".style-zx.css", "");
141
+ return e.get(t) || "";
142
+ }
141
143
  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;
144
+ },
145
+ transform(s, t) {
146
+ if (!/\.(t|j)sx?$/.test(t))
147
+ return null;
148
+ const p = r.command === "serve", a = x(s, t, p);
149
+ return a && a.hasZx ? (e.set(t, a.css), r.command === "build" && n.add(a.css), {
150
+ code: a.code,
151
+ map: a.map
152
+ }) : null;
153
+ },
154
+ async handleHotUpdate({ file: s, modules: t, read: p, server: a }) {
155
+ if (!/\.(t|j)sx?$/.test(s))
156
+ return;
157
+ const u = await p(), l = x(u, s, !0);
158
+ if (l && l.hasZx) {
159
+ e.set(s, l.css);
160
+ const c = `${s}.style-zx.css`, m = a.moduleGraph.getModuleById(c);
161
+ if (m)
162
+ return [...t, m];
163
+ }
164
+ }
147
165
  },
148
- async handleHotUpdate({ file: t, modules: r, read: s, server: o }) {
149
- if (!/\.(t|j)sx?$/.test(t))
150
- 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];
166
+ {
167
+ name: "vite-plugin-style-zx-post",
168
+ enforce: "post",
169
+ generateBundle(s, t) {
170
+ if (r.command === "build" && n.size > 0) {
171
+ const p = this.emitFile({
172
+ type: "asset",
173
+ name: "style-zx.css",
174
+ source: Array.from(n).join(`
175
+ `)
176
+ }), a = this.getFileName(p);
177
+ for (const u of Object.values(t))
178
+ if (u.type === "asset" && u.fileName.endsWith(".html")) {
179
+ const l = u.source;
180
+ l.includes("</head>") && (u.source = l.replace(
181
+ "</head>",
182
+ `<link rel="stylesheet" href="/${a}">
183
+ </head>`
184
+ ));
185
+ }
186
+ }
157
187
  }
158
188
  }
159
- };
189
+ ];
160
190
  }
161
191
  export {
162
- C as default
192
+ I as default
163
193
  };
@@ -1,2 +1,2 @@
1
1
  import { Plugin } from 'vite';
2
- export default function styleZx(): Plugin;
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.6",
4
+ "version": "0.0.8",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "repository": {