Skip to content

Commit 85f1e63

Browse files
committed
fix: fix copilot
1 parent e762f57 commit 85f1e63

File tree

4 files changed

+28
-83
lines changed

4 files changed

+28
-83
lines changed

packages/plugin-rsc/e2e/validate-imports.test.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@ test.describe('validate imports', () => {
1111
src: 'examples/starter',
1212
dest: root,
1313
files: {
14-
'package.json': {
15-
edit: (content) => {
16-
const pkg = JSON.parse(content)
17-
// Add package.json overrides like setupIsolatedFixture
18-
const packagesDir = path.join(import.meta.dirname, '..', '..')
19-
const overrides = {
20-
'@vitejs/plugin-rsc': `file:${path.join(packagesDir, 'plugin-rsc')}`,
21-
'@vitejs/plugin-react': `file:${path.join(packagesDir, 'plugin-react')}`,
22-
}
23-
Object.assign(((pkg.pnpm ??= {}).overrides ??= {}), overrides)
24-
// Add external dependencies that are managed in examples/e2e/package.json
25-
pkg.dependencies = {
26-
...pkg.dependencies,
27-
'server-only': '^0.0.1',
28-
}
29-
return JSON.stringify(pkg, null, 2)
30-
},
31-
},
3214
'src/client.tsx': /* tsx */ `
3315
"use client";
3416
import 'server-only';
@@ -56,15 +38,6 @@ test.describe('validate imports', () => {
5638
},
5739
})
5840

59-
// Install dependencies
60-
await x('pnpm', ['i'], {
61-
throwOnError: true,
62-
nodeOptions: {
63-
cwd: root,
64-
stdio: 'ignore',
65-
},
66-
})
67-
6841
// Expect build to fail
6942
const result = await x('pnpm', ['build'], {
7043
throwOnError: false,

packages/plugin-rsc/examples/e2e/package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
"name": "@vitejs/plugin-rsc-examples-e2e",
33
"private": true,
44
"type": "module",
5-
"dependencies": {
6-
"client-only": "^0.0.1",
7-
"server-only": "^0.0.1"
8-
},
95
"devDependencies": {
106
"@vitejs/plugin-rsc": "latest",
117
"@vitejs/plugin-react": "latest",

packages/plugin-rsc/src/plugin.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,46 +1963,39 @@ export function __fix_cloudflare(): Plugin {
19631963
function validateImportPlugin(): Plugin {
19641964
return {
19651965
name: 'rsc:validate-imports',
1966-
enforce: 'pre',
1967-
async resolveId(source, importer, options) {
1968-
// skip validation during optimizeDeps scan since for now
1969-
// we want to allow going through server/client boundary loosely
1970-
if (isScanBuild || ('scan' in options && options.scan)) {
1971-
return
1972-
}
1966+
resolveId: {
1967+
order: 'pre',
1968+
async handler(source, importer, options) {
1969+
// optimizer is not aware of server/client boudnary so skip
1970+
if ('scan' in options && options.scan) {
1971+
return
1972+
}
19731973

1974-
// Validate client-only imports in server environments
1975-
if (
1976-
source === 'client-only' &&
1977-
(this.environment.name === 'rsc' || this.environment.name === 'ssr')
1978-
) {
1979-
// Allow client-only in client components during SSR builds
1980-
if (importer && this.environment.name === 'ssr') {
1981-
try {
1982-
const code = await this.load({ id: importer })
1983-
if (
1984-
code?.code?.includes('"use client"') ||
1985-
code?.code?.includes("'use client'")
1986-
) {
1987-
return
1988-
}
1989-
} catch (e) {
1990-
// If we can't load the importer, proceed with validation
1974+
// Validate client-only imports in server environments
1975+
if (source === 'client-only') {
1976+
if (this.environment.name === 'rsc') {
1977+
throw new Error(
1978+
`'client-only' cannot be imported in server build (importer: '${importer ?? 'unknown'}', environment: ${this.environment.name})`,
1979+
)
19911980
}
1981+
return { id: `\0virtual:vite-rsc/empty`, moduleSideEffects: false }
1982+
}
1983+
if (source === 'server-only') {
1984+
if (this.environment.name !== 'rsc') {
1985+
throw new Error(
1986+
`'server-only' cannot be imported in client build (importer: '${importer ?? 'unknown'}', environment: ${this.environment.name})`,
1987+
)
1988+
}
1989+
return { id: `\0virtual:vite-rsc/empty`, moduleSideEffects: false }
19921990
}
1993-
throw new Error(
1994-
`'client-only' is included in server build (importer: ${importer ?? 'unknown'})`,
1995-
)
1996-
}
19971991

1998-
// Validate server-only imports in client environment
1999-
if (source === 'server-only' && this.environment.name === 'client') {
2000-
throw new Error(
2001-
`'server-only' is included in client build (importer: ${importer ?? 'unknown'})`,
2002-
)
1992+
return
1993+
},
1994+
},
1995+
load(id) {
1996+
if (id.startsWith('\0virtual:vite-rsc/empty')) {
1997+
return `export {}`
20031998
}
2004-
2005-
return
20061999
},
20072000
}
20082001
}

pnpm-lock.yaml

Lines changed: 0 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)