Skip to content

Commit c9a8ff6

Browse files
committed
fix: add module id to error
1 parent c7295d8 commit c9a8ff6

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

packages/plugin-rsc/src/plugins/validate-import.ts

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,11 @@ export function validateImportPlugin(): Plugin {
5252
if (this.environment.mode === 'dev') {
5353
if (id.startsWith(`\0virtual:vite-rsc/validate-imports/invalid/`)) {
5454
const chain = getImportChainDev(this.environment, id)
55-
const error = formatError(
55+
validateImportChain(
5656
chain,
5757
this.environment.name,
5858
this.environment.config.root,
5959
)
60-
if (error) {
61-
this.error({
62-
id: chain[1],
63-
message: error,
64-
})
65-
}
6660
}
6761
}
6862
},
@@ -75,26 +69,20 @@ export function validateImportPlugin(): Plugin {
7569
this,
7670
'\0virtual:vite-rsc/validate-imports/invalid/server-only',
7771
)
78-
const serverOnlyError = formatError(
72+
validateImportChain(
7973
serverOnly,
8074
this.environment.name,
8175
this.environment.config.root,
8276
)
83-
if (serverOnlyError) {
84-
throw new Error(serverOnlyError)
85-
}
8677
const clientOnly = getImportChainBuild(
8778
this,
8879
'\0virtual:vite-rsc/validate-imports/invalid/client-only',
8980
)
90-
const clientOnlyError = formatError(
81+
validateImportChain(
9182
clientOnly,
9283
this.environment.name,
9384
this.environment.config.root,
9485
)
95-
if (clientOnlyError) {
96-
throw new Error(clientOnlyError)
97-
}
9886
}
9987
},
10088
}
@@ -132,11 +120,11 @@ function getImportChainBuild(ctx: Rollup.PluginContext, id: string): string[] {
132120
return chain
133121
}
134122

135-
function formatError(
123+
function validateImportChain(
136124
chain: string[],
137125
environmentName: string,
138126
root: string,
139-
): string | undefined {
127+
) {
140128
if (chain.length === 0) return
141129
const id = chain[0]!
142130
const source = id.slice(id.lastIndexOf('/') + 1)
@@ -153,5 +141,9 @@ function formatError(
153141
if (chain.length > 6) {
154142
result += ' '.repeat(7) + '...\n'
155143
}
156-
return result
144+
const error = new Error(result)
145+
if (chain[1]) {
146+
Object.assign(error, { id: chain[1] })
147+
}
148+
throw error
157149
}

0 commit comments

Comments
 (0)