1
+ import path from 'node:path'
1
2
import type { DevEnvironment , Plugin , Rollup } from 'vite'
2
3
3
4
// https://github.com/vercel/next.js/blob/90f564d376153fe0b5808eab7b83665ee5e08aaf/packages/next/src/build/webpack-config.ts#L1249-L1280
@@ -51,7 +52,11 @@ export function validateImportPlugin(): Plugin {
51
52
if ( this . environment . mode === 'dev' ) {
52
53
if ( id . startsWith ( `\0virtual:vite-rsc/validate-imports/invalid/` ) ) {
53
54
const chain = getImportChainDev ( this . environment , id )
54
- const error = formatError ( chain , this . environment . name )
55
+ const error = formatError (
56
+ chain ,
57
+ this . environment . name ,
58
+ this . environment . config . root ,
59
+ )
55
60
if ( error ) {
56
61
this . error ( {
57
62
id : chain [ 1 ] ,
@@ -69,15 +74,23 @@ export function validateImportPlugin(): Plugin {
69
74
this ,
70
75
'\0virtual:vite-rsc/validate-imports/invalid/server-only' ,
71
76
)
72
- const serverOnlyError = formatError ( serverOnly , this . environment . name )
77
+ const serverOnlyError = formatError (
78
+ serverOnly ,
79
+ this . environment . name ,
80
+ this . environment . config . root ,
81
+ )
73
82
if ( serverOnlyError ) {
74
83
throw new Error ( serverOnlyError )
75
84
}
76
85
const clientOnly = getImportChainBuild (
77
86
this ,
78
87
'\0virtual:vite-rsc/validate-imports/invalid/client-only' ,
79
88
)
80
- const clientOnlyError = formatError ( clientOnly , this . environment . name )
89
+ const clientOnlyError = formatError (
90
+ clientOnly ,
91
+ this . environment . name ,
92
+ this . environment . config . root ,
93
+ )
81
94
if ( clientOnlyError ) {
82
95
throw new Error ( clientOnlyError )
83
96
}
@@ -121,6 +134,7 @@ function getImportChainBuild(ctx: Rollup.PluginContext, id: string): string[] {
121
134
function formatError (
122
135
chain : string [ ] ,
123
136
environmentName : string ,
137
+ root : string ,
124
138
) : string | undefined {
125
139
if ( chain . length === 0 ) return
126
140
const id = chain [ 0 ] !
@@ -130,7 +144,9 @@ function formatError(
130
144
result += chain
131
145
. slice ( 1 , 6 )
132
146
. map (
133
- ( id , i ) => ' ' . repeat ( i + 1 ) + `imported by ${ id . replaceAll ( '\0' , '' ) } \n` ,
147
+ ( id , i ) =>
148
+ ' ' . repeat ( i + 1 ) +
149
+ `imported by ${ path . relative ( root , id ) . replaceAll ( '\0' , '' ) } \n` ,
134
150
)
135
151
. join ( '' )
136
152
if ( chain . length > 6 ) {
0 commit comments