@weapp-tailwindcss/react-native 0.3.4 → 0.3.6

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.
Files changed (50) hide show
  1. package/README.md +14 -0
  2. package/README.zh-CN.md +14 -0
  3. package/dist/babel.cjs +2 -3
  4. package/dist/babel.d.cts +6 -0
  5. package/dist/babel.d.ts +2 -3
  6. package/dist/babel.js +1 -2
  7. package/dist/compiler.cjs +12 -2
  8. package/dist/compiler.d.cts +7 -0
  9. package/dist/compiler.d.ts +1 -3
  10. package/dist/compiler.js +13 -3
  11. package/dist/env.d.cts +10 -0
  12. package/dist/env.d.ts +1 -3
  13. package/dist/index.cjs +7 -3
  14. package/dist/index.d.cts +7 -0
  15. package/dist/index.d.ts +5 -5
  16. package/dist/index.js +4 -4
  17. package/dist/manifest-DRxbQp2v.cjs +47 -0
  18. package/dist/manifest-DuhmZ2T9.js +24 -0
  19. package/dist/metro-BdsKKvRn.d.cts +63 -0
  20. package/dist/{metro-BZbdIyTH.js → metro-OMh6z1jy.js} +216 -97
  21. package/dist/metro-ahmChgsA.d.ts +63 -0
  22. package/dist/{metro-Ds2ZUc6_.cjs → metro-kaKzXOAU.cjs} +238 -101
  23. package/dist/metro-transformer.cjs +5 -11
  24. package/dist/metro-transformer.d.cts +2 -0
  25. package/dist/metro-transformer.d.ts +1 -3
  26. package/dist/metro-transformer.js +4 -10
  27. package/dist/metro.cjs +4 -1
  28. package/dist/metro.d.cts +2 -0
  29. package/dist/metro.d.ts +2 -53
  30. package/dist/metro.js +1 -2
  31. package/dist/{rolldown-runtime-VH7oDXx4.cjs → rolldown-runtime-CmQyd0Gu.cjs} +1 -2
  32. package/dist/runtime-CbkbHrPb.cjs +383 -0
  33. package/dist/runtime-D4ZGX6yk.d.cts +42 -0
  34. package/dist/runtime-DuvYZu4P.js +330 -0
  35. package/dist/{runtime-B3hAisFx.d.ts → runtime-YHlnn5ip.d.ts} +20 -12
  36. package/dist/runtime.cjs +10 -186
  37. package/dist/runtime.d.cts +3 -0
  38. package/dist/runtime.d.ts +3 -3
  39. package/dist/runtime.js +1 -179
  40. package/dist/tailwind-4T0v_pRK.d.ts +13 -0
  41. package/dist/tailwind-B1Gl9P_t.js +121 -0
  42. package/dist/tailwind-CVspvWID.cjs +129 -0
  43. package/dist/tailwind-CfKLPJuW.d.cts +13 -0
  44. package/dist/tailwind.cjs +2 -58
  45. package/dist/tailwind.d.cts +2 -0
  46. package/dist/tailwind.d.ts +2 -16
  47. package/dist/tailwind.js +1 -55
  48. package/dist/{types-CKAFTXX9.d.ts → types-CxNMLBiF.d.cts} +6 -3
  49. package/dist/types-CxNMLBiF.d.ts +20 -0
  50. package/package.json +75 -33
