Skip to content
Draft
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
6 changes: 5 additions & 1 deletion .claude/settings.local.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
"Bash(mkdir:*)",
"Bash(TEST_ONLY=dev yarn vitest --config ./vitest.config.rolldown.ts --run --reporter=dot --color=false api-rolldown.test.ts)",
"Bash(bun llink:*)",
"Bash(bun:*)"
"Bash(bun:*)",
"Bash(npm run dev:*)",
"Bash(npm run build:*)",
"Bash(npm run test:dev:*)",
"Bash(mv:*)"
],
"deny": []
}
Expand Down
79 changes: 79 additions & 0 deletions packages/one/src/vite/__tests__/server-client-only.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'

describe('server-only module', () => {
let originalEnv: string | undefined

beforeEach(() => {
originalEnv = process.env.VITE_ENVIRONMENT
vi.resetModules()
})

afterEach(() => {
if (originalEnv !== undefined) {
process.env.VITE_ENVIRONMENT = originalEnv
} else {
delete process.env.VITE_ENVIRONMENT
}
})

it('should throw an error when imported in client environment', async () => {
process.env.VITE_ENVIRONMENT = 'client'

await expect(async () => {
await import('../server-only')
}).rejects.toThrow('This file should only be imported on the server! Current environment: client')
})

it('should not throw when imported in ssr environment', async () => {
process.env.VITE_ENVIRONMENT = 'ssr'

await expect(import('../server-only')).resolves.not.toThrow()
})

it('should throw when VITE_ENVIRONMENT is not set', async () => {
delete process.env.VITE_ENVIRONMENT

await expect(async () => {
await import('../server-only')
}).rejects.toThrow('This file should only be imported on the server! Current environment: undefined')
})
})

describe('client-only module', () => {
let originalEnv: string | undefined

beforeEach(() => {
originalEnv = process.env.VITE_ENVIRONMENT
vi.resetModules()
})

afterEach(() => {
if (originalEnv !== undefined) {
process.env.VITE_ENVIRONMENT = originalEnv
} else {
delete process.env.VITE_ENVIRONMENT
}
})

it('should throw an error when imported in ssr environment', async () => {
process.env.VITE_ENVIRONMENT = 'ssr'

await expect(async () => {
await import('../client-only')
}).rejects.toThrow('This file should only be imported on the client! Current environment: ssr')
})

it('should not throw when imported in client environment', async () => {
process.env.VITE_ENVIRONMENT = 'client'

await expect(import('../client-only')).resolves.not.toThrow()
})

it('should throw when VITE_ENVIRONMENT is not set', async () => {
delete process.env.VITE_ENVIRONMENT

await expect(async () => {
await import('../client-only')
}).rejects.toThrow('This file should only be imported on the client! Current environment: undefined')
})
})
5 changes: 5 additions & 0 deletions packages/one/src/vite/client-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (process.env.VITE_ENVIRONMENT !== 'client') {
throw new Error(`This file should only be imported on the client! Current environment: ${process.env.VITE_ENVIRONMENT}`)
}

