@weapp-tailwindcss/react-native 0.2.18 → 0.2.19

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/babel.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { PluginObject, PluginPass } from "@babel/core";
2
1
  import * as t from "@babel/types";
2
+ import { PluginObject, PluginPass } from "@babel/core";
3
3
  //#region src/babel.d.ts
4
4
  export interface WeappReactNativeBabelOptions {
5
5
  classNameSet?: Iterable<string> | undefined;
package/dist/compiler.cjs CHANGED
@@ -1,343 +1,14 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_rolldown_runtime = require("./rolldown-runtime-VH7oDXx4.cjs");
3
2
  let node_crypto = require("node:crypto");
4
- let postcss = require("postcss");
5
- postcss = require_rolldown_runtime.__toESM(postcss, 1);
3
+ let _weapp_tailwindcss_postcss_native = require("@weapp-tailwindcss/postcss/native");
6
4
  //#region src/compiler.ts
7
- const CLASS_SELECTOR_RE = /\.((?:\\.|[^\s.#:[>+~])+)/g;
8
- const COLOR_PROPERTIES = /* @__PURE__ */ new Set([
9
- "color",
10
- "backgroundColor",
11
- "borderColor",
12
- "borderTopColor",
13
- "borderRightColor",
14
- "borderBottomColor",
15
- "borderLeftColor",
16
- "shadowColor",
17
- "textDecorationColor"
18
- ]);
19
- const NUMERIC_PROPERTIES = /* @__PURE__ */ new Set([
20
- "aspectRatio",
21
- "borderBottomWidth",
22
- "borderLeftWidth",
23
- "borderRadius",
24
- "borderRightWidth",
25
- "borderTopLeftRadius",
26
- "borderTopRightRadius",
27
- "borderTopWidth",
28
- "borderWidth",
29
- "bottom",
30
- "elevation",
31
- "flex",
32
- "flexBasis",
33
- "flexGrow",
34
- "flexShrink",
35
- "fontSize",
36
- "gap",
37
- "height",
38
- "left",
39
- "letterSpacing",
40
- "lineHeight",
41
- "margin",
42
- "marginBottom",
43
- "marginHorizontal",
44
- "marginLeft",
45
- "marginRight",
46
- "marginTop",
47
- "marginVertical",
48
- "maxHeight",
49
- "maxWidth",
50
- "minHeight",
51
- "minWidth",
52
- "opacity",
53
- "padding",
54
- "paddingBottom",
55
- "paddingHorizontal",
56
- "paddingLeft",
57
- "paddingRight",
58
- "paddingTop",
59
- "paddingVertical",
60
- "right",
61
- "top",
62
- "width",
63
- "zIndex"
64
- ]);
65
- const STRING_PROPERTIES = /* @__PURE__ */ new Set([
66
- "alignContent",
67
- "alignItems",
68
- "alignSelf",
69
- "borderStyle",
70
- "direction",
71
- "display",
72
- "flexDirection",
73
- "flexWrap",
74
- "fontFamily",
75
- "fontStyle",
76
- "fontWeight",
77
- "justifyContent",
78
- "overflow",
79
- "position",
80
- "textAlign",
81
- "textDecorationLine",
82
- "textDecorationStyle",
83
- "textTransform",
84
- "writingDirection"
85
- ]);
86
- const SHORTHANDS = {
87
- margin: [
88
- "marginTop",
89
- "marginRight",
90
- "marginBottom",
91
- "marginLeft"
92
- ],
93
- padding: [
94
- "paddingTop",
95
- "paddingRight",
96
- "paddingBottom",
97
- "paddingLeft"
98
- ]
99
- };
100
- const UNSUPPORTED_PROPERTIES = /* @__PURE__ */ new Set([
101
- "filter",
102
- "backdropFilter",
103
- "animation",
104
- "transition",
105
- "textShadow",
106
- "backgroundImage",
107
- "appearance",
108
- "content",
109
- "cursor",
110
- "userSelect",
111
- "whiteSpace",
112
- "objectFit",
113
- "listStyleType",
114
- "outline"
115
- ]);
116
- function decodeCssIdentifier(value) {
117
- return value.replace(/\\([0-9a-f]{1,6})\s?/gi, (_, code) => String.fromCodePoint(Number.parseInt(code, 16))).replace(/\\(.)/g, "$1");
118
- }
119
- function splitClassName(className) {
120
- const parts = [];
121
- let current = "";
122
- let bracketDepth = 0;
123
- let escaped = false;
124
- for (const character of className) {
125
- if (escaped) {
126
- current += character;
127
- escaped = false;
128
- continue;
129
- }
130
- if (character === "\\") {
131
- current += character;
132
- escaped = true;
133
- continue;
134
- }
135
- if (character === "[") bracketDepth += 1;
136
- if (character === "]") bracketDepth = Math.max(0, bracketDepth - 1);
137
- if (character === ":" && bracketDepth === 0) {
138
- parts.push(current);
139
- current = "";
140
- continue;
141
- }
142
- current += character;
143
- }
144
- parts.push(current);
145
- return parts;
146
- }
147
- function baseClassName(className) {
148
- return splitClassName(className).at(-1);
149
- }
150
- function unsupportedVariant(className) {
151
- return splitClassName(className).slice(0, -1).find((variant) => ![
152
- "dark",
153
- "ios",
154
- "android",
155
- "native"
156
- ].includes(variant));
157
- }
158
- function propertyName(property) {
159
- return {
160
- "padding-inline": "paddingHorizontal",
161
- "padding-inline-start": "paddingLeft",
162
- "padding-inline-end": "paddingRight",
163
- "padding-block": "paddingVertical",
164
- "padding-block-start": "paddingTop",
165
- "padding-block-end": "paddingBottom",
166
- "margin-inline": "marginHorizontal",
167
- "margin-inline-start": "marginLeft",
168
- "margin-inline-end": "marginRight",
169
- "margin-block": "marginVertical",
170
- "margin-block-start": "marginTop",
171
- "margin-block-end": "marginBottom"
172
- }[property] ?? property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
173
- }
174
- function splitValue(value) {
175
- return value.trim().split(/\s+/).filter(Boolean);
176
- }
177
- function parseNumber(value, variables) {
178
- const resolved = resolveVariables(value, variables);
179
- const fraction = resolved.match(/^(-?(?:\d+\.\d+|\d+|\.\d+))\s*\/\s*(-?(?:\d+\.\d+|\d+|\.\d+))$/);
180
- if (fraction && Number(fraction[2]) !== 0) return Number(fraction[1]) / Number(fraction[2]);
181
- const calculated = resolved.match(/^calc\(\s*(-?(?:\d+\.\d+|\d+|\.\d+))(px|rem|em|%)?\s*\*\s*(-?(?:\d+\.\d+|\d+|\.\d+))\s*\)$/);
182
- if (calculated) {
183
- const base = parseNumber(`${calculated[1]}${calculated[2] ?? ""}`, variables);
184
- return typeof base === "number" ? base * Number(calculated[3]) : void 0;
185
- }
186
- const match = resolved.match(/^(-?(?:\d+\.\d+|\d+|\.\d+))(px|rem|em|%)?$/);
187
- if (!match) return;
188
- const number = Number(match[1]);
189
- if (match[2] === "%") return `${number}%`;
190
- if (match[2] === "rem" || match[2] === "em") return number * 16;
191
- return number;
192
- }
193
- function resolveVariables(value, variables) {
194
- return value.replace(/var\((--[\w-]+)(?:,\s*([^)]*))?\)/g, (_, key, fallback) => variables[key] ?? fallback ?? `var(${key})`);
195
- }
196
- function isColor(value) {
197
- return /^(?:#[\da-f]{3,8}|(?:rgb|rgba|hsl|hsla|oklch|oklab)\([^)]*\)|[a-z]+)$/i.test(value);
198
- }
199
- function oklchToHex(value) {
200
- const match = value.match(/^oklch\(\s*([\d.]+)%?\s+([\d.]+)\s+([\d.]+)(?:\s+\/\s*([\d.]+))?\s*\)$/i);
201
- if (!match) return;
202
- const lightness = Number(match[1]) > 1 ? Number(match[1]) / 100 : Number(match[1]);
203
- const chroma = Number(match[2]);
204
- const hue = Number(match[3]) * Math.PI / 180;
205
- const alpha = match[4] === void 0 ? 1 : Number(match[4]);
206
- const a = chroma * Math.cos(hue);
207
- const b = chroma * Math.sin(hue);
208
- const l = lightness + .3963377774 * a + .2158037573 * b;
209
- const m = lightness - .1055613458 * a - .0638541728 * b;
210
- const s = lightness - .0894841775 * a - 1.291485548 * b;
211
- const linear = (channel) => channel ** 3;
212
- const red = 4.0767416621 * linear(l) - 3.3077115913 * linear(m) + .2309699292 * linear(s);
213
- const green = -1.2684380046 * linear(l) + 2.6097574011 * linear(m) - .3413193965 * linear(s);
214
- const blue = -.0041960863 * linear(l) - .7034186147 * linear(m) + 1.707614701 * linear(s);
215
- const toByte = (channel) => Math.round(Math.max(0, Math.min(1, channel <= .0031308 ? 12.92 * channel : 1.055 * channel ** (1 / 2.4) - .055)) * 255);
216
- const hex = [
217
- red,
218
- green,
219
- blue
220
- ].map((channel) => toByte(channel).toString(16).padStart(2, "0")).join("");
221
- return alpha < 1 ? `#${hex}${Math.round(alpha * 255).toString(16).padStart(2, "0")}` : `#${hex}`;
222
- }
223
- function normalizeColor(value) {
224
- return /^oklch\(/i.test(value) ? oklchToHex(value) : value;
225
- }
226
- function parseTransform(value, variables) {
227
- const transform = [];
228
- for (const match of value.matchAll(/(translateX|translateY|translate|scaleX|scaleY|scale|rotate|skewX|skewY)\(([^)]*)\)/g)) {
229
- const name = match[1];
230
- const values = splitValue(resolveVariables(match[2] ?? "", variables).replace(",", " "));
231
- if (!name || !values.length) continue;
232
- if (name.startsWith("scale")) {
233
- const number = Number(values[0] ?? "");
234
- if (Number.isFinite(number)) transform.push({ [name]: number });
235
- continue;
236
- }
237
- if (name === "translate") {
238
- const x = parseNumber(values[0] ?? "", variables);
239
- const y = parseNumber(values[1] ?? "0", variables);
240
- if (x !== void 0 && y !== void 0) transform.push({ translateX: x }, { translateY: y });
241
- continue;
242
- }
243
- const property = name.replace(/^translate$/, "translateX");
244
- const parsed = name.startsWith("rotate") || name.startsWith("skew") ? values[0] : parseNumber(values[0] ?? "", variables);
245
- if (parsed !== void 0) transform.push({ [property]: parsed });
246
- }
247
- return transform.length ? transform : void 0;
248
- }
249
- function parseShadow(value, variables) {
250
- const resolved = resolveVariables(value, variables);
251
- const color = resolved.match(/(?:rgba?|hsla?)\([^)]*\)|#[\da-f]{3,8}/i)?.[0];
252
- const parts = splitValue(color ? resolved.replace(color, "") : resolved);
253
- const lengths = parts.filter((part) => parseNumber(part, variables) !== void 0).slice(0, 3);
254
- if (lengths.length < 2) return;
255
- const [x = "0", y = "0", blur = "0"] = lengths;
256
- const shadowColor = normalizeColor(color ?? parts.find((part) => isColor(part)) ?? "#000000") ?? "#000000";
257
- const opacity = /rgba\([^,]+,[^,]+,[^,]+,\s*([\d.]+)\)/i.exec(shadowColor)?.[1];
258
- return {
259
- shadowOffset: {
260
- width: parseNumber(x, variables),
261
- height: parseNumber(y, variables)
262
- },
263
- shadowRadius: parseNumber(blur, variables),
264
- shadowColor,
265
- ...opacity ? { shadowOpacity: Number(opacity) } : {}
266
- };
267
- }
268
- function parseValue(property, value, variables) {
269
- const trimmed = value.trim();
270
- if (UNSUPPORTED_PROPERTIES.has(property)) return;
271
- if (trimmed.includes("linear-gradient(") || trimmed.includes("url(")) return;
272
- if (COLOR_PROPERTIES.has(property)) {
273
- const color = resolveVariables(trimmed, variables);
274
- return isColor(color) ? normalizeColor(color) : void 0;
275
- }
276
- if (NUMERIC_PROPERTIES.has(property)) {
277
- const number = parseNumber(trimmed, variables);
278
- if (property === "opacity" && typeof number === "string" && number.endsWith("%")) return Number.parseFloat(number) / 100;
279
- return number;
280
- }
281
- if (property === "transform") return parseTransform(trimmed, variables);
282
- if (property === "boxShadow") return parseShadow(trimmed, variables);
283
- if (property === "display" && !["flex", "none"].includes(trimmed)) return;
284
- return STRING_PROPERTIES.has(property) ? trimmed : void 0;
285
- }
286
- function expandDeclaration(property, value, variables) {
287
- if (property === "margin" || property === "padding") {
288
- const parts = splitValue(value).map((item) => parseNumber(item, variables));
289
- if (parts.includes(void 0)) return;
290
- const [top, right = top, bottom = top, left = right] = parts;
291
- return Object.fromEntries(SHORTHANDS[property].map((key, index) => [key, [
292
- top,
293
- right,
294
- bottom,
295
- left
296
- ][index]]));
297
- }
298
- if (property === "border") {
299
- const width = splitValue(value).find((item) => /^\d/.test(item));
300
- const color = splitValue(value).find((item) => isColor(item));
301
- return {
302
- ...width ? { borderWidth: parseNumber(width, variables) } : {},
303
- ...color ? { borderColor: color } : {}
304
- };
305
- }
306
- if (property === "boxShadow") return parseShadow(value, variables);
307
- const parsed = parseValue(property, value, variables);
308
- return parsed === void 0 ? void 0 : { [property]: parsed };
309
- }
310
- function walkClasses(selector) {
311
- const classes = [];
312
- for (const match of selector.matchAll(CLASS_SELECTOR_RE)) {
313
- const token = decodeCssIdentifier(match[1]);
314
- if (token && !classes.includes(token)) classes.push(token);
315
- }
316
- return classes;
317
- }
318
- function variantForClass(className) {
319
- const result = {};
320
- for (const variant of splitClassName(className).slice(0, -1)) {
321
- if (variant === "dark") result.colorScheme = "dark";
322
- if (variant === "ios" || variant === "android" || variant === "native") result.platform = variant;
323
- }
324
- return result;
325
- }
326
- function atRuleVariant(node) {
327
- if (!node) return {};
328
- const params = node.params.toLowerCase();
329
- if (params.includes("prefers-color-scheme") && params.includes("dark")) return { colorScheme: "dark" };
330
- if (params.includes("platform") && params.includes("ios")) return { platform: "ios" };
331
- if (params.includes("platform") && params.includes("android")) return { platform: "android" };
332
- return {};
333
- }
334
5
  function addNativeVariantRules(manifest, candidates) {
335
6
  for (const candidate of candidates) {
336
- const parts = splitClassName(candidate);
7
+ const parts = (0, _weapp_tailwindcss_postcss_native.splitClassName)(candidate);
337
8
  if (parts.length < 2) continue;
338
9
  const base = parts.at(-1);
339
10
  if (!base || manifest.rules[candidate] || !manifest.rules[base]) continue;
340
- const variant = variantForClass(candidate);
11
+ const variant = (0, _weapp_tailwindcss_postcss_native.variantForClass)(candidate);
341
12
  if (!variant.colorScheme && !variant.platform) continue;
342
13
  manifest.rules[candidate] = manifest.rules[base].map((rule) => ({
343
14
  ...rule,
@@ -347,69 +18,6 @@ function addNativeVariantRules(manifest, candidates) {
347
18
  manifest.classSet.push(candidate);
348
19
  }
349
20
  }
350
- function ancestors(node) {
351
- const result = [];
352
- let current = node.parent;
353
- while (current && current.type !== "root") {
354
- if (current.type === "atrule") result.unshift(current);
355
- current = current.parent;
356
- }
357
- return result;
358
- }
359
- function collectVariables(root) {
360
- const variables = {};
361
- root.walkDecls((decl) => {
362
- if (decl.prop.startsWith("--")) variables[decl.prop] = decl.value.trim();
363
- });
364
- return variables;
365
- }
366
- function addWarning(warnings, warning) {
367
- if (!warnings.some((item) => item.message === warning.message && item.property === warning.property && item.className === warning.className)) warnings.push(warning);
368
- }
369
- function compileRule(rule, className, variables, warnings, order) {
370
- const unsupported = unsupportedVariant(className);
371
- if (unsupported) {
372
- addWarning(warnings, {
373
- className,
374
- property: "variant",
375
- message: `不支持将 ${unsupported}: 变体编译为 React Native 条件样式`
376
- });
377
- return [];
378
- }
379
- const styles = {
380
- normal: {},
381
- important: {}
382
- };
383
- rule.walkDecls((decl) => {
384
- if (decl.prop.startsWith("--")) return;
385
- const property = propertyName(decl.prop);
386
- const important = decl.important || /!important\s*$/i.test(decl.value);
387
- const value = decl.value.replace(/\s*!important\s*$/i, "");
388
- const expanded = expandDeclaration(property, value, variables);
389
- if (!expanded || Object.values(expanded).includes(void 0)) {
390
- addWarning(warnings, {
391
- className,
392
- property,
393
- message: `不支持将 ${decl.prop}: ${value} 编译为 React Native style`
394
- });
395
- return;
396
- }
397
- Object.assign(styles[important ? "important" : "normal"], expanded);
398
- });
399
- const variant = {
400
- ...variantForClass(className),
401
- ...ancestors(rule).reduce((result, node) => ({
402
- ...result,
403
- ...atRuleVariant(node)
404
- }), {})
405
- };
406
- return ["normal", "important"].filter((kind) => Object.keys(styles[kind]).length > 0).map((kind) => ({
407
- style: styles[kind],
408
- ...variant,
409
- important: kind === "important" || void 0,
410
- order
411
- }));
412
- }
413
21
  /** 为 manifest 生成稳定的 StyleSheet ID 和 Babel 静态 lookup。 */
414
22
  function finalizeNativeManifest(manifest) {
415
23
  const styleSheet = {};
@@ -430,30 +38,20 @@ function finalizeNativeManifest(manifest) {
430
38
  return manifest;
431
39
  }
432
40
  function compileNativeStylesheet(css, options = {}) {
433
- const root = postcss.default.parse(css);
434
- const variables = collectVariables(root);
435
- const allowed = options.classSet ? new Set(options.classSet) : void 0;
436
- const rules = {};
437
- const warnings = [];
438
- let order = 0;
439
- root.walkRules((rule) => {
440
- if (options.ignorePreflight !== false && (rule.selector.includes(":root") || rule.selector.includes("*") || rule.selector.includes("::"))) return;
441
- for (const selector of rule.selectors) for (const className of walkClasses(selector)) {
442
- if (allowed && !allowed.has(className)) continue;
443
- const compiled = compileRule(rule, className, variables, warnings, order++);
444
- if (compiled.length) (rules[className] ??= []).push(...compiled);
445
- }
446
- });
41
+ const compiled = (0, _weapp_tailwindcss_postcss_native.compileNativeCss)(css, options);
447
42
  return finalizeNativeManifest({
448
43
  version: 1,
449
- classSet: Object.keys(rules),
450
- rules,
451
- variables,
452
- warnings
44
+ classSet: Object.keys(compiled.rules),
45
+ ...compiled
453
46
  });
454
47
  }
455
48
  //#endregion
456
49
  exports.addNativeVariantRules = addNativeVariantRules;
457
- exports.baseClassName = baseClassName;
50
+ Object.defineProperty(exports, "baseClassName", {
51
+ enumerable: true,
52
+ get: function() {
53
+ return _weapp_tailwindcss_postcss_native.baseClassName;
54
+ }
55
+ });
458
56
  exports.compileNativeStylesheet = compileNativeStylesheet;
459
57
  exports.finalizeNativeManifest = finalizeNativeManifest;
@@ -1,8 +1,9 @@
1
- import { o as NativeStyleManifest, t as CompileNativeStylesheetOptions } from "./types-BTxnqaRV.js";
1
+ import { o as NativeStyleManifest, t as CompileNativeStylesheetOptions } from "./types-BKoPIarb.js";
2
+ import { baseClassName } from "@weapp-tailwindcss/postcss/native";
2
3
  //#region src/compiler.d.ts
3
- export declare function baseClassName(className: string): string | undefined;
4
4
  export declare function addNativeVariantRules(manifest: NativeStyleManifest, candidates: Iterable<string>): void;
5
5
  /** 为 manifest 生成稳定的 StyleSheet ID 和 Babel 静态 lookup。 */
6
6
  export declare function finalizeNativeManifest(manifest: NativeStyleManifest): NativeStyleManifest;
7
7
  export declare function compileNativeStylesheet(css: string, options?: CompileNativeStylesheetOptions): NativeStyleManifest;
8
- //#endregion
8
+ //#endregion
9
+ export { baseClassName };
package/dist/compiler.js CHANGED
@@ -1,333 +1,6 @@
1
1
  import { createHash } from "node:crypto";
2
- import postcss from "postcss";
2
+ import { baseClassName, compileNativeCss, splitClassName, variantForClass } from "@weapp-tailwindcss/postcss/native";
3
3
  //#region src/compiler.ts
4
- const CLASS_SELECTOR_RE = /\.((?:\\.|[^\s.#:[>+~])+)/g;
5
- const COLOR_PROPERTIES = /* @__PURE__ */ new Set([
6
- "color",
7
- "backgroundColor",
8
- "borderColor",
9
- "borderTopColor",
10
- "borderRightColor",
11
- "borderBottomColor",
12
- "borderLeftColor",
13
- "shadowColor",
14
- "textDecorationColor"
15
- ]);
16
- const NUMERIC_PROPERTIES = /* @__PURE__ */ new Set([
17
- "aspectRatio",
18
- "borderBottomWidth",
19
- "borderLeftWidth",
20
- "borderRadius",
21
- "borderRightWidth",
22
- "borderTopLeftRadius",
23
- "borderTopRightRadius",
24
- "borderTopWidth",
25
- "borderWidth",
26
- "bottom",
27
- "elevation",
28
- "flex",
29
- "flexBasis",
30
- "flexGrow",
31
- "flexShrink",
32
- "fontSize",
33
- "gap",
34
- "height",
35
- "left",
36
- "letterSpacing",
37
- "lineHeight",
38
- "margin",
39
- "marginBottom",
40
- "marginHorizontal",
41
- "marginLeft",
42
- "marginRight",
43
- "marginTop",
44
- "marginVertical",
45
- "maxHeight",
46
- "maxWidth",
47
- "minHeight",
48
- "minWidth",
49
- "opacity",
50
- "padding",
51
- "paddingBottom",
52
- "paddingHorizontal",
53
- "paddingLeft",
54
- "paddingRight",
55
- "paddingTop",
56
- "paddingVertical",
57
- "right",
58
- "top",
59
- "width",
60
- "zIndex"
61
- ]);
62
- const STRING_PROPERTIES = /* @__PURE__ */ new Set([
63
- "alignContent",
64
- "alignItems",
65
- "alignSelf",
66
- "borderStyle",
67
- "direction",
68
- "display",
69
- "flexDirection",
70
- "flexWrap",
71
- "fontFamily",
72
- "fontStyle",
73
- "fontWeight",
74
- "justifyContent",
75
- "overflow",
76
- "position",
77
- "textAlign",
78
- "textDecorationLine",
79
- "textDecorationStyle",
80
- "textTransform",
81
- "writingDirection"
82
- ]);
83
- const SHORTHANDS = {
84
- margin: [
85
- "marginTop",
86
- "marginRight",
87
- "marginBottom",
88
- "marginLeft"
89
- ],
90
- padding: [
91
- "paddingTop",
92
- "paddingRight",
93
- "paddingBottom",
94
- "paddingLeft"
95
- ]
96
- };
97
- const UNSUPPORTED_PROPERTIES = /* @__PURE__ */ new Set([
98
- "filter",
99
- "backdropFilter",
100
- "animation",
101
- "transition",
102
- "textShadow",
103
- "backgroundImage",
104
- "appearance",
105
- "content",
106
- "cursor",
107
- "userSelect",
108
- "whiteSpace",
109
- "objectFit",
110
- "listStyleType",
111
- "outline"
112
- ]);
113
- function decodeCssIdentifier(value) {
114
- return value.replace(/\\([0-9a-f]{1,6})\s?/gi, (_, code) => String.fromCodePoint(Number.parseInt(code, 16))).replace(/\\(.)/g, "$1");
115
- }
116
- function splitClassName(className) {
117
- const parts = [];
118
- let current = "";
119
- let bracketDepth = 0;
120
- let escaped = false;
121
- for (const character of className) {
122
- if (escaped) {
123
- current += character;
124
- escaped = false;
125
- continue;
126
- }
127
- if (character === "\\") {
128
- current += character;
129
- escaped = true;
130
- continue;
131
- }
132
- if (character === "[") bracketDepth += 1;
133
- if (character === "]") bracketDepth = Math.max(0, bracketDepth - 1);
134
- if (character === ":" && bracketDepth === 0) {
135
- parts.push(current);
136
- current = "";
137
- continue;
138
- }
139
- current += character;
140
- }
141
- parts.push(current);
142
- return parts;
143
- }
144
- function baseClassName(className) {
145
- return splitClassName(className).at(-1);
146
- }
147
- function unsupportedVariant(className) {
148
- return splitClassName(className).slice(0, -1).find((variant) => ![
149
- "dark",
150
- "ios",
151
- "android",
152
- "native"
153
- ].includes(variant));
154
- }
155
- function propertyName(property) {
156
- return {
157
- "padding-inline": "paddingHorizontal",
158
- "padding-inline-start": "paddingLeft",
159
- "padding-inline-end": "paddingRight",
160
- "padding-block": "paddingVertical",
161
- "padding-block-start": "paddingTop",
162
- "padding-block-end": "paddingBottom",
163
- "margin-inline": "marginHorizontal",
164
- "margin-inline-start": "marginLeft",
165
- "margin-inline-end": "marginRight",
166
- "margin-block": "marginVertical",
167
- "margin-block-start": "marginTop",
168
- "margin-block-end": "marginBottom"
169
- }[property] ?? property.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
170
- }
171
- function splitValue(value) {
172
- return value.trim().split(/\s+/).filter(Boolean);
173
- }
174
- function parseNumber(value, variables) {
175
- const resolved = resolveVariables(value, variables);
176
- const fraction = resolved.match(/^(-?(?:\d+\.\d+|\d+|\.\d+))\s*\/\s*(-?(?:\d+\.\d+|\d+|\.\d+))$/);
177
- if (fraction && Number(fraction[2]) !== 0) return Number(fraction[1]) / Number(fraction[2]);
178
- const calculated = resolved.match(/^calc\(\s*(-?(?:\d+\.\d+|\d+|\.\d+))(px|rem|em|%)?\s*\*\s*(-?(?:\d+\.\d+|\d+|\.\d+))\s*\)$/);
179
- if (calculated) {
180
- const base = parseNumber(`${calculated[1]}${calculated[2] ?? ""}`, variables);
181
- return typeof base === "number" ? base * Number(calculated[3]) : void 0;
182
- }
183
- const match = resolved.match(/^(-?(?:\d+\.\d+|\d+|\.\d+))(px|rem|em|%)?$/);
184
- if (!match) return;
185
- const number = Number(match[1]);
186
- if (match[2] === "%") return `${number}%`;
187
- if (match[2] === "rem" || match[2] === "em") return number * 16;
188
- return number;
189
- }
190
- function resolveVariables(value, variables) {
191
- return value.replace(/var\((--[\w-]+)(?:,\s*([^)]*))?\)/g, (_, key, fallback) => variables[key] ?? fallback ?? `var(${key})`);
192
- }
193
- function isColor(value) {
194
- return /^(?:#[\da-f]{3,8}|(?:rgb|rgba|hsl|hsla|oklch|oklab)\([^)]*\)|[a-z]+)$/i.test(value);
195
- }
196
- function oklchToHex(value) {
197
- const match = value.match(/^oklch\(\s*([\d.]+)%?\s+([\d.]+)\s+([\d.]+)(?:\s+\/\s*([\d.]+))?\s*\)$/i);
198
- if (!match) return;
199
- const lightness = Number(match[1]) > 1 ? Number(match[1]) / 100 : Number(match[1]);
200
- const chroma = Number(match[2]);
201
- const hue = Number(match[3]) * Math.PI / 180;
202
- const alpha = match[4] === void 0 ? 1 : Number(match[4]);
203
- const a = chroma * Math.cos(hue);
204
- const b = chroma * Math.sin(hue);
205
- const l = lightness + .3963377774 * a + .2158037573 * b;
206
- const m = lightness - .1055613458 * a - .0638541728 * b;
207
- const s = lightness - .0894841775 * a - 1.291485548 * b;
208
- const linear = (channel) => channel ** 3;
209
- const red = 4.0767416621 * linear(l) - 3.3077115913 * linear(m) + .2309699292 * linear(s);
210
- const green = -1.2684380046 * linear(l) + 2.6097574011 * linear(m) - .3413193965 * linear(s);
211
- const blue = -.0041960863 * linear(l) - .7034186147 * linear(m) + 1.707614701 * linear(s);
212
- const toByte = (channel) => Math.round(Math.max(0, Math.min(1, channel <= .0031308 ? 12.92 * channel : 1.055 * channel ** (1 / 2.4) - .055)) * 255);
213
- const hex = [
214
- red,
215
- green,
216
- blue
217
- ].map((channel) => toByte(channel).toString(16).padStart(2, "0")).join("");
218
- return alpha < 1 ? `#${hex}${Math.round(alpha * 255).toString(16).padStart(2, "0")}` : `#${hex}`;
219
- }
220
- function normalizeColor(value) {
221
- return /^oklch\(/i.test(value) ? oklchToHex(value) : value;
222
- }
223
- function parseTransform(value, variables) {
224
- const transform = [];
225
- for (const match of value.matchAll(/(translateX|translateY|translate|scaleX|scaleY|scale|rotate|skewX|skewY)\(([^)]*)\)/g)) {
226
- const name = match[1];
227
- const values = splitValue(resolveVariables(match[2] ?? "", variables).replace(",", " "));
228
- if (!name || !values.length) continue;
229
- if (name.startsWith("scale")) {
230
- const number = Number(values[0] ?? "");
231
- if (Number.isFinite(number)) transform.push({ [name]: number });
232
- continue;
233
- }
234
- if (name === "translate") {
235
- const x = parseNumber(values[0] ?? "", variables);
236
- const y = parseNumber(values[1] ?? "0", variables);
237
- if (x !== void 0 && y !== void 0) transform.push({ translateX: x }, { translateY: y });
238
- continue;
239
- }
240
- const property = name.replace(/^translate$/, "translateX");
241
- const parsed = name.startsWith("rotate") || name.startsWith("skew") ? values[0] : parseNumber(values[0] ?? "", variables);
242
- if (parsed !== void 0) transform.push({ [property]: parsed });
243
- }
244
- return transform.length ? transform : void 0;
245
- }
246
- function parseShadow(value, variables) {
247
- const resolved = resolveVariables(value, variables);
248
- const color = resolved.match(/(?:rgba?|hsla?)\([^)]*\)|#[\da-f]{3,8}/i)?.[0];
249
- const parts = splitValue(color ? resolved.replace(color, "") : resolved);
250
- const lengths = parts.filter((part) => parseNumber(part, variables) !== void 0).slice(0, 3);
251
- if (lengths.length < 2) return;
252
- const [x = "0", y = "0", blur = "0"] = lengths;
253
- const shadowColor = normalizeColor(color ?? parts.find((part) => isColor(part)) ?? "#000000") ?? "#000000";
254
- const opacity = /rgba\([^,]+,[^,]+,[^,]+,\s*([\d.]+)\)/i.exec(shadowColor)?.[1];
255
- return {
256
- shadowOffset: {
257
- width: parseNumber(x, variables),
258
- height: parseNumber(y, variables)
259
- },
260
- shadowRadius: parseNumber(blur, variables),
261
- shadowColor,
262
- ...opacity ? { shadowOpacity: Number(opacity) } : {}
263
- };
264
- }
265
- function parseValue(property, value, variables) {
266
- const trimmed = value.trim();
267
- if (UNSUPPORTED_PROPERTIES.has(property)) return;
268
- if (trimmed.includes("linear-gradient(") || trimmed.includes("url(")) return;
269
- if (COLOR_PROPERTIES.has(property)) {
270
- const color = resolveVariables(trimmed, variables);
271
- return isColor(color) ? normalizeColor(color) : void 0;
272
- }
273
- if (NUMERIC_PROPERTIES.has(property)) {
274
- const number = parseNumber(trimmed, variables);
275
- if (property === "opacity" && typeof number === "string" && number.endsWith("%")) return Number.parseFloat(number) / 100;
276
- return number;
277
- }
278
- if (property === "transform") return parseTransform(trimmed, variables);
279
- if (property === "boxShadow") return parseShadow(trimmed, variables);
280
- if (property === "display" && !["flex", "none"].includes(trimmed)) return;
281
- return STRING_PROPERTIES.has(property) ? trimmed : void 0;
282
- }
283
- function expandDeclaration(property, value, variables) {
284
- if (property === "margin" || property === "padding") {
285
- const parts = splitValue(value).map((item) => parseNumber(item, variables));
286
- if (parts.includes(void 0)) return;
287
- const [top, right = top, bottom = top, left = right] = parts;
288
- return Object.fromEntries(SHORTHANDS[property].map((key, index) => [key, [
289
- top,
290
- right,
291
- bottom,
292
- left
293
- ][index]]));
294
- }
295
- if (property === "border") {
296
- const width = splitValue(value).find((item) => /^\d/.test(item));
297
- const color = splitValue(value).find((item) => isColor(item));
298
- return {
299
- ...width ? { borderWidth: parseNumber(width, variables) } : {},
300
- ...color ? { borderColor: color } : {}
301
- };
302
- }
303
- if (property === "boxShadow") return parseShadow(value, variables);
304
- const parsed = parseValue(property, value, variables);
305
- return parsed === void 0 ? void 0 : { [property]: parsed };
306
- }
307
- function walkClasses(selector) {
308
- const classes = [];
309
- for (const match of selector.matchAll(CLASS_SELECTOR_RE)) {
310
- const token = decodeCssIdentifier(match[1]);
311
- if (token && !classes.includes(token)) classes.push(token);
312
- }
313
- return classes;
314
- }
315
- function variantForClass(className) {
316
- const result = {};
317
- for (const variant of splitClassName(className).slice(0, -1)) {
318
- if (variant === "dark") result.colorScheme = "dark";
319
- if (variant === "ios" || variant === "android" || variant === "native") result.platform = variant;
320
- }
321
- return result;
322
- }
323
- function atRuleVariant(node) {
324
- if (!node) return {};
325
- const params = node.params.toLowerCase();
326
- if (params.includes("prefers-color-scheme") && params.includes("dark")) return { colorScheme: "dark" };
327
- if (params.includes("platform") && params.includes("ios")) return { platform: "ios" };
328
- if (params.includes("platform") && params.includes("android")) return { platform: "android" };
329
- return {};
330
- }
331
4
  function addNativeVariantRules(manifest, candidates) {
332
5
  for (const candidate of candidates) {
333
6
  const parts = splitClassName(candidate);
@@ -344,69 +17,6 @@ function addNativeVariantRules(manifest, candidates) {
344
17
  manifest.classSet.push(candidate);
345
18
  }
346
19
  }
347
- function ancestors(node) {
348
- const result = [];
349
- let current = node.parent;
350
- while (current && current.type !== "root") {
351
- if (current.type === "atrule") result.unshift(current);
352
- current = current.parent;
353
- }
354
- return result;
355
- }
356
- function collectVariables(root) {
357
- const variables = {};
358
- root.walkDecls((decl) => {
359
- if (decl.prop.startsWith("--")) variables[decl.prop] = decl.value.trim();
360
- });
361
- return variables;
362
- }
363
- function addWarning(warnings, warning) {
364
- if (!warnings.some((item) => item.message === warning.message && item.property === warning.property && item.className === warning.className)) warnings.push(warning);
365
- }
366
- function compileRule(rule, className, variables, warnings, order) {
367
- const unsupported = unsupportedVariant(className);
368
- if (unsupported) {
369
- addWarning(warnings, {
370
- className,
371
- property: "variant",
372
- message: `不支持将 ${unsupported}: 变体编译为 React Native 条件样式`
373
- });
374
- return [];
375
- }
376
- const styles = {
377
- normal: {},
378
- important: {}
379
- };
380
- rule.walkDecls((decl) => {
381
- if (decl.prop.startsWith("--")) return;
382
- const property = propertyName(decl.prop);
383
- const important = decl.important || /!important\s*$/i.test(decl.value);
384
- const value = decl.value.replace(/\s*!important\s*$/i, "");
385
- const expanded = expandDeclaration(property, value, variables);
386
- if (!expanded || Object.values(expanded).includes(void 0)) {
387
- addWarning(warnings, {
388
- className,
389
- property,
390
- message: `不支持将 ${decl.prop}: ${value} 编译为 React Native style`
391
- });
392
- return;
393
- }
394
- Object.assign(styles[important ? "important" : "normal"], expanded);
395
- });
396
- const variant = {
397
- ...variantForClass(className),
398
- ...ancestors(rule).reduce((result, node) => ({
399
- ...result,
400
- ...atRuleVariant(node)
401
- }), {})
402
- };
403
- return ["normal", "important"].filter((kind) => Object.keys(styles[kind]).length > 0).map((kind) => ({
404
- style: styles[kind],
405
- ...variant,
406
- important: kind === "important" || void 0,
407
- order
408
- }));
409
- }
410
20
  /** 为 manifest 生成稳定的 StyleSheet ID 和 Babel 静态 lookup。 */
411
21
  function finalizeNativeManifest(manifest) {
412
22
  const styleSheet = {};
@@ -427,26 +37,11 @@ function finalizeNativeManifest(manifest) {
427
37
  return manifest;
428
38
  }
429
39
  function compileNativeStylesheet(css, options = {}) {
430
- const root = postcss.parse(css);
431
- const variables = collectVariables(root);
432
- const allowed = options.classSet ? new Set(options.classSet) : void 0;
433
- const rules = {};
434
- const warnings = [];
435
- let order = 0;
436
- root.walkRules((rule) => {
437
- if (options.ignorePreflight !== false && (rule.selector.includes(":root") || rule.selector.includes("*") || rule.selector.includes("::"))) return;
438
- for (const selector of rule.selectors) for (const className of walkClasses(selector)) {
439
- if (allowed && !allowed.has(className)) continue;
440
- const compiled = compileRule(rule, className, variables, warnings, order++);
441
- if (compiled.length) (rules[className] ??= []).push(...compiled);
442
- }
443
- });
40
+ const compiled = compileNativeCss(css, options);
444
41
  return finalizeNativeManifest({
445
42
  version: 1,
446
- classSet: Object.keys(rules),
447
- rules,
448
- variables,
449
- warnings
43
+ classSet: Object.keys(compiled.rules),
44
+ ...compiled
450
45
  });
451
46
  }
452
47
  //#endregion
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import weappReactNativeBabel from "./babel.js";
2
- import { a as NativeStyleEnvironment, c as NativeStyleRuntime, i as NativePlatform, l as NativeStyleValue, n as NativeClassValue, o as NativeStyleManifest, r as NativeCompilerWarning, s as NativeStyleRule, t as CompileNativeStylesheetOptions } from "./types-BTxnqaRV.js";
2
+ import { a as NativeStyleEnvironment, c as NativeStyleRuntime, i as NativePlatform, l as NativeStyleValue, n as NativeClassValue, o as NativeStyleManifest, r as NativeCompilerWarning, s as NativeStyleRule, t as CompileNativeStylesheetOptions } from "./types-BKoPIarb.js";
3
3
  import { compileNativeStylesheet } from "./compiler.js";
4
4
  import { VIRTUAL_MANIFEST_MODULE, getRegisteredManifest, getVirtualModuleCode, getVirtualModuleCodeAsync, withWeappTailwindcss } from "./metro.js";
5
5
  import { composeStyle, createNativeStyleRuntime, getManifest, getStaticStyle, setEnvironment, setManifest, setStyleSheetFactory, tw } from "./runtime.js";
package/dist/metro.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { o as NativeStyleManifest } from "./types-BTxnqaRV.js";
1
+ import { o as NativeStyleManifest } from "./types-BKoPIarb.js";
2
2
  //#region src/metro.d.ts
3
3
  export declare const VIRTUAL_MANIFEST_MODULE = "@weapp-tailwindcss/react-native/virtual";
4
4
  export interface MetroConfigLike {
package/dist/runtime.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { a as NativeStyleEnvironment, c as NativeStyleRuntime, i as NativePlatform, l as NativeStyleValue, n as NativeClassValue, o as NativeStyleManifest } from "./types-BTxnqaRV.js";
1
+ import { a as NativeStyleEnvironment, c as NativeStyleRuntime, i as NativePlatform, l as NativeStyleValue, n as NativeClassValue, o as NativeStyleManifest } from "./types-BKoPIarb.js";
2
2
  //#region src/runtime.d.ts
3
3
  interface StyleValue {
4
4
  [key: string]: unknown;
package/dist/tailwind.cjs CHANGED
@@ -4,6 +4,7 @@ const require_compiler = require("./compiler.cjs");
4
4
  let node_process = require("node:process");
5
5
  node_process = require_rolldown_runtime.__toESM(node_process, 1);
6
6
  let weapp_tailwindcss_generator = require("weapp-tailwindcss/generator");
7
+ let _weapp_tailwindcss_postcss_native = require("@weapp-tailwindcss/postcss/native");
7
8
  //#region src/tailwind.ts
8
9
  /**
9
10
  * 使用 weapp-tailwindcss 的 Tailwind v4 generator 生成原始 CSS,再编译为 RN manifest。
@@ -21,7 +22,7 @@ async function generateNativeStylesheet(options = {}) {
21
22
  }));
22
23
  const generatorCandidates = new Set(options.candidates ?? []);
23
24
  for (const candidate of generatorCandidates) {
24
- const base = require_compiler.baseClassName(candidate);
25
+ const base = (0, _weapp_tailwindcss_postcss_native.baseClassName)(candidate);
25
26
  if (base && /^(?:ios|android|native):/.test(candidate)) generatorCandidates.add(base);
26
27
  }
27
28
  let generated = await generator.generate({
@@ -31,7 +32,7 @@ async function generateNativeStylesheet(options = {}) {
31
32
  });
32
33
  const platformBases = /* @__PURE__ */ new Set();
33
34
  for (const candidate of generated.rawCandidates ?? []) {
34
- const base = require_compiler.baseClassName(candidate);
35
+ const base = (0, _weapp_tailwindcss_postcss_native.baseClassName)(candidate);
35
36
  if (base && /^(?:ios|android|native):/.test(candidate) && !generated.classSet.has(base)) platformBases.add(base);
36
37
  }
37
38
  if (platformBases.size) generated = await generator.generate({
@@ -42,7 +43,7 @@ async function generateNativeStylesheet(options = {}) {
42
43
  const classSet = new Set(generated.classSet);
43
44
  const requestedCandidates = new Set(options.candidates ?? []);
44
45
  for (const candidate of generated.rawCandidates ?? []) {
45
- const base = require_compiler.baseClassName(candidate);
46
+ const base = (0, _weapp_tailwindcss_postcss_native.baseClassName)(candidate);
46
47
  if (base && generated.classSet.has(base) && /^(?:dark|ios|android|native):/.test(candidate)) requestedCandidates.add(candidate);
47
48
  }
48
49
  for (const candidate of requestedCandidates) classSet.add(candidate);
@@ -1,4 +1,4 @@
1
- import { o as NativeStyleManifest } from "./types-BTxnqaRV.js";
1
+ import { o as NativeStyleManifest } from "./types-BKoPIarb.js";
2
2
  import { TailwindV4SourceOptions } from "weapp-tailwindcss/generator";
3
3
  //#region src/tailwind.d.ts
4
4
  export interface GenerateNativeStylesheetOptions extends TailwindV4SourceOptions {
@@ -1,22 +1,9 @@
1
+ import { CompileNativeStylesheetOptions, NativeCompilerWarning, NativeCompilerWarning as NativeCompilerWarning$1, NativePlatform, NativePlatform as NativePlatform$1, NativeStyleRule, NativeStyleRule as NativeStyleRule$1 } from "@weapp-tailwindcss/postcss/native";
1
2
  //#region src/types.d.ts
2
- type NativePlatform = 'android' | 'ios' | 'native' | 'web';
3
3
  interface NativeStyleEnvironment {
4
4
  colorScheme?: 'light' | 'dark' | undefined;
5
5
  platform?: NativePlatform | undefined;
6
6
  }
7
- interface NativeStyleRule {
8
- style: Record<string, unknown>;
9
- colorScheme?: 'dark' | undefined;
10
- platform?: NativePlatform | undefined;
11
- important?: boolean | undefined;
12
- order?: number | undefined;
13
- id?: string | undefined;
14
- }
15
- interface NativeCompilerWarning {
16
- className?: string | undefined;
17
- property?: string | undefined;
18
- message: string;
19
- }
20
7
  interface NativeStyleManifest {
21
8
  version: 1;
22
9
  classSet: string[];
@@ -30,10 +17,6 @@ interface NativeStyleManifest {
30
17
  variables: Record<string, string>;
31
18
  warnings: NativeCompilerWarning[];
32
19
  }
33
- interface CompileNativeStylesheetOptions {
34
- classSet?: Iterable<string> | undefined;
35
- ignorePreflight?: boolean | undefined;
36
- }
37
20
  type NativeClassValue = string | false | null | undefined | NativeClassValue[] | Record<string, boolean>;
38
21
  type NativeStyleValue = Record<string, unknown> | number | readonly NativeStyleValue[];
39
22
  interface NativeStyleRuntime {
@@ -45,4 +28,4 @@ interface NativeStyleRuntime {
45
28
  getManifest: () => NativeStyleManifest | undefined;
46
29
  }
47
30
  //#endregion
48
- export { NativeStyleEnvironment as a, NativeStyleRuntime as c, NativePlatform as i, NativeStyleValue as l, NativeClassValue as n, NativeStyleManifest as o, NativeCompilerWarning as r, NativeStyleRule as s, CompileNativeStylesheetOptions as t };
31
+ export { NativeStyleEnvironment as a, NativeStyleRuntime as c, NativePlatform$1 as i, NativeStyleValue as l, NativeClassValue as n, NativeStyleManifest as o, NativeCompilerWarning$1 as r, NativeStyleRule$1 as s, CompileNativeStylesheetOptions as t };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-tailwindcss/react-native",
3
3
  "type": "module",
4
- "version": "0.2.18",
4
+ "version": "0.2.19",
5
5
  "description": "Tailwind CSS compiler and Expo Metro integration for cross-platform React Native apps. 面向跨端 React Native 应用的 Tailwind CSS 编译器与 Expo Metro 集成。",
6
6
  "license": "MIT",
7
7
  "homepage": "https://tw.weapp.dev/docs/quick-start/react-native-expo",
@@ -92,8 +92,8 @@
92
92
  },
93
93
  "dependencies": {
94
94
  "@babel/types": "^8.0.5",
95
- "postcss": "^8.5.28",
96
- "weapp-tailwindcss": "5.5.6"
95
+ "@weapp-tailwindcss/postcss": "3.3.6",
96
+ "weapp-tailwindcss": "5.5.7"
97
97
  },
98
98
  "devDependencies": {
99
99
  "@babel/core": "^8.0.5",