@@ -0,0 +1,330 @@
1
+ import { n as copyPublication, t as copyManifestSnapshot } from "./manifest-DuhmZ2T9.js";
2
+ import { resolveNativeBoxShadow, resolveNativeTransform } from "@weapp-tailwindcss/postcss/native-runtime";
3
+ // src/runtime/line-height.ts
4
+ function flattenInlineStyle(value, resolveRegistered) {
5
+ const style = {};
6
+ const active = /* @__PURE__ */ new Set();
7
+ const pending = [{ value }];
8
+ while (pending.length) {
9
+ const current = pending.pop();
10
+ if (Array.isArray(current.value)) {
11
+ if (current.exit) {
12
+ active.delete(current.value);
13
+ continue;
14
+ }
15
+ if (active.has(current.value)) throw new Error("内联 style 数组包含循环引用");
16
+ active.add(current.value);
17
+ pending.push({
18
+ value: current.value,
19
+ exit: true
20
+ });
21
+ for (let index = current.value.length - 1; index >= 0; index--) pending.push({ value: current.value[index] });
22
+ } else if (current.value && typeof current.value === "object") Object.assign(style, current.value);
23
+ else if (typeof current.value === "number") Object.assign(style, resolveRegistered?.(current.value));
24
+ }
25
+ return style;
26
+ }
27
+ /** 字号级联完成后再计算;继承字号未知时不猜测平台默认值。 */
28
+ function relativeLineHeightStyle(relative, inline) {
29
+ if (!relative) return;
30
+ const style = flattenInlineStyle(inline);
31
+ if (style.lineHeight !== void 0 && !relative.important) return;
32
+ const fontSize = relative.importantFontSize ?? style.fontSize ?? relative.fontSize;
33
+ return typeof fontSize === "number" && Number.isFinite(fontSize) && fontSize >= 0 ? { lineHeight: fontSize * relative.multiplier } : void 0;
34
+ }
35
+ // src/runtime/shadow.ts
36
+ /** 阴影颜色依赖最终文字颜色;继承上下文未知时保留元数据,不猜测黑色。 */
37
+ function deferredBoxShadowStyle(context, inline) {
38
+ const style = flattenInlineStyle(inline);
39
+ if (!context.important && "boxShadow" in style) return;
40
+ const color = context.importantColor ?? (typeof style.color === "string" ? style.color : context.color);
41
+ const computed = resolveNativeBoxShadow(context.expression, context.bindings, color);
42
+ if (computed) return { boxShadow: computed };
43
+ if (!color && resolveNativeBoxShadow(context.expression, context.bindings, "#000000")) return;
44
+ throw new Error(`无法将当前 class 组合的 box-shadow 编译为原生阴影:${context.expression.value}`);
45
+ }
46
+ function inlineBoxShadowStyle(context, inline) {
47
+ if (!context || typeof flattenInlineStyle(inline).color !== "string") return;
48
+ return deferredBoxShadowStyle(context, inline);
49
+ }
50
+ // src/runtime/resolve.ts
51
+ function matches(rule, environment) {
52
+ if (rule.colorScheme && rule.colorScheme !== environment.colorScheme) return false;
53
+ if (!rule.platform) return true;
54
+ if (rule.platform === "native") return environment.platform !== "web";
55
+ return rule.platform === environment.platform;
56
+ }
57
+ function resolveRules(ids, manifest, environment, nativeStyleSheet) {
58
+ const normal = [];
59
+ const important = [];
60
+ for (const id of ids) {
61
+ const rule = manifest?.styleEntries?.[id];
62
+ if (rule) {
63
+ if (!matches(rule, environment)) continue;
64
+ (rule.important ? important : normal).push({
65
+ rule,
66
+ id
67
+ });
68
+ continue;
69
+ }
70
+ const fallbackIds = manifest?.staticLookup?.[id] ?? [];
71
+ if (fallbackIds.length) for (const fallbackId of fallbackIds) {
72
+ const fallbackRule = manifest?.styleEntries?.[fallbackId];
73
+ if (!fallbackRule || !matches(fallbackRule, environment)) continue;
74
+ (fallbackRule.important ? important : normal).push({
75
+ rule: fallbackRule,
76
+ id: fallbackId
77
+ });
78
+ }
79
+ else for (const fallbackRule of manifest?.rules?.[id] ?? []) {
80
+ if (!matches(fallbackRule, environment)) continue;
81
+ (fallbackRule.important ? important : normal).push({
82
+ rule: fallbackRule,
83
+ id
84
+ });
85
+ }
86
+ }
87
+ const order = (left, right) => (left.rule.order ?? 0) - (right.rule.order ?? 0);
88
+ normal.sort(order);
89
+ important.sort(order);
90
+ const customProperties = Object.assign({}, ...[...normal, ...important].map(({ rule }) => rule.customProperties));
91
+ const style = {};
92
+ const importantStyle = {};
93
+ let relative;
94
+ let boxShadow;
95
+ let transform;
96
+ const apply = (rule, isImportant) => {
97
+ Object.assign(style, rule.style);
98
+ if (isImportant) Object.assign(importantStyle, rule.style);
99
+ if (rule.lineHeightMultiplier !== void 0) {
100
+ relative = {
101
+ multiplier: rule.lineHeightMultiplier,
102
+ important: isImportant
103
+ };
104
+ delete style.lineHeight;
105
+ if (isImportant) delete importantStyle.lineHeight;
106
+ } else if ("lineHeight" in rule.style) relative = void 0;
107
+ if (rule.boxShadowExpression) {
108
+ boxShadow = {
109
+ expression: rule.boxShadowExpression,
110
+ bindings: customProperties,
111
+ important: isImportant
112
+ };
113
+ delete style.boxShadow;
114
+ if (isImportant) delete importantStyle.boxShadow;
115
+ } else if ("boxShadow" in rule.style) boxShadow = void 0;
116
+ if (rule.transformExpression) transform = {
117
+ expression: rule.transformExpression,
118
+ important: isImportant
119
+ };
120
+ else if ("transform" in rule.style) transform = void 0;
121
+ };
122
+ for (const { rule } of normal) apply(rule, false);
123
+ for (const { rule } of important) apply(rule, true);
124
+ if (transform) {
125
+ const computed = resolveNativeTransform(transform.expression, customProperties);
126
+ if (!computed) throw new Error(`无法将当前 class 组合的 transform 编译为原生变换:${transform.expression.value}`);
127
+ style.transform = computed;
128
+ if (transform.important) importantStyle.transform = computed;
129
+ }
130
+ if (boxShadow) {
131
+ boxShadow.color = typeof style.color === "string" ? style.color : environment.color;
132
+ boxShadow.importantColor = typeof importantStyle.color === "string" ? importantStyle.color : void 0;
133
+ const computed = deferredBoxShadowStyle(boxShadow);
134
+ if (computed) {
135
+ Object.assign(style, computed);
136
+ if (boxShadow.important) Object.assign(importantStyle, computed);
137
+ }
138
+ }
139
+ if (relative) {
140
+ relative.fontSize = typeof style.fontSize === "number" ? style.fontSize : environment.fontSize;
141
+ relative.importantFontSize = typeof importantStyle.fontSize === "number" ? importantStyle.fontSize : void 0;
142
+ const computed = relativeLineHeightStyle(relative);
143
+ if (computed) {
144
+ Object.assign(style, computed);
145
+ if (relative.important) Object.assign(importantStyle, computed);
146
+ }
147
+ }
148
+ const normalIds = normal.map((item) => item.id);
149
+ const importantIds = important.map((item) => item.id);
150
+ const asStyleValue = (ids, fallback) => {
151
+ if (relative || boxShadow || transform) return fallback;
152
+ if (!nativeStyleSheet) return fallback;
153
+ const values = ids.map((id) => nativeStyleSheet[id]).filter((value) => value !== void 0);
154
+ if (values.length === 1) return values[0];
155
+ if (values.length > 1) return values;
156
+ return fallback;
157
+ };
158
+ return {
159
+ style,
160
+ importantStyle,
161
+ styleValue: asStyleValue([...normalIds, ...importantIds], style),
162
+ importantValue: asStyleValue(importantIds, importantStyle),
163
+ relativeLineHeight: relative,
164
+ boxShadow
165
+ };
166
+ }
167
+ // src/runtime.ts
168
+ let activeEnvironment = {};
169
+ let styleSheetFactory;
170
+ let styleSheetFlattener;
171
+ let styleSheetRevision = 0;
172
+ function resolveRegisteredStyle(value) {
173
+ const resolved = styleSheetFlattener?.(value);
174
+ return resolved && typeof resolved === "object" && !Array.isArray(resolved) ? resolved : void 0;
175
+ }
176
+ function createStyleSheet(manifest) {
177
+ const revision = styleSheetRevision;
178
+ const factory = styleSheetFactory;
179
+ const source = manifest.styleSheet ?? Object.fromEntries(Object.entries(manifest.styleEntries ?? {}).map(([id, rule]) => [id, rule.style]));
180
+ return {
181
+ revision,
182
+ styles: factory?.(JSON.parse(JSON.stringify(source)))
183
+ };
184
+ }
185
+ function flatten(value, result) {
186
+ if (!value) return;
187
+ if (typeof value === "string") {
188
+ result.push(...value.split(/\s+/).filter(Boolean));
189
+ return;
190
+ }
191
+ if (Array.isArray(value)) {
192
+ for (const item of value) flatten(item, result);
193
+ return;
194
+ }
195
+ for (const [className, enabled] of Object.entries(value)) if (enabled) result.push(className);
196
+ }
197
+ function canonicalTokens(value) {
198
+ const tokens = [];
199
+ flatten(value, tokens);
200
+ return [...new Set(tokens)].sort();
201
+ }
202
+ function resolveDynamic(value, manifest, environment, nativeStyleSheet) {
203
+ const classNames = canonicalTokens(value);
204
+ return resolveRules(manifest?.staticLookup ? classNames.flatMap((className) => manifest.staticLookup?.[className] ?? []) : classNames, manifest, environment, nativeStyleSheet);
205
+ }
206
+ function rememberStyle(styleMetadata, style, importantStyle, relativeLineHeight, boxShadow) {
207
+ const important = importantStyle && (typeof importantStyle !== "object" || Object.keys(importantStyle).length) ? importantStyle : void 0;
208
+ if (important !== void 0 || relativeLineHeight || boxShadow) styleMetadata.set(style, {
209
+ important,
210
+ relativeLineHeight,
211
+ boxShadow
212
+ });
213
+ return style;
214
+ }
215
+ function createNativeStyleRuntime(initialManifest, initialPublication) {
216
+ if (!initialManifest && initialPublication) throw new Error("React Native 发布身份必须绑定 manifest");
217
+ let manifest = initialManifest ? copyManifestSnapshot(initialManifest) : void 0;
218
+ let publication = copyPublication(initialPublication);
219
+ let environment = activeEnvironment;
220
+ const cache = /* @__PURE__ */ new Map();
221
+ const styleMetadata = /* @__PURE__ */ new Map();
222
+ const initial = manifest ? createStyleSheet(manifest) : void 0;
223
+ let nativeStyleSheet = initial?.styles;
224
+ let revision = initial?.revision ?? styleSheetRevision;
225
+ const ensureStyleSheet = () => {
226
+ if (revision !== styleSheetRevision) {
227
+ const prepared = manifest ? createStyleSheet(manifest) : void 0;
228
+ nativeStyleSheet = prepared?.styles;
229
+ revision = prepared?.revision ?? styleSheetRevision;
230
+ cache.clear();
231
+ }
232
+ };
233
+ return {
234
+ tw(value, requestedEnvironment = {}) {
235
+ ensureStyleSheet();
236
+ const effectiveEnvironment = {
237
+ ...environment,
238
+ ...requestedEnvironment
239
+ };
240
+ const key = `${canonicalTokens(value).join("")}|${effectiveEnvironment.colorScheme ?? ""}|${effectiveEnvironment.platform ?? ""}|${effectiveEnvironment.fontSize ?? ""}|${effectiveEnvironment.color ?? ""}`;
241
+ const cached = cache.get(key);
242
+ if (cached) return cached;
243
+ const resolved = resolveDynamic(value, manifest, effectiveEnvironment, nativeStyleSheet);
244
+ const style = rememberStyle(styleMetadata, resolved.styleValue, resolved.importantValue, resolved.relativeLineHeight, resolved.boxShadow);
245
+ cache.set(key, style);
246
+ return style;
247
+ },
248
+ getStaticStyle(ids, requestedEnvironment = {}) {
249
+ ensureStyleSheet();
250
+ const effectiveEnvironment = {
251
+ ...environment,
252
+ ...requestedEnvironment
253
+ };
254
+ const resolved = resolveRules(ids, manifest, effectiveEnvironment, nativeStyleSheet);
255
+ return rememberStyle(styleMetadata, resolved.styleValue, resolved.importantValue, resolved.relativeLineHeight, resolved.boxShadow);
256
+ },
257
+ composeStyle(tailwindStyle, inlineStyle) {
258
+ const metadata = styleMetadata.get(tailwindStyle);
259
+ const important = metadata?.important;
260
+ const inline = flattenInlineStyle(inlineStyle, resolveRegisteredStyle);
261
+ const relative = relativeLineHeightStyle(metadata?.relativeLineHeight, inline);
262
+ const boxShadow = inlineBoxShadowStyle(metadata?.boxShadow, inline);
263
+ if (relative || boxShadow) return [
264
+ tailwindStyle,
265
+ inlineStyle,
266
+ {
267
+ ...flattenInlineStyle(important, resolveRegisteredStyle),
268
+ ...relative,
269
+ ...boxShadow
270
+ }
271
+ ];
272
+ return important !== void 0 ? [
273
+ tailwindStyle,
274
+ inlineStyle,
275
+ important
276
+ ] : [tailwindStyle, inlineStyle];
277
+ },
278
+ setManifest(nextManifest, nextPublication) {
279
+ const preparedManifest = copyManifestSnapshot(nextManifest);
280
+ const preparedPublication = copyPublication(nextPublication);
281
+ const preparedStyleSheet = createStyleSheet(preparedManifest);
282
+ manifest = preparedManifest;
283
+ publication = preparedPublication;
284
+ nativeStyleSheet = preparedStyleSheet.styles;
285
+ revision = preparedStyleSheet.revision;
286
+ cache.clear();
287
+ styleMetadata.clear();
288
+ },
289
+ setEnvironment(nextEnvironment) {
290
+ environment = nextEnvironment;
291
+ activeEnvironment = nextEnvironment;
292
+ cache.clear();
293
+ },
294
+ getManifest() {
295
+ return manifest;
296
+ },
297
+ getPublication() {
298
+ return publication;
299
+ }
300
+ };
301
+ }
302
+ const defaultRuntime = createNativeStyleRuntime();
303
+ function setManifest(manifest, publication) {
304
+ defaultRuntime.setManifest(manifest, publication);
305
+ }
306
+ /** 由 Expo virtual module 注入,避免 runtime 引入 Node-only 的 require shim。 */
307
+ function setStyleSheetFactory(factory, flatten) {
308
+ styleSheetFactory = factory;
309
+ styleSheetFlattener = flatten;
310
+ styleSheetRevision += 1;
311
+ }
312
+ function setEnvironment(environment) {
313
+ defaultRuntime.setEnvironment(environment);
314
+ }
315
+ function getManifest() {
316
+ return defaultRuntime.getManifest();
317
+ }
318
+ function getPublication() {
319
+ return defaultRuntime.getPublication();
320
+ }
321
+ function tw(value, environment) {
322
+ return defaultRuntime.tw(value, environment);
323
+ }
324
+ function getStaticStyle(ids, environment) {
325
+ return defaultRuntime.getStaticStyle(ids, environment);
326
+ }
327
+ function composeStyle(tailwindStyle, inlineStyle) {
328
+ return defaultRuntime.composeStyle(tailwindStyle, inlineStyle);
329
+ }
330
+ export { getStaticStyle as a, setStyleSheetFactory as c, getPublication as i, tw as l, createNativeStyleRuntime as n, setEnvironment as o, getManifest as r, setManifest as s, composeStyle as t };
@@ -1,34 +1,42 @@
1
- import { i as NativeStyleManifest, r as NativePlatform } from "./types-CKAFTXX9.js";
2
- import { StyleProp } from "react-native";
3
- //#region src/runtime/types.d.ts
1
+ import { a as NativeStyleManifest, i as NativePlatform, r as NativeManifestPublication } from "./types-CxNMLBiF.js";
2
+ type FalsyStyle = undefined | null | false | '';
3
+ type RegisteredStyle<T> = number & {
4
+ __registeredStyleBrand: T;
5
+ };
6
+ interface RecursiveStyleArray<T> extends Array<T | ReadonlyArray<T> | RecursiveStyleArray<T>> {}
7
+ type StyleRecord = Record<string, unknown>;
4
8
  interface NativeStyleEnvironment {
5
9
  colorScheme?: 'light' | 'dark' | undefined;
6
10
  platform?: NativePlatform | undefined;
11
+ /** 未在当前元素指定字号时,调用方可显式传入已知的继承字号。 */
12
+ fontSize?: number | undefined;
13
+ /** 当前元素没有文字颜色时,调用方可显式提供已知的继承颜色。 */
14
+ color?: string | undefined;
7
15
  }
