'use client' // The bundler wiring the gui engine needs on web, as ONE plugin. // // @hanzo/gui is authored against React Native primitives, so a web bundle has to // alias `react-native` → `react-native-web`, pre-bundle the CJS-only pieces, keep // exactly one physical copy of React and of the engine, and define the platform // flags the engine reads at module scope. Every Lux surface needs the identical // six-part incantation, and every surface that hand-rolled it got a different // subset — which is how you end up debugging `createContext of undefined` in one // app and a duplicate-React theme context in another. // // import { luxUi } from '@luxfi/ui/vite' // export default defineConfig({ plugins: [react(), luxUi()] }) // // Node-only (it is a build-time module) and dependency-free: it returns a plain // Vite plugin object, so it does not force `vite` into anyone's runtime graph. import { ENGINE_DEDUPE, GUI_DEFINES } from './engine'; interface LuxUiViteOptions { /** * Extra packages to keep to a single physical copy. React, the gui engine and * TanStack Query are always deduped. */ readonly dedupe?: ReadonlyArray; /** * Extra packages to pre-bundle. The engine, react-native-web and the CJS-only * colour helper are always included. */ readonly include?: ReadonlyArray; } /** Minimal structural shape of a Vite plugin — avoids a hard `vite` type dep. */ interface LuxUiVitePlugin { readonly name: string; /** Ahead of a consumer's own plugins, so the native redirect below wins. */ readonly enforce: 'pre'; readonly resolveId: (source: string) => string | null; readonly load: (id: string) => string | null; readonly config: () => { define: Record; resolve: { alias: Record; dedupe: Array }; optimizeDeps: { include: Array; rollupOptions: { plugins: Array }; }; }; } // The WHOLE engine, not a hand-picked three. `@hanzogui/popper` and // `@hanzogui/popover` publish the contexts a Tooltip trigger reads, and they // were the two missing from the old list — so a consumer using this plugin // could still get two poppers and the `setReference` crash it causes. The list // itself lives in ./engine, shared with the Next wrapper. const DEDUPE = ENGINE_DEDUPE; const INCLUDE = [ 'react-native-web', '@react-native/normalize-color', '@hanzo/gui', '@hanzogui/core', '@luxfi/ui', ]; /** * The two lookups react-native-svg makes for code that only exists on a phone, * and what a browser gets instead. * * The icon set draws through react-native-svg, whose New Architecture files ask * the platform to register a native view — `codegenNativeComponent`, which * react-native-web does not carry, and `TurboModuleRegistry`, which it does not * export. Aliasing `react-native` to `react-native-web` rewrites the specifier * to a path that does not exist there, so the build stops on a file that was * never going to be present. * * A hook rather than an alias, because an alias matches the specifier as * written and these are reached by a relative `require` from inside the * package. A hook also wins: the alias above sorts ahead of a consumer's own. * * `codegen` answers with the name it was given. React renders an unrecognised * lowercase string as an unknown element, so a native view that reached the * browser draws nothing and throws nothing — the right outcome for something * whose implementation is native code. * * `registry` throws instead, naming the member. Returning null would fail later * on a property of null, several frames from anything that says why. */ const CODEGEN = '\0luxui:codegen' const REGISTRY = '\0luxui:registry' const STUBS: Record = { [CODEGEN]: 'export default (name) => name\n', [REGISTRY]: 'const ask = (property) => {\n' + ' throw new Error(\n' + ' `react-native-svg asked the platform for the native module member "${String(property)}". ` +\n' + ' `A browser has no native module to answer with; drawing on web goes through ` +\n' + ' `react-native-svg\'s ordinary components, which do not reach this.`,\n' + ' )\n' + '}\n' + 'export default new Proxy({}, { get: (_t, p) => ask(p) })\n' + 'export const getEnforcing = ask\n' + 'export const get = ask\n', } /** Sends both lookups at a stand-in, in every program that reads this config. */ const natives = () => ({ name: '@luxfi/ui:natives', enforce: 'pre' as const, resolveId(source: string) { if (/Utilities\/codegenNativeComponent$/.test(source)) return CODEGEN // Only the two svg modules, never TurboModuleRegistry itself: // react-native-web answers that for things it really does implement, and // redirecting all of it takes NativeAnimatedModule down with it. if (/fabric\/NativeSvg(Renderable|View)Module$/.test(source)) return REGISTRY return null }, load(id: string) { return STUBS[id] ?? null }, }) export function luxUi(options: LuxUiViteOptions = {}): LuxUiVitePlugin { const stubs = natives() return { name: '@luxfi/ui', enforce: 'pre', resolveId: stubs.resolveId, load: stubs.load, config: () => ({ // The engine branches on these at module scope; without them it takes the // native path in a browser bundle. (./engine) define: { ...GUI_DEFINES }, resolve: { alias: { 'react-native': 'react-native-web' }, dedupe: [ ...new Set([ ...DEDUPE, ...(options.dedupe ?? []) ]) ], }, optimizeDeps: { include: [ ...new Set([ ...INCLUDE, ...(options.include ?? []) ]) ], // The prebundler runs its own plugin list and takes none of the above, // so the redirect has to be handed to it separately or it meets the // same missing file the other two programs were spared. rollupOptions: { plugins: [ natives() ] }, }, }), }; } export type { LuxUiViteOptions };