@fluenti/react 0.1.0

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 (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +244 -0
  3. package/dist/__tests__/DateTime.test.d.ts +2 -0
  4. package/dist/__tests__/DateTime.test.d.ts.map +1 -0
  5. package/dist/__tests__/Number.test.d.ts +2 -0
  6. package/dist/__tests__/Number.test.d.ts.map +1 -0
  7. package/dist/__tests__/Plural.test.d.ts +2 -0
  8. package/dist/__tests__/Plural.test.d.ts.map +1 -0
  9. package/dist/__tests__/Select.test.d.ts +2 -0
  10. package/dist/__tests__/Select.test.d.ts.map +1 -0
  11. package/dist/__tests__/Trans.test.d.ts +2 -0
  12. package/dist/__tests__/Trans.test.d.ts.map +1 -0
  13. package/dist/__tests__/locale-switch.test.d.ts +2 -0
  14. package/dist/__tests__/locale-switch.test.d.ts.map +1 -0
  15. package/dist/__tests__/plural-core.test.d.ts +2 -0
  16. package/dist/__tests__/plural-core.test.d.ts.map +1 -0
  17. package/dist/__tests__/provider.test.d.ts +2 -0
  18. package/dist/__tests__/provider.test.d.ts.map +1 -0
  19. package/dist/__tests__/server.test.d.ts +2 -0
  20. package/dist/__tests__/server.test.d.ts.map +1 -0
  21. package/dist/__tests__/trans-core.test.d.ts +2 -0
  22. package/dist/__tests__/trans-core.test.d.ts.map +1 -0
  23. package/dist/compile-time-t.d.ts +3 -0
  24. package/dist/compile-time-t.d.ts.map +1 -0
  25. package/dist/components/DateTime.d.ts +16 -0
  26. package/dist/components/DateTime.d.ts.map +1 -0
  27. package/dist/components/Number.d.ts +16 -0
  28. package/dist/components/Number.d.ts.map +1 -0
  29. package/dist/components/Plural.d.ts +35 -0
  30. package/dist/components/Plural.d.ts.map +1 -0
  31. package/dist/components/Select.d.ts +32 -0
  32. package/dist/components/Select.d.ts.map +1 -0
  33. package/dist/components/Trans.d.ts +29 -0
  34. package/dist/components/Trans.d.ts.map +1 -0
  35. package/dist/components/icu-rich.d.ts +22 -0
  36. package/dist/components/icu-rich.d.ts.map +1 -0
  37. package/dist/components/plural-core.d.ts +15 -0
  38. package/dist/components/plural-core.d.ts.map +1 -0
  39. package/dist/components/trans-core.d.ts +28 -0
  40. package/dist/components/trans-core.d.ts.map +1 -0
  41. package/dist/context.d.ts +3 -0
  42. package/dist/context.d.ts.map +1 -0
  43. package/dist/global-registry.d.ts +18 -0
  44. package/dist/global-registry.d.ts.map +1 -0
  45. package/dist/hooks/__useI18n.d.ts +12 -0
  46. package/dist/hooks/__useI18n.d.ts.map +1 -0
  47. package/dist/hooks/useI18n.d.ts +11 -0
  48. package/dist/hooks/useI18n.d.ts.map +1 -0
  49. package/dist/icu-rich-DBeWY1k6.js +108 -0
  50. package/dist/icu-rich-DBeWY1k6.js.map +1 -0
  51. package/dist/icu-rich-XY1SdM5K.cjs +2 -0
  52. package/dist/icu-rich-XY1SdM5K.cjs.map +1 -0
  53. package/dist/index.cjs +3 -0
  54. package/dist/index.cjs.map +1 -0
  55. package/dist/index.d.ts +17 -0
  56. package/dist/index.d.ts.map +1 -0
  57. package/dist/index.js +200 -0
  58. package/dist/index.js.map +1 -0
  59. package/dist/msg.d.ts +2 -0
  60. package/dist/msg.d.ts.map +1 -0
  61. package/dist/provider.d.ts +3 -0
  62. package/dist/provider.d.ts.map +1 -0
  63. package/dist/server.cjs +2 -0
  64. package/dist/server.cjs.map +1 -0
  65. package/dist/server.d.ts +240 -0
  66. package/dist/server.d.ts.map +1 -0
  67. package/dist/server.js +122 -0
  68. package/dist/server.js.map +1 -0
  69. package/dist/types.d.ts +55 -0
  70. package/dist/types.d.ts.map +1 -0
  71. package/package.json +82 -0
@@ -0,0 +1,240 @@
1
+ import { FluentInstanceExtended, Locale, Messages, DateFormatOptions, NumberFormatOptions } from '@fluenti/core';
2
+ import { ReactNode, ReactElement } from 'react';
3
+ export { detectLocale, getSSRLocaleScript, getHydratedLocale, isRTL, getDirection } from '@fluenti/core';
4
+ export type { DetectLocaleOptions } from '@fluenti/core';
5
+ /**
6
+ * Configuration for `createServerI18n`.
7
+ */
8
+ export interface ServerI18nConfig {
9
+ /** Load messages for a given locale. Called once per locale per request. */
10
+ loadMessages: (locale: string) => Promise<Messages | {
11
+ default: Messages;
12
+ }>;
13
+ /** Fallback locale when a translation is missing */
14
+ fallbackLocale?: string;
15
+ /**
16
+ * Auto-resolve locale when `setLocale()` was not called.
17
+ *
18
+ * This is the fallback for contexts where the layout doesn't run — most
19
+ * notably **Server Actions** (`'use server'`), which are independent
20
+ * requests that skip the layout tree entirely.
21
+ *
22
+ * Common patterns:
23
+ * - Read from a cookie (Next.js: `cookies().get('locale')`)
24
+ * - Read from a request header set by middleware
25
+ * - Query the database for the authenticated user's preference
26
+ *
27
+ * If omitted and `setLocale()` was not called, `getI18n()` will throw.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * resolveLocale: async () => {
32
+ * const { cookies } = await import('next/headers')
33
+ * return (await cookies()).get('locale')?.value ?? 'en'
34
+ * }
35
+ * ```
36
+ */
37
+ resolveLocale?: () => string | Promise<string>;
38
+ /** Custom fallback chains per locale */
39
+ fallbackChain?: Record<string, Locale[]>;
40
+ /** Custom date format styles */
41
+ dateFormats?: DateFormatOptions;
42
+ /** Custom number format styles */
43
+ numberFormats?: NumberFormatOptions;
44
+ /** Handler for missing translation keys */
45
+ missing?: (locale: Locale, id: string) => string | undefined;
46
+ }
47
+ export interface ServerTransProps {
48
+ /** Source text with embedded components */
49
+ children: ReactNode;
50
+ /** Override auto-generated hash ID */
51
+ id?: string;
52
+ /** Message context used for identity and translator disambiguation */
53
+ context?: string;
54
+ /** Context comment for translators */
55
+ comment?: string;
56
+ /** Custom render wrapper */
57
+ render?: (translation: ReactNode) => ReactNode;
58
+ }
59
+ export interface ServerPluralProps {
60
+ /** The count value */
61
+ value: number;
62
+ /** Override the auto-generated synthetic ICU message id */
63
+ id?: string;
64
+ /** Message context used for identity and translator disambiguation */
65
+ context?: string;
66
+ /** Translator-facing note preserved in extraction catalogs */
67
+ comment?: string;
68
+ /** Text for zero (if language supports) */
69
+ zero?: ReactNode;
70
+ /** Singular form. `#` replaced with value */
71
+ one?: ReactNode;
72
+ /** Dual form (Arabic, etc.) */
73
+ two?: ReactNode;
74
+ /** Few form (Slavic languages, etc.) */
75
+ few?: ReactNode;
76
+ /** Many form */
77
+ many?: ReactNode;
78
+ /** Default plural form */
79
+ other: ReactNode;
80
+ /** Offset from value before selecting form */
81
+ offset?: number;
82
+ }
83
+ export interface ServerSelectProps {
84
+ /** The selector value */
85
+ value: string;
86
+ /** Override the auto-generated synthetic ICU message id */
87
+ id?: string;
88
+ /** Message context used for identity and translator disambiguation */
89
+ context?: string;
90
+ /** Translator-facing note preserved in extraction catalogs */
91
+ comment?: string;
92
+ /** Default case */
93
+ other: ReactNode;
94
+ /** Type-safe named options. Takes precedence over direct case props. */
95
+ options?: Record<string, ReactNode>;
96
+ /** Named cases — any string key maps to a ReactNode */
97
+ [key: string]: ReactNode | Record<string, ReactNode> | string | undefined;
98
+ }
99
+ export interface ServerDateTimeProps {
100
+ /** Date value to format */
101
+ value: Date | number;
102
+ /** Named format style */
103
+ style?: string;
104
+ }
105
+ export interface ServerNumberProps {
106
+ /** Number value to format */
107
+ value: number;
108
+ /** Named format style */
109
+ style?: string;
110
+ }
111
+ type ServerTransComponent = (props: ServerTransProps) => Promise<ReactElement>;
112
+ type ServerPluralComponent = (props: ServerPluralProps) => Promise<ReactElement>;
113
+ type ServerSelectComponent = (props: ServerSelectProps) => Promise<ReactElement>;
114
+ type ServerDateTimeComponent = (props: ServerDateTimeProps) => Promise<ReactElement>;
115
+ type ServerNumberComponent = (props: ServerNumberProps) => Promise<ReactElement>;
116
+ /**
117
+ * The object returned by `createServerI18n`.
118
+ */
119
+ export interface ServerI18n {
120
+ /**
121
+ * Set the locale for the current server request.
122
+ * Call this once in your root layout or page before any `getI18n()` calls.
123
+ *
124
+ * Uses `React.cache()` — scoped to the current request automatically.
125
+ */
126
+ setLocale: (locale: string) => void;
127
+ /**
128
+ * Get a fully configured i18n instance for the current request.
129
+ * Messages are loaded lazily and cached per-request.
130
+ *
131
+ * @example
132
+ * ```tsx
133
+ * // In any Server Component
134
+ * const { t, d, n, locale } = await getI18n()
135
+ * return <h1>{t('welcome')}</h1>
136
+ * ```
137
+ */
138
+ getI18n: () => Promise<FluentInstanceExtended & {
139
+ locale: string;
140
+ }>;
141
+ /**
142
+ * `<Trans>` for React Server Components.
143
+ * Async component — automatically resolves the i18n instance.
144
+ *
145
+ * @example
146
+ * ```tsx
147
+ * <Trans>Read the <a href="/docs">documentation</a>.</Trans>
148
+ * ```
149
+ */
150
+ Trans: ServerTransComponent;
151
+ /**
152
+ * `<Plural>` for React Server Components.
153
+ *
154
+ * @example
155
+ * ```tsx
156
+ * <Plural value={count} one="# item" other="# items" />
157
+ * ```
158
+ */
159
+ Plural: ServerPluralComponent;
160
+ /**
161
+ * `<Select>` for React Server Components.
162
+ *
163
+ * @example
164
+ * ```tsx
165
+ * <Select value={gender} male="He liked this" other="They liked this" />
166
+ * ```
167
+ */
168
+ Select: ServerSelectComponent;
169
+ /**
170
+ * `<DateTime>` for React Server Components.
171
+ *
172
+ * @example
173
+ * ```tsx
174
+ * <DateTime value={new Date()} style="long" />
175
+ * ```
176
+ */
177
+ DateTime: ServerDateTimeComponent;
178
+ /**
179
+ * `<NumberFormat>` for React Server Components.
180
+ *
181
+ * @example
182
+ * ```tsx
183
+ * <NumberFormat value={1234.56} style="currency" />
184
+ * ```
185
+ */
186
+ NumberFormat: ServerNumberComponent;
187
+ /**
188
+ * Synchronous accessor for the cached i18n instance.
189
+ * Used internally by @fluenti/next webpack loader.
190
+ * @internal
191
+ */
192
+ __getSyncInstance: () => FluentInstanceExtended & {
193
+ locale: string;
194
+ };
195
+ }
196
+ /**
197
+ * Create server-side i18n utilities for React Server Components.
198
+ *
199
+ * Uses `React.cache()` to share state within a single server request
200
+ * without React Context (which is unavailable in Server Components).
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * // lib/i18n.server.ts — define once
205
+ * import { createServerI18n } from '@fluenti/react/server'
206
+ *
207
+ * export const { setLocale, getI18n, Trans, Plural, Select, DateTime, NumberFormat } = createServerI18n({
208
+ * loadMessages: (locale) => import(`../messages/${locale}.json`),
209
+ * fallbackLocale: 'en',
210
+ * })
211
+ * ```
212
+ *
213
+ * ```tsx
214
+ * // app/[locale]/layout.tsx — set locale once
215
+ * import { setLocale } from '@/lib/i18n.server'
216
+ *
217
+ * export default async function Layout({ params, children }) {
218
+ * const { locale } = await params
219
+ * setLocale(locale)
220
+ * return <html lang={locale}><body>{children}</body></html>
221
+ * }
222
+ * ```
223
+ *
224
+ * ```tsx
225
+ * // app/[locale]/page.tsx — use Trans, Plural, etc. directly
226
+ * import { Trans, Plural, Select } from '@/lib/i18n.server'
227
+ *
228
+ * export default async function Page() {
229
+ * return (
230
+ * <div>
231
+ * <Trans>Read the <a href="/docs">documentation</a>.</Trans>
232
+ * <Plural value={5} one="# item" other="# items" />
233
+ * <Select value="male" male="He liked this" other="They liked this" />
234
+ * </div>
235
+ * )
236
+ * }
237
+ * ```
238
+ */
239
+ export declare function createServerI18n(config: ServerI18nConfig): ServerI18n;
240
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,sBAAsB,EAEtB,MAAM,EACN,QAAQ,EACR,iBAAiB,EACjB,mBAAmB,EACpB,MAAM,eAAe,CAAA;AACtB,OAAO,EAA2B,KAAK,SAAS,EAAE,KAAK,YAAY,EAAE,MAAM,OAAO,CAAA;AAMlF,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,iBAAiB,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAA;AACxG,YAAY,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAA;AAExD;;GAEG;AACH,MAAM,WAAW,gBAAgB;I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,KAAK,EAAE,iBAAiB,KAAK,OAAO,CAAC,YAAY,CAAC,CAAA;AAChF,KAAK,uBAAuB,GAAG,CAAC,KAAK,EAAE,mBAAmB,KAAK,OAAO,CAAC,YAAY,CAAC,CAAA;AACpF,KAAK,qBAAqB,GAAG,CAAC,KAAK,EAAE,iBAAiB,KAAK,OAAO,CAAC,YAAY,CAAC,CAAA;AAEhF;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;;;;OAKG;IACH,SAAS,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;IAEnC;;;;;;;;;;OAUG;IACH,OAAO,EAAE,MAAM,OAAO,CAAC,sBAAsB,GAAG;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;IAEnE;;;;;;;;OAQG;IACH,KAAK,EAAE,oBAAoB,CAAA;IAE3B;;;;;;;OAOG;IACH,MAAM,EAAE,qBAAqB,CAAA;IAE7B;;;;;;;OAOG;IACH,MAAM,EAAE,qBAAqB,CAAA;IAE7B;;;;;;;OAOG;IACH,QAAQ,EAAE,uBAAuB,CAAA;IAEjC;;;;;;;OAOG;IACH,YAAY,EAAE,qBAAqB,CAAA;IAEnC;;;;OAIG;IACH,iBAAiB,EAAE,MAAM,sBAAsB,GAAG;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE,CAAA;CACrE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,gBAAgB,GAAG,UAAU,CA+PrE"}
package/dist/server.js ADDED
@@ -0,0 +1,122 @@
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c } from "./icu-rich-DBeWY1k6.js";
2
+ import { Fragment as l, cache as u, createElement as d } from "react";
3
+ import { createFluent as f, detectLocale as p, getDirection as m, getHydratedLocale as h, getSSRLocaleScript as g, hashMessage as _, isRTL as v } from "@fluenti/core";
4
+ //#region src/server.ts
5
+ function y(p) {
6
+ let m = u(() => ({
7
+ locale: null,
8
+ instance: null
9
+ })), h = u(() => /* @__PURE__ */ new Map()), g = null, v = 0, y = 0;
10
+ function b(e) {
11
+ m().locale = e, v++, g = null;
12
+ }
13
+ async function x(e) {
14
+ let t = h(), n = t.get(e);
15
+ if (n) return n;
16
+ let r = await p.loadMessages(e), i = typeof r == "object" && r && "default" in r ? r.default : r;
17
+ return t.set(e, i), i;
18
+ }
19
+ async function S() {
20
+ let e = m();
21
+ !e.locale && p.resolveLocale && (e.locale = await p.resolveLocale());
22
+ let t = e.locale;
23
+ if (!t) throw Error("[fluenti] No locale set. Call setLocale(locale) in your layout before using getI18n(), or provide a resolveLocale function in createServerI18n config to auto-detect locale in Server Actions and other contexts where the layout does not run.");
24
+ if (e.instance && e.instance.locale === t) return e.instance;
25
+ let n = {};
26
+ n[t] = await x(t), p.fallbackLocale && p.fallbackLocale !== t && (n[p.fallbackLocale] = await x(p.fallbackLocale));
27
+ let r = {
28
+ locale: t,
29
+ messages: n
30
+ };
31
+ return p.fallbackLocale !== void 0 && (r.fallbackLocale = p.fallbackLocale), p.fallbackChain !== void 0 && (r.fallbackChain = p.fallbackChain), p.dateFormats !== void 0 && (r.dateFormats = p.dateFormats), p.numberFormats !== void 0 && (r.numberFormats = p.numberFormats), p.missing !== void 0 && (r.missing = p.missing), e.instance = f(r), g = e.instance, y = v, e.instance;
32
+ }
33
+ async function C({ children: e, id: n, context: i, comment: a, render: o }) {
34
+ let c = await S(), { message: u, components: f } = s(e), p = n ?? t(u, i), m = r(c.t({
35
+ id: p,
36
+ message: u,
37
+ ...i === void 0 ? {} : { context: i },
38
+ ...a === void 0 ? {} : { comment: a }
39
+ }), f);
40
+ return d(l, null, o ? o(m) : m);
41
+ }
42
+ async function w({ value: t, id: r, context: i, comment: o, zero: s, one: u, two: f, few: p, many: m, other: h, offset: g }) {
43
+ let v = await S(), { messages: y, components: b } = e(a, {
44
+ zero: s,
45
+ one: u,
46
+ two: f,
47
+ few: p,
48
+ many: m,
49
+ other: h
50
+ }), x = c({
51
+ ...y.zero !== void 0 && { zero: y.zero },
52
+ ...y.one !== void 0 && { one: y.one },
53
+ ...y.two !== void 0 && { two: y.two },
54
+ ...y.few !== void 0 && { few: y.few },
55
+ ...y.many !== void 0 && { many: y.many },
56
+ other: y.other ?? ""
57
+ }, g);
58
+ return d(l, null, n({
59
+ id: r ?? (i === void 0 ? x : _(x, i)),
60
+ message: x,
61
+ ...i === void 0 ? {} : { context: i },
62
+ ...o === void 0 ? {} : { comment: o }
63
+ }, { count: t }, (e, t) => v.t(e, t), b));
64
+ }
65
+ async function T({ value: t, id: r, context: a, comment: s, other: c, options: u, ...f }) {
66
+ let p = await S(), m = u === void 0 ? {
67
+ ...Object.fromEntries(Object.entries(f).filter(([e]) => ![
68
+ "value",
69
+ "id",
70
+ "context",
71
+ "comment",
72
+ "options",
73
+ "other"
74
+ ].includes(e))),
75
+ other: c
76
+ } : {
77
+ ...u,
78
+ other: c
79
+ }, h = [...Object.keys(m).filter((e) => e !== "other"), "other"], { messages: g, components: v } = e(h, m), y = o(Object.fromEntries([...h].map((e) => [e, g[e] ?? ""]))), b = i(y.forms);
80
+ return d(l, null, n({
81
+ id: r ?? (a === void 0 ? b : _(b, a)),
82
+ message: b,
83
+ ...a === void 0 ? {} : { context: a },
84
+ ...s === void 0 ? {} : { comment: s }
85
+ }, { value: y.valueMap[t] ?? "other" }, (e, t) => p.t(e, t), v));
86
+ }
87
+ async function E({ value: e, style: t }) {
88
+ return d(l, null, (await S()).d(e, t));
89
+ }
90
+ async function D({ value: e, style: t }) {
91
+ return d(l, null, (await S()).n(e, t));
92
+ }
93
+ function O() {
94
+ let e = m();
95
+ if (e.instance) return e.instance;
96
+ if (g && y === v) return g;
97
+ let t = e.locale ?? p.fallbackLocale ?? "en", n = h(), r = {}, i = n.get(t);
98
+ if (i && (r[t] = i), p.fallbackLocale && p.fallbackLocale !== t) {
99
+ let e = n.get(p.fallbackLocale);
100
+ e && (r[p.fallbackLocale] = e);
101
+ }
102
+ let a = {
103
+ locale: t,
104
+ messages: r
105
+ };
106
+ return p.fallbackLocale !== void 0 && (a.fallbackLocale = p.fallbackLocale), p.fallbackChain !== void 0 && (a.fallbackChain = p.fallbackChain), p.dateFormats !== void 0 && (a.dateFormats = p.dateFormats), p.numberFormats !== void 0 && (a.numberFormats = p.numberFormats), p.missing !== void 0 && (a.missing = p.missing), e.instance = f(a), e.instance;
107
+ }
108
+ return {
109
+ setLocale: b,
110
+ getI18n: S,
111
+ __getSyncInstance: O,
112
+ Trans: C,
113
+ Plural: w,
114
+ Select: T,
115
+ DateTime: E,
116
+ NumberFormat: D
117
+ };
118
+ }
119
+ //#endregion
120
+ export { y as createServerI18n, p as detectLocale, m as getDirection, h as getHydratedLocale, g as getSSRLocaleScript, v as isRTL };
121
+
122
+ //# sourceMappingURL=server.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.js","names":[],"sources":["../src/server.ts"],"sourcesContent":["import { cache } from 'react'\nimport { createFluent, hashMessage as hashSyntheticMessage } from '@fluenti/core'\nimport type {\n FluentInstanceExtended,\n FluentConfigExtended,\n Locale,\n Messages,\n DateFormatOptions,\n NumberFormatOptions,\n} from '@fluenti/core'\nimport { createElement, Fragment, type ReactNode, type ReactElement } from 'react'\nimport { hashMessage, extractMessage, reconstruct } from './components/trans-core'\nimport { PLURAL_CATEGORIES, type PluralCategory } from './components/plural-core'\nimport { buildICUPluralMessage, buildICUSelectMessage, normalizeSelectForms, renderRichTranslation, serializeRichForms } from './components/icu-rich'\n\n// Re-export SSR utilities from core for convenience\nexport { detectLocale, getSSRLocaleScript, getHydratedLocale, isRTL, getDirection } from '@fluenti/core'\nexport type { DetectLocaleOptions } from '@fluenti/core'\n\n/**\n * Configuration for `createServerI18n`.\n */\nexport interface ServerI18nConfig {\n /** Load messages for a given locale. Called once per locale per request. */\n loadMessages: (locale: string) => Promise<Messages | { default: Messages }>\n /** Fallback locale when a translation is missing */\n fallbackLocale?: string\n /**\n * Auto-resolve locale when `setLocale()` was not called.\n *\n * This is the fallback for contexts where the layout doesn't run — most\n * notably **Server Actions** (`'use server'`), which are independent\n * requests that skip the layout tree entirely.\n *\n * Common patterns:\n * - Read from a cookie (Next.js: `cookies().get('locale')`)\n * - Read from a request header set by middleware\n * - Query the database for the authenticated user's preference\n *\n * If omitted and `setLocale()` was not called, `getI18n()` will throw.\n *\n * @example\n * ```ts\n * resolveLocale: async () => {\n * const { cookies } = await import('next/headers')\n * return (await cookies()).get('locale')?.value ?? 'en'\n * }\n * ```\n */\n resolveLocale?: () => string | Promise<string>\n /** Custom fallback chains per locale */\n fallbackChain?: Record<string, Locale[]>\n /** Custom date format styles */\n dateFormats?: DateFormatOptions\n /** Custom number format styles */\n numberFormats?: NumberFormatOptions\n /** Handler for missing translation keys */\n missing?: (locale: Locale, id: string) => string | undefined\n}\n\n// ─── Server Component Props ──────────────────────────────────────────────────\n\nexport interface ServerTransProps {\n /** Source text with embedded components */\n children: ReactNode\n /** Override auto-generated hash ID */\n id?: string\n /** Message context used for identity and translator disambiguation */\n context?: string\n /** Context comment for translators */\n comment?: string\n /** Custom render wrapper */\n render?: (translation: ReactNode) => ReactNode\n}\n\nexport interface ServerPluralProps {\n /** The count value */\n value: number\n /** Override the auto-generated synthetic ICU message id */\n id?: string\n /** Message context used for identity and translator disambiguation */\n context?: string\n /** Translator-facing note preserved in extraction catalogs */\n comment?: string\n /** Text for zero (if language supports) */\n zero?: ReactNode\n /** Singular form. `#` replaced with value */\n one?: ReactNode\n /** Dual form (Arabic, etc.) */\n two?: ReactNode\n /** Few form (Slavic languages, etc.) */\n few?: ReactNode\n /** Many form */\n many?: ReactNode\n /** Default plural form */\n other: ReactNode\n /** Offset from value before selecting form */\n offset?: number\n}\n\nexport interface ServerSelectProps {\n /** The selector value */\n value: string\n /** Override the auto-generated synthetic ICU message id */\n id?: string\n /** Message context used for identity and translator disambiguation */\n context?: string\n /** Translator-facing note preserved in extraction catalogs */\n comment?: string\n /** Default case */\n other: ReactNode\n /** Type-safe named options. Takes precedence over direct case props. */\n options?: Record<string, ReactNode>\n /** Named cases — any string key maps to a ReactNode */\n [key: string]: ReactNode | Record<string, ReactNode> | string | undefined\n}\n\nexport interface ServerDateTimeProps {\n /** Date value to format */\n value: Date | number\n /** Named format style */\n style?: string\n}\n\nexport interface ServerNumberProps {\n /** Number value to format */\n value: number\n /** Named format style */\n style?: string\n}\n\n// ─── Server Component Types ──────────────────────────────────────────────────\n\ntype ServerTransComponent = (props: ServerTransProps) => Promise<ReactElement>\ntype ServerPluralComponent = (props: ServerPluralProps) => Promise<ReactElement>\ntype ServerSelectComponent = (props: ServerSelectProps) => Promise<ReactElement>\ntype ServerDateTimeComponent = (props: ServerDateTimeProps) => Promise<ReactElement>\ntype ServerNumberComponent = (props: ServerNumberProps) => Promise<ReactElement>\n\n/**\n * The object returned by `createServerI18n`.\n */\nexport interface ServerI18n {\n /**\n * Set the locale for the current server request.\n * Call this once in your root layout or page before any `getI18n()` calls.\n *\n * Uses `React.cache()` — scoped to the current request automatically.\n */\n setLocale: (locale: string) => void\n\n /**\n * Get a fully configured i18n instance for the current request.\n * Messages are loaded lazily and cached per-request.\n *\n * @example\n * ```tsx\n * // In any Server Component\n * const { t, d, n, locale } = await getI18n()\n * return <h1>{t('welcome')}</h1>\n * ```\n */\n getI18n: () => Promise<FluentInstanceExtended & { locale: string }>\n\n /**\n * `<Trans>` for React Server Components.\n * Async component — automatically resolves the i18n instance.\n *\n * @example\n * ```tsx\n * <Trans>Read the <a href=\"/docs\">documentation</a>.</Trans>\n * ```\n */\n Trans: ServerTransComponent\n\n /**\n * `<Plural>` for React Server Components.\n *\n * @example\n * ```tsx\n * <Plural value={count} one=\"# item\" other=\"# items\" />\n * ```\n */\n Plural: ServerPluralComponent\n\n /**\n * `<Select>` for React Server Components.\n *\n * @example\n * ```tsx\n * <Select value={gender} male=\"He liked this\" other=\"They liked this\" />\n * ```\n */\n Select: ServerSelectComponent\n\n /**\n * `<DateTime>` for React Server Components.\n *\n * @example\n * ```tsx\n * <DateTime value={new Date()} style=\"long\" />\n * ```\n */\n DateTime: ServerDateTimeComponent\n\n /**\n * `<NumberFormat>` for React Server Components.\n *\n * @example\n * ```tsx\n * <NumberFormat value={1234.56} style=\"currency\" />\n * ```\n */\n NumberFormat: ServerNumberComponent\n\n /**\n * Synchronous accessor for the cached i18n instance.\n * Used internally by @fluenti/next webpack loader.\n * @internal\n */\n __getSyncInstance: () => FluentInstanceExtended & { locale: string }\n}\n\n/**\n * Create server-side i18n utilities for React Server Components.\n *\n * Uses `React.cache()` to share state within a single server request\n * without React Context (which is unavailable in Server Components).\n *\n * @example\n * ```ts\n * // lib/i18n.server.ts — define once\n * import { createServerI18n } from '@fluenti/react/server'\n *\n * export const { setLocale, getI18n, Trans, Plural, Select, DateTime, NumberFormat } = createServerI18n({\n * loadMessages: (locale) => import(`../messages/${locale}.json`),\n * fallbackLocale: 'en',\n * })\n * ```\n *\n * ```tsx\n * // app/[locale]/layout.tsx — set locale once\n * import { setLocale } from '@/lib/i18n.server'\n *\n * export default async function Layout({ params, children }) {\n * const { locale } = await params\n * setLocale(locale)\n * return <html lang={locale}><body>{children}</body></html>\n * }\n * ```\n *\n * ```tsx\n * // app/[locale]/page.tsx — use Trans, Plural, etc. directly\n * import { Trans, Plural, Select } from '@/lib/i18n.server'\n *\n * export default async function Page() {\n * return (\n * <div>\n * <Trans>Read the <a href=\"/docs\">documentation</a>.</Trans>\n * <Plural value={5} one=\"# item\" other=\"# items\" />\n * <Select value=\"male\" male=\"He liked this\" other=\"They liked this\" />\n * </div>\n * )\n * }\n * ```\n */\nexport function createServerI18n(config: ServerI18nConfig): ServerI18n {\n // Request-scoped store using React.cache()\n // Each server request gets its own isolated state\n const getRequestStore = cache((): {\n locale: string | null\n instance: (FluentInstanceExtended & { locale: string }) | null\n } => ({\n locale: null,\n instance: null,\n }))\n\n // Cache loaded messages per-request to avoid redundant imports\n const getMessageCache = cache((): Map<string, Messages> => new Map())\n\n // Module-level fallback for server actions where React.cache()\n // may not share state with the page render.\n let _lastInstance: (FluentInstanceExtended & { locale: string }) | null = null\n let _requestId = 0\n let _lastRequestId = 0\n\n function setLocale(locale: string): void {\n getRequestStore().locale = locale\n _requestId++\n _lastInstance = null\n }\n\n async function loadLocaleMessages(locale: string): Promise<Messages> {\n const messageCache = getMessageCache()\n const cached = messageCache.get(locale)\n if (cached) return cached\n\n const raw = await config.loadMessages(locale)\n const messages: Messages =\n typeof raw === 'object' && raw !== null && 'default' in raw\n ? (raw as { default: Messages }).default\n : (raw as Messages)\n\n messageCache.set(locale, messages)\n return messages\n }\n\n async function getI18n(): Promise<FluentInstanceExtended & { locale: string }> {\n const store = getRequestStore()\n\n // If setLocale() was never called (e.g. Server Action — independent request\n // that skips the layout), try the resolveLocale fallback.\n if (!store.locale && config.resolveLocale) {\n store.locale = await config.resolveLocale()\n }\n\n const locale = store.locale\n\n if (!locale) {\n throw new Error(\n '[fluenti] No locale set. Call setLocale(locale) in your layout before using getI18n(), ' +\n 'or provide a resolveLocale function in createServerI18n config to auto-detect locale ' +\n 'in Server Actions and other contexts where the layout does not run.',\n )\n }\n\n // Return cached instance if locale hasn't changed\n if (store.instance && store.instance.locale === locale) {\n return store.instance\n }\n\n // Load messages for current locale (and fallback if configured)\n const allMessages: Record<string, Messages> = {}\n allMessages[locale] = await loadLocaleMessages(locale)\n\n if (config.fallbackLocale && config.fallbackLocale !== locale) {\n allMessages[config.fallbackLocale] = await loadLocaleMessages(config.fallbackLocale)\n }\n\n const fluentConfig: FluentConfigExtended = {\n locale,\n messages: allMessages,\n }\n if (config.fallbackLocale !== undefined) fluentConfig.fallbackLocale = config.fallbackLocale\n if (config.fallbackChain !== undefined) fluentConfig.fallbackChain = config.fallbackChain\n if (config.dateFormats !== undefined) fluentConfig.dateFormats = config.dateFormats\n if (config.numberFormats !== undefined) fluentConfig.numberFormats = config.numberFormats\n if (config.missing !== undefined) fluentConfig.missing = config.missing\n\n store.instance = createFluent(fluentConfig)\n _lastInstance = store.instance\n _lastRequestId = _requestId\n return store.instance\n }\n\n // ─── Async Server Components ─────────────────────────────────────────────\n\n async function Trans({ children, id, context, comment, render }: ServerTransProps): Promise<ReactElement> {\n const i18n = await getI18n()\n const { message, components } = extractMessage(children)\n const messageId = id ?? hashMessage(message, context)\n const translated = i18n.t({\n id: messageId,\n message,\n ...(context !== undefined ? { context } : {}),\n ...(comment !== undefined ? { comment } : {}),\n })\n const result = reconstruct(translated, components)\n return createElement(Fragment, null, render ? render(result) : result)\n }\n\n async function Plural({\n value,\n id,\n context,\n comment,\n zero,\n one,\n two,\n few,\n many,\n other,\n offset,\n }: ServerPluralProps): Promise<ReactElement> {\n const i18n = await getI18n()\n const forms: Record<PluralCategory, ReactNode | undefined> = {\n zero,\n one,\n two,\n few,\n many,\n other,\n }\n const { messages, components } = serializeRichForms(PLURAL_CATEGORIES, forms)\n const icuMessage = buildICUPluralMessage(\n {\n ...(messages.zero !== undefined && { zero: messages.zero }),\n ...(messages.one !== undefined && { one: messages.one }),\n ...(messages.two !== undefined && { two: messages.two }),\n ...(messages.few !== undefined && { few: messages.few }),\n ...(messages.many !== undefined && { many: messages.many }),\n other: messages.other ?? '',\n },\n offset,\n )\n\n const result = renderRichTranslation(\n {\n id: id ?? (context === undefined ? icuMessage : hashSyntheticMessage(icuMessage, context)),\n message: icuMessage,\n ...(context !== undefined ? { context } : {}),\n ...(comment !== undefined ? { comment } : {}),\n },\n { count: value },\n (descriptor, values) => i18n.t(descriptor, values),\n components,\n )\n\n return createElement(Fragment, null, result)\n }\n\n async function Select({\n value,\n id,\n context,\n comment,\n other,\n options,\n ...cases\n }: ServerSelectProps): Promise<ReactElement> {\n const i18n = await getI18n()\n const forms: Record<string, ReactNode | undefined> = options === undefined\n ? {\n ...Object.fromEntries(\n Object.entries(cases).filter(([key]) => !['value', 'id', 'context', 'comment', 'options', 'other'].includes(key)),\n ),\n other,\n }\n : {\n ...options,\n other,\n }\n\n const orderedKeys = [...Object.keys(forms).filter(key => key !== 'other'), 'other'] as const\n const { messages, components } = serializeRichForms(orderedKeys, forms)\n const normalized = normalizeSelectForms(\n Object.fromEntries(\n [...orderedKeys].map((key) => [key, messages[key] ?? '']),\n ),\n )\n const icuMessage = buildICUSelectMessage(normalized.forms)\n\n const result = renderRichTranslation(\n {\n id: id ?? (context === undefined ? icuMessage : hashSyntheticMessage(icuMessage, context)),\n message: icuMessage,\n ...(context !== undefined ? { context } : {}),\n ...(comment !== undefined ? { comment } : {}),\n },\n { value: normalized.valueMap[value] ?? 'other' },\n (descriptor, values) => i18n.t(descriptor, values),\n components,\n )\n\n return createElement(Fragment, null, result)\n }\n\n async function DateTime({ value, style }: ServerDateTimeProps): Promise<ReactElement> {\n const i18n = await getI18n()\n return createElement(Fragment, null, i18n.d(value, style))\n }\n\n async function NumberFormat({ value, style }: ServerNumberProps): Promise<ReactElement> {\n const i18n = await getI18n()\n return createElement(Fragment, null, i18n.n(value, style))\n }\n\n /**\n * Synchronous accessor for the cached i18n instance.\n * Used by @fluenti/next webpack loader for t`` in RSC.\n * Throws if getI18n() hasn't been called yet in this request.\n * @internal\n */\n function __getSyncInstance(): FluentInstanceExtended & { locale: string } {\n const store = getRequestStore()\n if (store.instance) {\n return store.instance\n }\n\n // Module-level fallback for server actions where React.cache()\n // may not share state across different call contexts.\n // Only use within the same request (tracked by _requestId).\n if (_lastInstance && _lastRequestId === _requestId) {\n return _lastInstance\n }\n\n // Final fallback: create instance synchronously with the default locale.\n // This handles Suspense boundaries and streamed components where\n // the React.cache store may not have been populated yet.\n const locale = store.locale ?? config.fallbackLocale ?? 'en'\n const messageCache = getMessageCache()\n const messages: Record<string, Messages> = {}\n const cached = messageCache.get(locale)\n if (cached) messages[locale] = cached\n if (config.fallbackLocale && config.fallbackLocale !== locale) {\n const fallback = messageCache.get(config.fallbackLocale)\n if (fallback) messages[config.fallbackLocale] = fallback\n }\n\n const fluentConfig: FluentConfigExtended = { locale, messages }\n if (config.fallbackLocale !== undefined) fluentConfig.fallbackLocale = config.fallbackLocale\n if (config.fallbackChain !== undefined) fluentConfig.fallbackChain = config.fallbackChain\n if (config.dateFormats !== undefined) fluentConfig.dateFormats = config.dateFormats\n if (config.numberFormats !== undefined) fluentConfig.numberFormats = config.numberFormats\n if (config.missing !== undefined) fluentConfig.missing = config.missing\n\n store.instance = createFluent(fluentConfig)\n return store.instance\n }\n\n return { setLocale, getI18n, __getSyncInstance, Trans, Plural, Select, DateTime, NumberFormat }\n}\n"],"mappings":";;;;AA0QA,SAAgB,EAAiB,GAAsC;CAGrE,IAAM,IAAkB,SAGlB;EACJ,QAAQ;EACR,UAAU;EACX,EAAE,EAGG,IAAkB,wBAAmC,IAAI,KAAK,CAAC,EAIjE,IAAsE,MACtE,IAAa,GACb,IAAiB;CAErB,SAAS,EAAU,GAAsB;AAGvC,EAFA,GAAiB,CAAC,SAAS,GAC3B,KACA,IAAgB;;CAGlB,eAAe,EAAmB,GAAmC;EACnE,IAAM,IAAe,GAAiB,EAChC,IAAS,EAAa,IAAI,EAAO;AACvC,MAAI,EAAQ,QAAO;EAEnB,IAAM,IAAM,MAAM,EAAO,aAAa,EAAO,EACvC,IACJ,OAAO,KAAQ,YAAY,KAAgB,aAAa,IACnD,EAA8B,UAC9B;AAGP,SADA,EAAa,IAAI,GAAQ,EAAS,EAC3B;;CAGT,eAAe,IAAgE;EAC7E,IAAM,IAAQ,GAAiB;AAI/B,EAAI,CAAC,EAAM,UAAU,EAAO,kBAC1B,EAAM,SAAS,MAAM,EAAO,eAAe;EAG7C,IAAM,IAAS,EAAM;AAErB,MAAI,CAAC,EACH,OAAU,MACR,kPAGD;AAIH,MAAI,EAAM,YAAY,EAAM,SAAS,WAAW,EAC9C,QAAO,EAAM;EAIf,IAAM,IAAwC,EAAE;AAGhD,EAFA,EAAY,KAAU,MAAM,EAAmB,EAAO,EAElD,EAAO,kBAAkB,EAAO,mBAAmB,MACrD,EAAY,EAAO,kBAAkB,MAAM,EAAmB,EAAO,eAAe;EAGtF,IAAM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essageDescriptor, CompileTimeT, DateFormatOptions, NumberFormatOptions, FluentInstanceExtended } from '@fluenti/core';
3
+ export interface I18nContextValue {
4
+ /** The underlying Fluent instance (escape hatch for advanced use) */
5
+ i18n: FluentInstanceExtended;
6
+ /** Translate a message by id with optional interpolation values */
7
+ t: {
8
+ (id: string | MessageDescriptor, values?: Record<string, unknown>): string;
9
+ (strings: TemplateStringsArray, ...exprs: unknown[]): string;
10
+ };
11
+ /** Format a date value for the current locale */
12
+ d: (value: Date | number, style?: string) => string;
13
+ /** Format a number value for the current locale */
14
+ n: (value: number, style?: string) => string;
15
+ /** Format an ICU message string directly (no catalog lookup) */
16
+ format: (message: string, values?: Record<string, unknown>) => string;
17
+ /** Merge additional messages into a locale catalog at runtime */
18
+ loadMessages: (locale: string, messages: Messages) => void;
19
+ /** Return all locale codes that have loaded messages */
20
+ getLocales: () => string[];
21
+ /** Current locale */
22
+ locale: string;
23
+ /** Change the active locale (async when lazy loading) */
24
+ setLocale: (locale: string) => Promise<void>;
25
+ /** Whether a locale is currently being loaded */
26
+ isLoading: boolean;
27
+ /** Set of locales whose messages have been loaded */
28
+ loadedLocales: string[];
29
+ /** Preload a locale in the background without switching to it */
30
+ preloadLocale: (locale: string) => Promise<void>;
31
+ }
32
+ export interface I18nProviderProps {
33
+ /** Active locale code */
34
+ locale: string;
35
+ /** Fallback locale when translation is missing */
36
+ fallbackLocale?: string;
37
+ /** Static message catalogs */
38
+ messages?: AllMessages;
39
+ /** Async loader for lazy loading */
40
+ loadMessages?: (locale: string) => Promise<Messages | {
41
+ default: Messages;
42
+ }>;
43
+ /** Custom fallback chains per locale */
44
+ fallbackChain?: Record<string, string[]>;
45
+ /** Date format styles */
46
+ dateFormats?: DateFormatOptions;
47
+ /** Number format styles */
48
+ numberFormats?: NumberFormatOptions;
49
+ /** Missing message handler */
50
+ missing?: (locale: Locale, id: string) => string | undefined;
51
+ /** App content */
52
+ children: ReactNode;
53
+ }
54
+ export type { Locale, Messages, AllMessages, MessageDescriptor, CompileTimeMessageDescriptor, CompileTimeT, DateFormatOptions, NumberFormatOptions, FluentInstanceExtended, };
55
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AACtC,OAAO,KAAK,EACV,MAAM,EACN,QAAQ,EACR,WAAW,EACX,iBAAiB,EACjB,4BAA4B,EAC5B,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,EACnB,sBAAsB,EACvB,MAAM,eAAe,CAAA;AAEtB,MAAM,WAAW,gBAAgB;IAC/B,qEAAqE;IACrE,IAAI,EAAE,sBAAsB,CAAA;IAC5B,mEAAmE;IACnE,CAAC,EAAE;QACD,CAAC,EAAE,EAAE,MAAM,GAAG,iBAAiB,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAA;QAC1E,CAAC,OAAO,EAAE,oBAAoB,EAAE,GAAG,KAAK,EAAE,OAAO,EAAE,GAAG,MAAM,CAAA;KAC7D,CAAA;IACD,iDAAiD;IACjD,CAAC,EAAE,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,MAAM,CAAA;IACnD,mDAAmD;IACnD,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,MAAM,CAAA;IAC5C,gEAAgE;IAChE,MAAM,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,MAAM,CAAA;IACrE,iEAAiE;IACjE,YAAY,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAA;IAC1D,wDAAwD;IACxD,UAAU,EAAE,MAAM,MAAM,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 "version": "0.1.0",
4
+ "type": "module",
5
+ "description": "React bindings for Fluenti — I18nProvider, useI18n, Trans/Plural/Select components",
6
+ "homepage": "https://fluenti.dev",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/usefluenti/fluenti.git",
10
+ "directory": "packages/react"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/usefluenti/fluenti/issues"
14
+ },
15
+ "license": "MIT",
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "keywords": [
20
+ "i18n",
21
+ "internationalization",
22
+ "compile-time",
23
+ "icu",
24
+ "messageformat",
25
+ "react",
26
+ "hooks",
27
+ "provider"
28
+ ],
29
+ "main": "./dist/index.cjs",
30
+ "module": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "import": {
35
+ "types": "./dist/index.d.ts",
36
+ "default": "./dist/index.js"
37
+ },
38
+ "require": {
39
+ "types": "./dist/index.d.ts",
40
+ "default": "./dist/index.cjs"
41
+ }
42
+ },
43
+ "./server": {
44
+ "import": {
45
+ "types": "./dist/server.d.ts",
46
+ "default": "./dist/server.js"
47
+ },
48
+ "require": {
49
+ "types": "./dist/server.d.ts",
50
+ "default": "./dist/server.cjs"
51
+ }
52
+ }
53
+ },
54
+ "files": [
55
+ "dist"
56
+ ],
57
+ "peerDependencies": {
58
+ "react": ">=18.0.0"
59
+ },
60
+ "dependencies": {
61
+ "@fluenti/core": "0.1.0"
62
+ },
63
+ "devDependencies": {
64
+ "typescript": "^5.9",
65
+ "vite": "^8",
66
+ "vite-plugin-dts": "^4",
67
+ "vitest": "^4",
68
+ "@vitest/coverage-v8": "^4",
69
+ "react": "^19.0.0",
70
+ "react-dom": "^19.0.0",
71
+ "@testing-library/react": "^16.0.0",
72
+ "@types/react": "^19.0.0",
73
+ "@types/react-dom": "^19.0.0",
74
+ "happy-dom": "^20"
75
+ },
76
+ "scripts": {
77
+ "build": "vite build",
78
+ "dev": "vite build --watch",
79
+ "test": "vitest run",
80
+ "typecheck": "tsc --noEmit"
81
+ }
82
+ }