Repository navigation
Description
Which project does this relate to?
Start
Describe the bug
Hopefully this explanation is detailed enough, but the fix is only one line.
The bug
When using tanstackStart({ prerender: { enabled: true } }) with a monorepo where --configLoader native is required to launch Vite, the prerender server will crash.
Root cause
After the client and SSR builds complete, @tanstack/start-plugin-core/src/vite/prerender.ts spins up a Vite preview server to handle prerender requests (here):
// packages/start-plugin-core/src/vite/prerender.ts
async function startPreviewServer(viteConfig: ResolvedConfig): Promise<PreviewServer> {
const vite = await import('vite')
return await vite.preview({
configFile: viteConfig.configFile,
preview: { port: 0, open: false },
// ← configLoader is NOT forwarded
})
}This calls vite.preview() without forwarding the configLoader from the original invocation. The configLoader field defaults to 'bundle' (I need native instead), which makes Vite re-parse vite.config.ts using Rolldown's internal config bundler. This causes the preview server to crash because it doesn't have the custom condition from NODE_OPTIONS="--conditions=@repo/source" that would allow it to resolve any package export that requires said custom condition.
Complete minimal reproducer
Steps to Reproduce the Bug
$ pnpm install
$ pnpm buildYou should see:
✓ built in 263ms
failed to load config from/start-basic-static/packages/app/vite.config.ts
error during build:
Error: Failed to start the Vite preview server for prerendering
at startPreviewServer (file:///start-basic-static/node_modules/.pnpm/@[email protected]_@[email protected][email protected]_r_271e9cf7e315f271d5a87ddafbf33008/node_modules/@tanstack/start-plugin-core/dist/esm/vite/prerender.js:41:9)If you follow the above link to the bundled file, you will find the throw:
async function startPreviewServer(viteConfig) {
const vite = await import("vite");
try {
return await vite.preview({
configFile: viteConfig.configFile,
preview: {
port: 0,
open: false
}
});
} catch (error) {
throw new Error("Failed to start the Vite preview server for prerendering", { cause: error });
}
}The call to vite.preview() is just missing one line to propagate the config loader (see below).
Expected behavior
The vite.preview() call in startPreviewServer should forward configLoader (and ideally all relevant InlineConfig fields) from the resolved config of the original build. This is easy, a one line change (I tested it by hard-coding it in the shipped dist/esm/vite/prerender.js):
return await vite.preview({
configFile: viteConfig.configFile,
configLoader: viteConfig.inlineConfig?.configLoader, // ← propagate
preview: { port: 0, open: false },
})Note: configLoader is part of Vite 8's InlineConfig type
Screenshots or Videos
N/A
Platform
- TanStack Start 1.168.25 (I believe)
- macOS
- Browser: N/A
- Bundler: Vite
- Bundler Version: 8.0.16
Additional context
Thank you!
Activity
sebastienbarre commentedon Jun 9, 2026
Friendly tag of @edmundhung since they worked on this code (issue #5921). Hopefully this makes sense.
edmundhung commentedon Jun 10, 2026
Does
--configLoaderhas a corresponding config option inside vite config? If it's available only through the CLI argument, I think it's valid to do so.[-]`vite.preview()` called during static prerendering doesn't forward `configLoader`, causing `[UNRESOLVED_IMPORT]` warnings for packages that require custom export conditions (EASY FIX)[/-][+]`vite.preview()` called during static prerendering doesn't forward `configLoader`, causing preview server to crash (EASY FIX)[/+]sebastienbarre commentedon Jun 10, 2026
Hell @edmundhung ; I managed to create a monorepo for you to reproduce the error. Sorry for the delay. In the process, I realized this is not a warning, this is actually a crash, the whole preview server will crash (throw) in this small monorepo. Same cause, same easy fix, but I updated the title and body of the message accordingly, this is more severe than in my monorepo.
The
--configLoaderhas an equivalent option as far as thevite.previewAPI is concerned, that's the one mentioned in my fix, and the root of the issue:The Vite config's
resolve.conditionsorssr.resolve.conditionsoptions are related, but NOT the fix (see below).sebastienbarre commentedon Jun 10, 2026
Update: maybe I misunderstood your question. No, there is no equivalent option inside the Vite config; if there was such an option, it would be too late to put it in the Vite config anyway;
--configLoaderis the option that governs what will be used to parse the Vite config, this happens before the Vite config is even set up. The crash happens because the preview server is initialized by re-reading the Vite config (the call tovite.previewcalls withconfigFile: viteConfig.configFile), and since it is not using the same loader anymore, it throws because it doesn't have theNODE_OPTIONSthe--configLoader nativeis using to actually parse the Vite config without erroring. Sorry if you knew all that already.This is related to setting
resolve.conditions: ['@repo/source']in the Vite config, yes, but that part is for the bundler (and I assume, passed down to Rolldown). The part that is missing is for situations where the Vite config itself needs to resolve packages that require that custom condition. In this monorepo example, it's theimport { MY_CONSTANT } from '@repo/support';found invite.config.ts. The@repo/support'spackage.jsonstates:Therefore the
@repo/sourcecustom condition is needed to import it "live; the goal here is tovite devorvite buildwith live types, since Vite can bundle both the app and the dependencies found in@repo/support. This needs to work even if the support libraries have not been built. In my real monorepo, there are situations where consumers will need to use the transpiled code indist/and other situations where Vite can work with live sources; this setup is meant to accommodate both.sebastienbarre commentedon Jun 10, 2026
This commit introduces the
NODE_OPTIONSand Node.js custom exports condition that reveal this issue.If you are curious, this is in relation to Colin McDonnell (
colinhacks)'s Live types in a TypeScript monorepo, i.e. how to setup a monorepo where types are live and you do not have to build anything, while still having the ability to build your internal packages for publishing. Custom conditions are a way to solve this. You need to set them up in your TSConfigcompilerOptions.customConditions, as well as your calls to Node using--conditions(or NODE_OPTIONS in the case ofvite build), and in your Vite config'sresolve.conditions(phew).edmundhung commentedon Jun 10, 2026
Ah, thanks for clarifying. That makes sense to me. But I'm not on the TanStack team, so I will defer to them on the final decision :)
schiller-manuel commentedon Jun 12, 2026
sounds reasonable. want to create a PR? we probably should create a separate e2e test project for this as a regression test
sebastienbarre commentedon Jun 12, 2026
I can create a PR. I'm afraid I'm not familiar with e2e testing though.
schiller-manuel commentedon Jun 12, 2026
have a look at the existing e2e test projects and follow their setup. let me know if you need help