Skip to content

Commit 372a0e7

Browse files
nihgwuhi-ogawaOpenCode
authored
fix(rsc): pass root importer explicitly instead of undefined for this.resolve in rsc:virtual-client-package (#1295)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com> Co-authored-by: Hiroshi Ogawa <4232207+hi-ogawa@users.noreply.github.com> Co-authored-by: OpenCode <noreply@opencode.ai>
1 parent 0560cd9 commit 372a0e7

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

packages/plugin-rsc/examples/basic/vite.config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export default defineConfig({
3232
react(),
3333
vitePluginUseCache(),
3434
vitePluginVirtualModuleTest(),
35+
testRscVirtualClientPackagePlugin(),
3536
rsc({
3637
entries: {
3738
client: './src/framework/entry.browser.tsx',
@@ -271,6 +272,25 @@ export default { fetch: handler };
271272
},
272273
}) as any
273274

275+
function testRscVirtualClientPackagePlugin(): Plugin {
276+
return {
277+
name: 'test-rsc-virtual-client-package',
278+
enforce: 'pre',
279+
resolveId(source, importer) {
280+
// `rsc:virtual-client-package` should pass an explicit root importer.
281+
if (
282+
this.environment.name === 'rsc' &&
283+
source === '@vitejs/test-dep-transitive-client/client' &&
284+
importer == null
285+
) {
286+
throw new Error(
287+
'`rsc:virtual-client-package` root resolution requires an importer',
288+
)
289+
}
290+
},
291+
}
292+
}
293+
274294
function testBuildPlugin(): Plugin[] {
275295
const moduleIds: { name: string; ids: string[] }[] = []
276296
return [

packages/plugin-rsc/src/plugin.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1720,9 +1720,15 @@ function vitePluginUseClient(
17201720
// transitive dependency reachable from `importer` but not installed
17211721
// at the root (see #1247). In that case, skip virtualization and
17221722
// let it fall back to referencing the fully resolved module id.
1723+
// Vite's resolver already treats an undefined importer as the project
1724+
// root, but other plugins may not, so pass the root importer explicitly.
1725+
const rootImporter = path.join(
1726+
this.environment.config.root,
1727+
'index.html',
1728+
)
17231729
const resolvedAtRoot = await this.resolve(
17241730
source,
1725-
undefined,
1731+
rootImporter,
17261732
options,
17271733
)
17281734
if (!resolvedAtRoot || resolvedAtRoot.id !== resolved.id) {

0 commit comments

Comments
 (0)