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 +1 -0
- package/dist/plugin.cjs +6 -6
- package/dist/plugin.d.ts +3 -0
- package/dist/plugin.js +89 -81
- package/dist/style-zx/index.d.ts +25 -0
- package/dist/style-zx/theme.d.ts +2 -0
- package/dist/vite-plugin-style-zx/compiler.d.ts +1 -0
- package/dist/vite-plugin-style-zx/index.d.ts +2 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/plugin.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
"use strict";const
|
|
2
|
-
${
|
|
3
|
-
${
|
|
4
|
-
}`,
|
|
5
|
-
|
|
6
|
-
`)
|
|
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
package/dist/plugin.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { parse as
|
|
2
|
-
import
|
|
3
|
-
import * as
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
const
|
|
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
|
-
},
|
|
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
|
|
32
|
+
function z(e) {
|
|
33
33
|
return e.replace(/[A-Z]/g, (t) => `-${t.toLowerCase()}`);
|
|
34
34
|
}
|
|
35
|
-
function
|
|
36
|
-
return typeof t == "number" && !
|
|
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
|
|
38
|
+
function v(e, t) {
|
|
39
39
|
let r = `.${t} {`, s = "";
|
|
40
|
-
for (const
|
|
41
|
-
const
|
|
42
|
-
if (
|
|
43
|
-
const
|
|
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
|
-
${
|
|
45
|
+
${l}`;
|
|
46
46
|
continue;
|
|
47
47
|
}
|
|
48
|
-
(
|
|
49
|
-
const
|
|
48
|
+
(b[n] || [n]).forEach((u) => {
|
|
49
|
+
const l = z(u), p = M(u, o);
|
|
50
50
|
r += `
|
|
51
|
-
${
|
|
51
|
+
${l}: ${p};`;
|
|
52
52
|
});
|
|
53
53
|
}
|
|
54
54
|
return r += `
|
|
55
55
|
}`, r + s;
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
return
|
|
57
|
+
function k(e) {
|
|
58
|
+
return E.createHash("md5").update(e).digest("hex").slice(0, 8);
|
|
59
59
|
}
|
|
60
|
-
function
|
|
61
|
-
if (
|
|
62
|
-
if (
|
|
63
|
-
if (
|
|
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 (
|
|
65
|
+
if (a.isObjectExpression(e)) {
|
|
66
66
|
const t = {};
|
|
67
67
|
return e.properties.forEach((r) => {
|
|
68
|
-
if (
|
|
69
|
-
const s =
|
|
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] =
|
|
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
|
-
|
|
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 =
|
|
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
|
|
86
|
-
let
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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 (
|
|
93
|
-
let
|
|
94
|
+
if (l && (n = !0, a.isJSXExpressionContainer(l.value) && a.isObjectExpression(l.value.expression))) {
|
|
95
|
+
let p = {};
|
|
94
96
|
try {
|
|
95
|
-
|
|
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
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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 (
|
|
106
|
-
if (
|
|
107
|
-
s.overwrite(
|
|
108
|
-
else if (
|
|
109
|
-
const c = e.slice(
|
|
110
|
-
s.overwrite(
|
|
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(
|
|
118
|
+
s.appendLeft(l.start, ` className="${g}" `);
|
|
114
119
|
}
|
|
115
120
|
}
|
|
116
|
-
}),
|
|
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
|
|
125
|
-
const e =
|
|
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(
|
|
130
|
-
return
|
|
133
|
+
resolveId(r) {
|
|
134
|
+
return r === e ? t : null;
|
|
131
135
|
},
|
|
132
|
-
load(
|
|
133
|
-
if (t
|
|
134
|
-
const
|
|
135
|
-
|
|
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(
|
|
140
|
-
if (!/\.(t|j)sx?$/.test(
|
|
149
|
+
transform(r, s) {
|
|
150
|
+
if (!/\.(t|j)sx?$/.test(s))
|
|
141
151
|
return null;
|
|
142
|
-
const
|
|
143
|
-
return
|
|
144
|
-
code:
|
|
145
|
-
map:
|
|
146
|
-
}
|
|
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:
|
|
149
|
-
if (!/\.(t|j)sx?$/.test(
|
|
158
|
+
async handleHotUpdate({ file: r, modules: s, read: n, server: o }) {
|
|
159
|
+
if (!/\.(t|j)sx?$/.test(r))
|
|
150
160
|
return;
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
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 @@
|
|
|
1
|
+
export declare function compileStyle(style: any, className: string): string;
|