diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index d263852609c91d..6c042377c60e07 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -98,12 +98,16 @@ export async function transformRequest( } } if (code == null) { - const msg = checkPublicFile(url, root) - ? `This file is in /public and will be copied as-is during build without ` + - `going through the plugin transforms, and therefore should not be ` + - `imported from source code. It can only be referenced via HTML tags.` - : `Does the file exist?` - throw new Error(`Failed to load url ${url} (resolved id: ${id}). ${msg}`) + if (checkPublicFile(url, root)) { + throw new Error( + `Failed to load url ${url} (resolved id: ${id}). ` + + `This file is in /public and will be copied as-is during build without ` + + `going through the plugin transforms, and therefore should not be ` + + `imported from source code. It can only be referenced via HTML tags.` + ) + } else { + return null + } } // ensure module in graph after successful load