-
-
Notifications
You must be signed in to change notification settings - Fork 115
vibe code server-only client-only #592
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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') | ||
| }) | ||
| }) |
| 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 {} |
| 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() | ||
| }) | ||
| }) | ||
| }) |
| 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'), | ||||||
| 'client-only': resolve(__dirname, '../client-only.js'), | ||||||
|
||||||
| 'client-only': resolve(__dirname, '../client-only.js'), | |
| 'client-only': resolve(__dirname, '../client-only.ts'), |
Copilot
AI
Jul 31, 2025
There was a problem hiding this comment.
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.
| return { id: resolve(__dirname, '../server-only.js'), external: false } | |
| return { id: resolve(__dirname, '../server-only.ts'), external: false } |
Copilot
AI
Jul 31, 2025
There was a problem hiding this comment.
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.
| return { id: resolve(__dirname, '../client-only.js'), external: false } | |
| return { id: resolve(__dirname, '../client-only.ts'), external: false } |
| 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 {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export {}; | ||
| //# sourceMappingURL=client-only.d.ts.map |
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export {}; | ||
| //# sourceMappingURL=server-only.d.ts.map |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"', | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
| }, | ||
|
|
||
| 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> | ||
| ) | ||
| } |
| 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> | ||
| ) | ||
| } |
There was a problem hiding this comment.
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.