export {}
3 changes: 3 additions & 0 deletions packages/one/src/vite/one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { generateFileSystemRouteTypesPlugin } from './plugins/generateFileSystem
import { SSRCSSPlugin } from './plugins/SSRCSSPlugin'
import { virtualEntryId } from './plugins/virtualEntryConstants'
import { createVirtualEntry } from './plugins/virtualEntryPlugin'
import { serverClientOnlyPlugin } from './plugins/serverClientOnlyPlugin'
import type { One } from './types'
import type {
ExpoManifestRequestHandlerPluginPluginOptions,
Expand Down Expand Up @@ -143,6 +144,8 @@ export function one(options: One.PluginOptions = {}): PluginOption {
__get: options,
},

serverClientOnlyPlugin(),

barrelOption === false
? null
: (barrel({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, it, expect } from 'vitest'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'
import { serverClientOnlyPlugin } from '../serverClientOnlyPlugin'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

describe('serverClientOnlyPlugin', () => {
const plugin = serverClientOnlyPlugin()

it('should have correct plugin name', () => {
expect(plugin.name).toBe('one:server-client-only')
})

it('should enforce pre position', () => {
expect(plugin.enforce).toBe('pre')
})

describe('config hook', () => {
it('should provide aliases for server-only and client-only modules', () => {
const config = plugin.config?.()

expect(config).toEqual({
resolve: {
alias: {
'server-only': resolve(__dirname, '../../server-only.js'),
'client-only': resolve(__dirname, '../../client-only.js'),
},
},
})
})
})

describe('resolveId hook', () => {
it('should resolve server-only module', () => {
const result = plugin.resolveId?.('server-only', undefined, {})

expect(result).toEqual({
id: resolve(__dirname, '../../server-only.js'),
external: false,
})
})

it('should resolve client-only module', () => {
const result = plugin.resolveId?.('client-only', undefined, {})

expect(result).toEqual({
id: resolve(__dirname, '../../client-only.js'),
external: false,
})
})

it('should return null for other modules', () => {
const result = plugin.resolveId?.('some-other-module', undefined, {})

expect(result).toBeNull()
})
})
})
34 changes: 34 additions & 0 deletions packages/one/src/vite/plugins/serverClientOnlyPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Plugin } from 'vite'
import { resolve, dirname } from 'path'
import { fileURLToPath } from 'url'

const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

export function serverClientOnlyPlugin(): Plugin {
return {
name: 'one:server-client-only',
enforce: 'pre',

config() {
return {
resolve: {
alias: {
'server-only': resolve(__dirname, '../server-only.js'),
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias points to '../server-only.js' but the actual file is '../server-only.ts'. This will cause module resolution to fail since the .js extension doesn't match the TypeScript source file.

Suggested change
'server-only': resolve(__dirname, '../server-only.js'),
'server-only': resolve(__dirname, '../server-only.ts'),

Copilot uses AI. Check for mistakes.
'client-only': resolve(__dirname, '../client-only.js'),
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias points to '../client-only.js' but the actual file is '../client-only.ts'. This will cause module resolution to fail since the .js extension doesn't match the TypeScript source file.

Suggested change
'client-only': resolve(__dirname, '../client-only.js'),
'client-only': resolve(__dirname, '../client-only.ts'),

Copilot uses AI. Check for mistakes.
},
},
}
},

resolveId(source) {
if (source === 'server-only') {
return { id: resolve(__dirname, '../server-only.js'), external: false }
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolveId function points to '../server-only.js' but the actual file is '../server-only.ts'. This will cause module resolution to fail.

Suggested change
return { id: resolve(__dirname, '../server-only.js'), external: false }
return { id: resolve(__dirname, '../server-only.ts'), external: false }

Copilot uses AI. Check for mistakes.
}
if (source === 'client-only') {
return { id: resolve(__dirname, '../client-only.js'), external: false }
Copy link

Copilot AI Jul 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolveId function points to '../client-only.js' but the actual file is '../client-only.ts'. This will cause module resolution to fail.

Suggested change
return { id: resolve(__dirname, '../client-only.js'), external: false }
return { id: resolve(__dirname, '../client-only.ts'), external: false }

Copilot uses AI. Check for mistakes.
}
return null
},
}
}
5 changes: 5 additions & 0 deletions packages/one/src/vite/server-only.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
if (process.env.VITE_ENVIRONMENT !== 'ssr') {
throw new Error(`This file should only be imported on the server! Current environment: ${process.env.VITE_ENVIRONMENT}`)
}

export {}
2 changes: 2 additions & 0 deletions packages/one/types/vite/client-only.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=client-only.d.ts.map
3 changes: 3 additions & 0 deletions packages/one/types/vite/plugins/serverClientOnlyPlugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Plugin } from 'vite';
export declare function serverClientOnlyPlugin(): Plugin;
//# sourceMappingURL=serverClientOnlyPlugin.d.ts.map
2 changes: 2 additions & 0 deletions packages/one/types/vite/server-only.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export {};
//# sourceMappingURL=server-only.d.ts.map
2 changes: 1 addition & 1 deletion packages/vxrn/src/exports/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const build = async (optionsIn: VXRNOptions, buildArgs: BuildArgs = {}) =

define: {
'process.env.TAMAGUI_IS_SERVER': '"1"',
'process.env.VITE_ENVIRONMENT': '"server"',
'process.env.VITE_ENVIRONMENT': '"ssr"',
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually i think this is a legit bugfix because vite uses ssr other places

...processEnvDefines,
...webBuildConfig.define,
},
Expand Down
8 changes: 8 additions & 0 deletions tests/test/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ export default () => {
<Button>Go to hooks</Button>
</Link>

<Link asChild href="/test-server-only">
<Button>Test Server Only</Button>
</Link>

<Link asChild href="/test-client-only">
<Button>Test Client Only</Button>
</Link>

<ToggleThemeButton />
</YStack>
)
Expand Down
33 changes: 33 additions & 0 deletions tests/test/app/test-client-only+spa.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useEffect } from 'react'
import { Button, H2, Paragraph, YStack } from 'tamagui'