8
16
  type NativeClassValue = string | false | null | undefined | NativeClassValue[] | Record<string, boolean>;
9
- type NativeStyleValue = StyleProp<Record<string, unknown>>;
17
+ type NativeStyleValue = StyleRecord | RegisteredStyle<StyleRecord> | RecursiveStyleArray<StyleRecord | RegisteredStyle<StyleRecord> | FalsyStyle> | FalsyStyle;
10
18
  interface NativeStyleRuntime {
11
19
  tw: (value: NativeClassValue, environment?: NativeStyleEnvironment) => NativeStyleValue;
12
20
  getStaticStyle: (ids: readonly string[], environment?: NativeStyleEnvironment) => NativeStyleValue;
13
21
  composeStyle: <const InlineStyle>(tailwindStyle: NativeStyleValue, inlineStyle: InlineStyle) => [NativeStyleValue, InlineStyle] | [NativeStyleValue, InlineStyle, NativeStyleValue];
14
- setManifest: (manifest: NativeStyleManifest) => void;
22
+ setManifest: (manifest: NativeStyleManifest, publication?: NativeManifestPublication) => void;
15
23
  setEnvironment: (environment: NativeStyleEnvironment) => void;
16
24
  getManifest: () => NativeStyleManifest | undefined;
25
+ getPublication: () => NativeManifestPublication | undefined;
17
26
  }
