getFrameworkAdapter, tanstack & nextjs
The adapter objects behind twilightReact(): tanstack generates route files and returns the Vite plugins; nextjs is a placeholder whose methods throw.
import { getFrameworkAdapter, tanstack, nextjs, FrameworkAdapter } from '@salla.sa/twilight-theme-engine/vite';In plain words
twilightReact() does its work through an adapter: an object that knows how to connect the engine to one framework. The engine ships two. tanstack is the one every theme uses; nextjs only reserves the name and throws when used.
A theme never needs these. They exist for scripts that want one step of twilightReact() on its own, such as regenerating the route files.
Signature
function getFrameworkAdapter(framework: 'tanstack' | 'nextjs'): FrameworkAdapter
const tanstack: FrameworkAdapter; // name 'tanstack'
const nextjs: FrameworkAdapter; // name 'nextjs'; both methods throw
interface FrameworkAdapter {
name: string;
generateRouteFiles: (options: {
projectRoot: string;
baseRoutes?: TwilightRoute[]; // replaces the engine's built-in routes
baseRoutesImport: string;
}) => TwilightRoute[];
createPlugins: (options: TwilightReactOptions) => Promise<PluginOption[]>;
}Example
// Node, from the theme folder. Writes app/routes/* like a dev start would.
import { tanstack } from '@salla.sa/twilight-theme-engine/vite';
const routes = tanstack.generateRouteFiles({
projectRoot: process.cwd(),
baseRoutesImport: '@salla.sa/twilight-theme-engine/routes',
});
console.table(routes.map(({ path, file, factory }) => ({ path, file, factory })));
How it behaves
twilightReact(options)isgetFrameworkAdapter(options.framework ?? 'tanstack').createPlugins(options), after pointing the engine's head helpers at that framework inside the Vite process.tanstack.generateRouteFilesreadsapp/routes.tsunderprojectRoot, merges its routes overbaseRoutes(or the built-in list) by path, writesapp/routes/*.tsxfor files that are missing or still start with// @auto-generated, writes the locale, account and redirect wrappers into the engine package's.twilight/folder, and returns the merged route table.tanstack.createPluginscallsgenerateRouteFilesonce right away and again in theconfighook with Vite'sroot, then returns the plugin list described on twilightReact.getFrameworkAdapterthrows[twilight-react] Unsupported framework: <name>for any other name. There is no way to register a third adapter: a custom one is used by calling itscreatePluginsyourself invite.config.ts.The
/nextjssubpath is a different thing: runtime helpers for a Next.js app, in the framework group. It does not make this adapter work.
Gotchas
generateRouteFileswrites files, andbaseRoutesreplaces the built-in list instead of adding to it: passbaseRoutes: [myRoute]and every built-in page disappears from the generated table. Fix: leavebaseRoutesout and add pages throughapp/routes.ts.An
app/routes.tsthat fails to compile or run only logs a warning and counts as no custom routes;generateRouteFilesdoes not throw.nextjs, andtwilightReact({ framework: 'nextjs' }), throw[twilight-react:nextjs] Next.js framework support is not yet implemented.packages/theme-engine/src/vite/README.mdshows an adapter withcreateRouterPluginandcreateReactPlugin, and importsGenerateRoutesOptionsfrom/vite. Neither exists: the interface hascreatePlugins, and the options type isParameters<FrameworkAdapter['generateRouteFiles']>[0].
Related
The one Vite plugin call in a theme's vite.config.ts: server rendering, a route for every storefront page, translations and build checks.
TwilightRoute & option typesSupporting types of /vite: one row of the generated route table, the framework names, and option shapes twilightReact() accepts but ignores.
Source and docs
- Engine source:
packages/theme-engine/src/vite/adapters/index.ts