export default function TestClientOnly() {
const [count, setCount] = useState(0)
const [browserInfo, setBrowserInfo] = useState<any>(null)

useEffect(() => {
// Only import client-only utilities on the client
if (typeof window !== 'undefined') {
import('../utils/client-utils').then(({ getBrowserInfo }) => {
setBrowserInfo(getBrowserInfo())
})
}
}, [])

return (
<YStack f={1} ai="center" jc="center" gap="$4" p="$4">
<H2>Client Only Test Page</H2>
<Paragraph>This page uses client-only utilities</Paragraph>
<Paragraph>Count: {count}</Paragraph>
<Button onPress={() => setCount(count + 1)}>Increment</Button>
<Paragraph fontSize="$2" color="$gray10">
Environment: {typeof window !== 'undefined' ? 'client' : 'server'}
</Paragraph>
{browserInfo && (
<Paragraph fontSize="$2" color="$gray10">
Browser: {browserInfo.language}
</Paragraph>
)}
</YStack>
)
}
26 changes: 26 additions & 0 deletions tests/test/app/test-server-only+ssr.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'server-only'
import { useLoader } from 'one'
import { H2, Paragraph, YStack } from 'tamagui'
import { getServerTime, getServerEnvironment } from '../utils/server-utils'

export async function loader() {
// This loader should only run on the server
const serverInfo = getServerEnvironment()
return {
message: 'This data was loaded on the server',
timestamp: getServerTime(),
...serverInfo
}
}

