Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-6982-duck-type-runnable-dev-env.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/start-plugin-core': patch
---

fix(start-plugin-core): replace isRunnableDevEnvironment instanceof check with duck-typing to avoid dual-package hazard when vite is aliased (e.g. vite-plus pnpm override)
20 changes: 13 additions & 7 deletions packages/start-plugin-core/src/dev-server-plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isRunnableDevEnvironment } from 'vite'
import { VIRTUAL_MODULES } from '@tanstack/start-server-core'
import { NodeRequest, sendNodeResponse } from 'srvx/node'
import { ENTRY_POINTS, VITE_ENVIRONMENT_NAMES } from '../constants'
Expand All @@ -9,7 +8,12 @@ import {
collectDevStyles,
normalizeCssModuleCacheKey,
} from './dev-styles'
import type { Connect, DevEnvironment, PluginOption } from 'vite'
import type {
Connect,
DevEnvironment,
PluginOption,
RunnableDevEnvironment,
} from 'vite'
import type { GetConfigFn } from '../types'

export function devServerPlugin({
Expand Down Expand Up @@ -145,15 +149,17 @@ export function devServerPlugin({

// do not install middleware if SSR env in case another plugin already did
if (
!isRunnableDevEnvironment(serverEnv) ||
// Use duck-type check instead of instanceof/isRunnableDevEnvironment to avoid
// dual-package hazard when vite is aliased (e.g. vite-plus pnpm override)
!('runner' in serverEnv) ||
// do not check via `isFetchableDevEnvironment` since nitro does implement the `FetchableDevEnvironment` interface but not via inheritance (which this helper checks)
'dispatchFetch' in serverEnv
) {
return
}
}

if (!isRunnableDevEnvironment(serverEnv)) {
if (!('runner' in serverEnv)) {
throw new Error(
'cannot install vite dev server middleware for TanStack Start since the SSR environment is not a RunnableDevEnvironment',
)
Expand All @@ -175,9 +181,9 @@ export function devServerPlugin({
* fetch(req: Request): Promise<Response>
* }
*/
const serverEntry = await serverEnv.runner.import(
ENTRY_POINTS.server,
)
const serverEntry = await (
serverEnv as RunnableDevEnvironment
).runner.import(ENTRY_POINTS.server)
const webRes = await serverEntry['default'].fetch(webReq)

return sendNodeResponse(res, webRes)
Expand Down
Loading