@@ -52,17 +52,11 @@ export function validateImportPlugin(): Plugin {
52
52
if ( this . environment . mode === 'dev' ) {
53
53
if ( id . startsWith ( `\0virtual:vite-rsc/validate-imports/invalid/` ) ) {
54
54
const chain = getImportChainDev ( this . environment , id )
55
- const error = formatError (
55
+ validateImportChain (
56
56
chain ,
57
57
this . environment . name ,
58
58
this . environment . config . root ,
59
59
)
60
- if ( error ) {
61
- this . error ( {
62
- id : chain [ 1 ] ,
63
- message : error ,
64
- } )
65
- }
66
60
}
67
61
}
68
62
} ,
@@ -75,26 +69,20 @@ export function validateImportPlugin(): Plugin {
75
69
this ,
76
70
'\0virtual:vite-rsc/validate-imports/invalid/server-only' ,
77
71
)
78
- const serverOnlyError = formatError (
72
+ validateImportChain (
79
73
serverOnly ,
80
74
this . environment . name ,
81
75
this . environment . config . root ,
82
76
)
83
- if ( serverOnlyError ) {
84
- throw new Error ( serverOnlyError )
85
- }
86
77
const clientOnly = getImportChainBuild (
87
78
this ,
88
79
'\0virtual:vite-rsc/validate-imports/invalid/client-only' ,
89
80
)
90
- const clientOnlyError = formatError (
81
+ validateImportChain (
91
82
clientOnly ,
92
83
this . environment . name ,
93
84
this . environment . config . root ,
94
85
)
95
- if ( clientOnlyError ) {
96
- throw new Error ( clientOnlyError )
97
- }
98
86
}
99
87
} ,
100
88
}
@@ -132,11 +120,11 @@ function getImportChainBuild(ctx: Rollup.PluginContext, id: string): string[] {
132
120
return chain
133
121
}
134
122
135
- function formatError (
123
+ function validateImportChain (
136
124
chain : string [ ] ,
137
125
environmentName : string ,
138
126
root : string ,
139
- ) : string | undefined {
127
+ ) {
140
128
if ( chain . length === 0 ) return
141
129
const id = chain [ 0 ] !
142
130
const source = id . slice ( id . lastIndexOf ( '/' ) + 1 )
@@ -153,5 +141,9 @@ function formatError(
153
141
if ( chain . length > 6 ) {
154
142
result += ' ' . repeat ( 7 ) + '...\n'
155
143
}
156
- return result
144
+ const error = new Error ( result )
145
+ if ( chain [ 1 ] ) {
146
+ Object . assign ( error , { id : chain [ 1 ] } )
147
+ }
148
+ throw error
157
149
}
0 commit comments