export default function TestServerOnly() {
const data = useLoader(loader)

return (
<YStack f={1} ai="center" jc="center" gap="$4" p="$4">
<H2>Server Only Test Page</H2>
<Paragraph>This page imports 'server-only' at the top</Paragraph>
<Paragraph>Loaded data: {JSON.stringify(data, null, 2)}</Paragraph>
</YStack>
)
}
2 changes: 1 addition & 1 deletion tests/test/routes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { OneRouter } from 'one'
declare module 'one' {
export namespace OneRouter {
export interface __routes<T extends string = string> extends Record<string, unknown> {
StaticRoutes: `/` | `/(auth-guard)` | `/(auth-guard)/auth-guard` | `/(blog)` | `/(blog)/blog/my-first-post` | `/(marketing)/about` | `/(sub-page-group)` | `/(sub-page-group)/sub-page` | `/(sub-page-group)/sub-page/sub` | `/(sub-page-group)/sub-page/sub2` | `/_sitemap` | `/about` | `/auth-guard` | `/blog/my-first-post` | `/expo-video` | `/hooks` | `/hooks/cases/navigating-into-nested-navigator` | `/hooks/cases/navigating-into-nested-navigator/nested-1` | `/hooks/cases/navigating-into-nested-navigator/nested-1/nested-2` | `/hooks/cases/navigating-into-nested-navigator/nested-1/nested-2/page` | `/hooks/contents` | `/hooks/contents/page-1` | `/hooks/contents/page-2` | `/layouts` | `/layouts/nested-layout/with-slug-layout-folder/[layoutSlug]/` | `/loader` | `/loader/other` | `/middleware` | `/not-found/deep/test` | `/not-found/fallback/test` | `/not-found/test` | `/rn-features/platform-specific-extensions/test` | `/rn-features/platform-specific-extensions/test-route-1` | `/rn-features/platform-specific-extensions/test-route-2` | `/server-data` | `/sheet` | `/spa/spapage` | `/ssr` | `/ssr/` | `/ssr/basic` | `/sub-page` | `/sub-page/sub` | `/sub-page/sub2` | `/vite-features/import-meta-env` | `/web-extensions`
StaticRoutes: `/` | `/(auth-guard)` | `/(auth-guard)/auth-guard` | `/(blog)` | `/(blog)/blog/my-first-post` | `/(marketing)/about` | `/(sub-page-group)` | `/(sub-page-group)/sub-page` | `/(sub-page-group)/sub-page/sub` | `/(sub-page-group)/sub-page/sub2` | `/_sitemap` | `/about` | `/auth-guard` | `/blog/my-first-post` | `/expo-video` | `/hooks` | `/hooks/cases/navigating-into-nested-navigator` | `/hooks/cases/navigating-into-nested-navigator/nested-1` | `/hooks/cases/navigating-into-nested-navigator/nested-1/nested-2` | `/hooks/cases/navigating-into-nested-navigator/nested-1/nested-2/page` | `/hooks/contents` | `/hooks/contents/page-1` | `/hooks/contents/page-2` | `/layouts` | `/layouts/nested-layout/with-slug-layout-folder/[layoutSlug]/` | `/loader` | `/loader/other` | `/middleware` | `/not-found/deep/test` | `/not-found/fallback/test` | `/not-found/test` | `/rn-features/platform-specific-extensions/test` | `/rn-features/platform-specific-extensions/test-route-1` | `/rn-features/platform-specific-extensions/test-route-2` | `/server-data` | `/sheet` | `/spa/spapage` | `/ssr` | `/ssr/` | `/ssr/basic` | `/sub-page` | `/sub-page/sub` | `/sub-page/sub2` | `/test-client-only` | `/test-server-only` | `/vite-features/import-meta-env` | `/web-extensions`
DynamicRoutes: `/dynamic-folder-routes/${OneRouter.SingleRoutePart<T>}/${OneRouter.SingleRoutePart<T>}` | `/hooks/contents/with-nested-slug/${OneRouter.SingleRoutePart<T>}` | `/hooks/contents/with-nested-slug/${OneRouter.SingleRoutePart<T>}/${OneRouter.SingleRoutePart<T>}` | `/hooks/contents/with-slug/${OneRouter.SingleRoutePart<T>}` | `/layouts/nested-layout/with-slug-layout-folder/${OneRouter.SingleRoutePart<T>}` | `/not-found/+not-found` | `/not-found/deep/+not-found` | `/routes/subpath/${string}` | `/segments-stable-ids/${string}` | `/spa/${OneRouter.SingleRoutePart<T>}` | `/ssr/${OneRouter.SingleRoutePart<T>}` | `/ssr/${string}`
DynamicRouteTemplate: `/dynamic-folder-routes/[serverId]/[channelId]` | `/hooks/contents/with-nested-slug/[folderSlug]` | `/hooks/contents/with-nested-slug/[folderSlug]/[fileSlug]` | `/hooks/contents/with-slug/[slug]` | `/layouts/nested-layout/with-slug-layout-folder/[layoutSlug]` | `/not-found/+not-found` | `/not-found/deep/+not-found` | `/routes/subpath/[...subpath]` | `/segments-stable-ids/[...segments]` | `/spa/[spaparams]` | `/ssr/[...rest]` | `/ssr/[param]`
IsTyped: true
Expand Down
Loading
Loading