'use client' // The bundler wiring the gui engine needs on Next, as ONE wrapper. // // The Next twin of `@luxfi/ui/vite`. @hanzo/gui is authored against React // Native primitives, so a Next build has to transpile the engine (it ships // untranspiled ESM), alias `react-native` → `react-native-web` in BOTH // bundlers Next now uses, and define the platform flags the engine reads at // module scope. // // const { withLuxUi } = require('@luxfi/ui/next') // module.exports = withLuxUi({ /* your config */ }) // // Every Next surface needed the identical incantation and every one that // hand-rolled it transpiled a DIFFERENT subset of the engine — which is how // one app renders a Sheet and the next throws `createContext of undefined` on // the same component. The list below is the umbrella's own export list, in one // place, so it cannot drift per app. // // Node-only (a build-time module) and dependency-free: it takes and returns a // plain object, so it does not force `next` into anyone's type graph. import { createRequire } from 'node:module'; import { ALIASABLE, GUI_DEFINES, GUI_PACKAGES } from './engine'; /** * Exact-match aliases pinning every aliasable package to ONE entry file, for * bundlers that have no `dedupe` of their own. * * Lives HERE, not in ./engine, because it needs `node:module`. `engine.ts` is * plain values so a browser bundle can import it; a `require('node:module')` * deferred inside it survived the CJS build and then died the moment a surface * used an ESM `next.config.mjs` — esbuild emits `Dynamic require of "module" is * not supported` and Next never boots. A build-time concern belongs in the * build-time module. * * Keys carry webpack's `$` exact-match suffix so only the bare specifier is * redirected — a deep import keeps resolving normally. Packages that are not * physically installed are skipped rather than throwing: the list is the * engine's full surface and no single app pulls all of it. */ function guiAliases(from: string): Record { const require_ = createRequire(`${ from.replace(/\/?$/, '/') }noop.js`); const alias: Record = {}; for (const pkg of ALIASABLE) { try { alias[`${ pkg }$`] = require_.resolve(pkg); } catch { // Not installed in this app — nothing to dedupe. } } return alias; } interface WebpackConfig { resolve?: { alias?: Record }; plugins?: Array; [key: string]: unknown; } /** The structural slice of a Next config this wrapper touches. */ export interface LuxUiNextConfig { /** App root, for resolving the engine's single copy. Defaults to `cwd`. */ dir?: string; transpilePackages?: Array; turbopack?: { resolveAlias?: Record; [key: string]: unknown }; env?: Record; webpack?: (config: WebpackConfig, context: unknown) => WebpackConfig; [key: string]: unknown; } /** * Wrap a Next config so `@luxfi/ui` and the gui engine build. * * Additive: an existing `transpilePackages`, `turbopack.resolveAlias`, * `env` and `webpack()` are all preserved and extended, never replaced. */ export function withLuxUi(config: LuxUiNextConfig = {}): LuxUiNextConfig { const userWebpack = config.webpack; // ONE physical copy of every engine package. Webpack has no `resolve.dedupe`, // so the equivalent is an exact-match alias per package, resolved from the app // root — see ./engine for why two copies is a crash and not a size problem. const engine = guiAliases(config.dir ?? process.cwd()); // Turbopack gets `react-native` and NOTHING ELSE, on purpose. // // It is the only dev bundler from Next 16, so a path-based dedupe here looked // like the way to kill the dev-mode `setReference` storm. It is not. Turbopack // resolves an alias VALUE against its own project root, which it infers from // the workspace and which is not the app directory: an absolute value comes // back as `Can't resolve './Users/…/react/index.js' — server relative imports // are not implemented yet`, and a value made relative to `dir` re-anchors to // that other root and mis-resolves Next's own runtime — the bootnode console // 500'd on every route with `Could not find the module // "[project]/work/hanzo/bootnode/web/…/global-error.js#default" in the React // Client Manifest`. // // Prod builds still dedupe through `webpack()` below. A dev-only duplicate is // a package-manager problem and belongs in the app's `pnpm.overrides`, not in // an alias map that can take the dev server down. // // `dir` is OURS, not Next's — leaving it on the returned object makes Next // print `Unrecognized key(s) in object: 'dir'` on every boot of every surface // that passes it. Consume it here. const { dir: _dir, ...rest } = config; return { ...rest, transpilePackages: [ ...new Set([ ...(config.transpilePackages ?? []), ...GUI_PACKAGES ]) ], turbopack: { ...config.turbopack, resolveAlias: { 'react-native': 'react-native-web', ...config.turbopack?.resolveAlias, }, }, // Next inlines `env` into the client bundle, which is how the engine's // module-scope platform checks get their answer under Turbopack too. env: { EXPO_OS: 'web', IS_WEB: 'true', ...config.env }, webpack(webpackConfig: WebpackConfig, context: unknown) { const next = userWebpack ? userWebpack(webpackConfig, context) : webpackConfig; next.resolve = { ...next.resolve, // The app's own aliases win — an app pinning a package on purpose is not // something the umbrella should override. alias: { 'react-native$': 'react-native-web', ...engine, ...next.resolve?.alias }, }; // `context` is Next's `{ webpack }` bag — use ITS webpack instance so the // DefinePlugin is the one running the build. const webpackModule = (context as { webpack?: { DefinePlugin?: new (d: Record) => unknown } })?.webpack; if (webpackModule?.DefinePlugin) { next.plugins = [ ...(next.plugins ?? []), new webpackModule.DefinePlugin({ ...GUI_DEFINES }) ]; } return next; }, }; } export { GUI_PACKAGES };