18
- //#endregion
19
- //#region src/runtime.d.ts
20
27
  interface StyleValue {
21
28
  [key: string]: unknown;
22
29
  }
23
30
  type StyleSheetFactory = (value: Record<string, StyleValue>) => Record<string, NativeStyleValue>;
24
- declare function createNativeStyleRuntime(initialManifest?: NativeStyleManifest): NativeStyleRuntime;
25
- declare function setManifest(manifest: NativeStyleManifest): void;
31
+ type StyleSheetFlattener = (value: NativeStyleValue) => unknown;
32
+ declare function createNativeStyleRuntime(initialManifest?: NativeStyleManifest, initialPublication?: NativeManifestPublication): NativeStyleRuntime;
33
+ declare function setManifest(manifest: NativeStyleManifest, publication?: NativeManifestPublication): void;
26
34
  /** 由 Expo virtual module 注入,避免 runtime 引入 Node-only 的 require shim。 */
27
- declare function setStyleSheetFactory(factory: StyleSheetFactory): void;
35
+ declare function setStyleSheetFactory(factory: StyleSheetFactory, flatten?: StyleSheetFlattener): void;
28
36
  declare function setEnvironment(environment: NativeStyleEnvironment): void;
29
37
  declare function getManifest(): NativeStyleManifest | undefined;
38
+ declare function getPublication(): NativeManifestPublication | undefined;
30
39
  declare function tw(value: NativeClassValue, environment?: NativeStyleEnvironment): NativeStyleValue;
31
40
  declare function getStaticStyle(ids: readonly string[], environment?: NativeStyleEnvironment): NativeStyleValue;
32
41
  declare function composeStyle<const InlineStyle>(tailwindStyle: NativeStyleValue, inlineStyle: InlineStyle): [NativeStyleValue, InlineStyle] | [NativeStyleValue, InlineStyle, NativeStyleValue];
33
- //#endregion
34
- export { setEnvironment as a, tw as c, NativeStyleRuntime as d, NativeStyleValue as f, getStaticStyle as i, NativeClassValue as l, createNativeStyleRuntime as n, setManifest as o, getManifest as r, setStyleSheetFactory as s, composeStyle as t, NativeStyleEnvironment as u };
42
+ export { getStaticStyle as a, setStyleSheetFactory as c, NativeStyleEnvironment as d, NativeStyleRuntime as f, getPublication as i, tw as l, createNativeStyleRuntime as n, setEnvironment as o, NativeStyleValue as p, getManifest as r, setManifest as s, composeStyle as t, NativeClassValue as u };
package/dist/runtime.cjs CHANGED
@@ -1,187 +1,11 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- //#region src/runtime.ts
3
- let activeManifest;
4
- let activeEnvironment = {};
5
- const styleMetadata = /* @__PURE__ */ new Map();
6
- let styleSheetFactory;
7
- let nativeStyleSheet = {};
8
- function createStyleSheet(manifest) {
9
- const source = manifest.styleSheet ?? Object.fromEntries(Object.entries(manifest.styleEntries ?? {}).map(([id, rule]) => [id, rule.style]));
10
- nativeStyleSheet = styleSheetFactory?.(source) ?? source;
11
- }
12
- function flatten(value, result) {
13
- if (!value) return;
14
- if (typeof value === "string") {
15
- result.push(...value.split(/\s+/).filter(Boolean));
16
- return;
17
- }
18
- if (Array.isArray(value)) {
19
- for (const item of value) flatten(item, result);
20
- return;
21
- }
22
- for (const [className, enabled] of Object.entries(value)) if (enabled) result.push(className);
23
- }
24
- function canonicalTokens(value) {
25
- const tokens = [];
26
- flatten(value, tokens);
27
- return [...new Set(tokens)].sort();
28
- }
29
- function matches(rule, environment) {
30
- if (rule.colorScheme && rule.colorScheme !== environment.colorScheme) return false;
31
- if (!rule.platform) return true;
32
- if (rule.platform === "native") return environment.platform !== "web";
33
- return rule.platform === environment.platform;
34
- }
35
- function resolveRules(ids, manifest, environment) {
36
- const normal = [];
37
- const important = [];
38
- for (const id of ids) {
39
- const rule = manifest?.styleEntries?.[id];
40
- if (rule) {
41
- if (!matches(rule, environment)) continue;
42
- (rule.important ? important : normal).push({
43
- rule,
44
- id
45
- });
46
- continue;
47
- }
48
- const fallbackIds = manifest?.staticLookup?.[id] ?? [];
49
- if (fallbackIds.length) for (const fallbackId of fallbackIds) {
50
- const fallbackRule = manifest?.styleEntries?.[fallbackId];
51
- if (!fallbackRule || !matches(fallbackRule, environment)) continue;
52
- (fallbackRule.important ? important : normal).push({
53
- rule: fallbackRule,
54
- id: fallbackId
55
- });
56
- }
57
- else for (const fallbackRule of manifest?.rules?.[id] ?? []) {
58
- if (!matches(fallbackRule, environment)) continue;
59
- (fallbackRule.important ? important : normal).push({
60
- rule: fallbackRule,
61
- id
62
- });
63
- }
64
- }
65
- const order = (left, right) => (left.rule.order ?? 0) - (right.rule.order ?? 0);
66
- normal.sort(order);
67
- important.sort(order);
68
- const style = {};
69
- const importantStyle = {};
70
- for (const { rule } of normal) Object.assign(style, rule.style);
71
- for (const { rule } of important) {
72
- Object.assign(style, rule.style);
73
- Object.assign(importantStyle, rule.style);
74
- }
75
- const normalIds = normal.map((item) => item.id);
76
- const importantIds = important.map((item) => item.id);
77
- const asStyleValue = (ids, fallback) => {
78
- if (!styleSheetFactory) return fallback;
79
- const values = ids.map((id) => nativeStyleSheet[id]).filter((value) => value !== void 0);
80
- if (values.length === 1) return values[0];
81
- if (values.length > 1) return values;
82
- return fallback;
83
- };
84
- return {
85
- style,
86
- importantStyle,
87
- styleValue: asStyleValue(normalIds, style),
88
- importantValue: asStyleValue(importantIds, importantStyle)
89
- };
90
- }
91
- function resolveDynamic(value, manifest, environment) {
92
- const classNames = canonicalTokens(value);
93
- return resolveRules(manifest?.staticLookup ? classNames.flatMap((className) => manifest.staticLookup?.[className] ?? []) : classNames, manifest, environment);
94
- }
95
- function rememberStyle(style, importantStyle) {
96
- if (importantStyle && (typeof importantStyle !== "object" || Object.keys(importantStyle).length)) styleMetadata.set(style, { important: importantStyle });
97
- return style;
98
- }
99
- function createNativeStyleRuntime(initialManifest) {
100
- let manifest = initialManifest;
101
- let environment = activeEnvironment;
102
- const cache = /* @__PURE__ */ new Map();
103
- const runtime = {
104
- tw(value, requestedEnvironment = {}) {
105
- const effectiveEnvironment = {
106
- ...environment,
107
- ...requestedEnvironment
108
- };
109
- const key = `${canonicalTokens(value).join("")}|${effectiveEnvironment.colorScheme ?? ""}|${effectiveEnvironment.platform ?? ""}`;
110
- const cached = cache.get(key);
111
- if (cached) return cached;
112
- const resolved = resolveDynamic(value, manifest, effectiveEnvironment);
113
- const style = rememberStyle(resolved.styleValue, resolved.importantValue);
114
- cache.set(key, style);
115
- return style;
116
- },
117
- getStaticStyle(ids, requestedEnvironment = {}) {
118
- const effectiveEnvironment = {
119
- ...environment,
120
- ...requestedEnvironment
121
- };
122
- const resolved = resolveRules(ids, manifest, effectiveEnvironment);
123
- return rememberStyle(resolved.styleValue, resolved.importantValue);
124
- },
125
- composeStyle(tailwindStyle, inlineStyle) {
126
- const important = styleMetadata.get(tailwindStyle)?.important;
127
- return important !== void 0 ? [
128
- tailwindStyle,
129
- inlineStyle,
130
- important
131
- ] : [tailwindStyle, inlineStyle];
132
- },
133
- setManifest(nextManifest) {
134
- manifest = nextManifest;
135
- cache.clear();
136
- styleMetadata.clear();
137
- createStyleSheet(nextManifest);
138
- activeManifest = nextManifest;
139
- },
140
- setEnvironment(nextEnvironment) {
141
- environment = nextEnvironment;
142
- activeEnvironment = nextEnvironment;
143
- cache.clear();
144
- },
145
- getManifest() {
146
- return manifest;
147
- }
148
- };
149
- if (initialManifest) {
150
- createStyleSheet(initialManifest);
151
- activeManifest = initialManifest;
152
- }
153
- return runtime;
154
- }
155
- const defaultRuntime = createNativeStyleRuntime();
156
- function setManifest(manifest) {
157
- defaultRuntime.setManifest(manifest);
158
- }
159
- /** 由 Expo virtual module 注入,避免 runtime 引入 Node-only 的 require shim。 */
160
- function setStyleSheetFactory(factory) {
161
- styleSheetFactory = factory;
162
- if (activeManifest) createStyleSheet(activeManifest);
163
- }
164
- function setEnvironment(environment) {
165
- defaultRuntime.setEnvironment(environment);
166
- }
167
- function getManifest() {
168
- return activeManifest ?? defaultRuntime.getManifest();
169
- }
170
- function tw(value, environment) {
171
- return defaultRuntime.tw(value, environment);
172
- }
173
- function getStaticStyle(ids, environment) {
174
- return defaultRuntime.getStaticStyle(ids, environment);
175
- }
176
- function composeStyle(tailwindStyle, inlineStyle) {
177
- return defaultRuntime.composeStyle(tailwindStyle, inlineStyle);
178
- }
179
- //#endregion
180
- exports.composeStyle = composeStyle;
181
- exports.createNativeStyleRuntime = createNativeStyleRuntime;
182
- exports.getManifest = getManifest;
183
- exports.getStaticStyle = getStaticStyle;
184
- exports.setEnvironment = setEnvironment;
185
- exports.setManifest = setManifest;
186
- exports.setStyleSheetFactory = setStyleSheetFactory;
187
- exports.tw = tw;
2
+ const require_runtime = require("./runtime-CbkbHrPb.cjs");
3
+ exports.composeStyle = require_runtime.composeStyle;
4
+ exports.createNativeStyleRuntime = require_runtime.createNativeStyleRuntime;
5
+ exports.getManifest = require_runtime.getManifest;
6
+ exports.getPublication = require_runtime.getPublication;
7
+ exports.getStaticStyle = require_runtime.getStaticStyle;
8
+ exports.setEnvironment = require_runtime.setEnvironment;
9
+ exports.setManifest = require_runtime.setManifest;
10
+ exports.setStyleSheetFactory = require_runtime.setStyleSheetFactory;
11
+ exports.tw = require_runtime.tw;
@@ -0,0 +1,3 @@
1
+ import { a as NativeStyleManifest, i as NativePlatform, r as NativeManifestPublication } from "./types-CxNMLBiF.cjs";
2
+ import { a as getStaticStyle, c as setStyleSheetFactory, d as NativeStyleEnvironment, i as getPublication, l as tw, n as createNativeStyleRuntime, o as setEnvironment, p as NativeStyleValue, r as getManifest, s as setManifest, t as composeStyle, u as NativeClassValue } from "./runtime-D4ZGX6yk.cjs";
3
+ export { type NativeClassValue, type NativeManifestPublication, type NativePlatform, type NativeStyleEnvironment, type NativeStyleManifest, type NativeStyleValue, composeStyle, createNativeStyleRuntime, getManifest, getPublication, getStaticStyle, setEnvironment, setManifest, setStyleSheetFactory, tw };
package/dist/runtime.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { i as NativeStyleManifest, r as NativePlatform } from "./types-CKAFTXX9.js";
2
- import { a as setEnvironment, c as tw, f as NativeStyleValue, i as getStaticStyle, l as NativeClassValue, n as createNativeStyleRuntime, o as setManifest, r as getManifest, s as setStyleSheetFactory, t as composeStyle, u as NativeStyleEnvironment } from "./runtime-B3hAisFx.js";
3
- export { type NativeClassValue, type NativePlatform, type NativeStyleEnvironment, type NativeStyleManifest, type NativeStyleValue, composeStyle, createNativeStyleRuntime, getManifest, getStaticStyle, setEnvironment, setManifest, setStyleSheetFactory, tw };
1
+ import { a as NativeStyleManifest, i as NativePlatform, r as NativeManifestPublication } from "./types-CxNMLBiF.js";
2
+ import { a as getStaticStyle, c as setStyleSheetFactory, d as NativeStyleEnvironment, i as getPublication, l as tw, n as createNativeStyleRuntime, o as setEnvironment, p as NativeStyleValue, r as getManifest, s as setManifest, t as composeStyle, u as NativeClassValue } from "./runtime-YHlnn5ip.js";
3
+ export { type NativeClassValue, type NativeManifestPublication, type NativePlatform, type NativeStyleEnvironment, type NativeStyleManifest, type NativeStyleValue, composeStyle, createNativeStyleRuntime, getManifest, getPublication, getStaticStyle, setEnvironment, setManifest, setStyleSheetFactory, tw };