Skip to content

Commit 70729c0

Browse files
thomasskkbluwy
andauthoredFeb 18, 2023
feat(ssr): add importer path to error msg when invalid url import occur (#11606)
Co-authored-by: bluwy <bjornlu.dev@gmail.com>
1 parent aab4f13 commit 70729c0

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed
 

‎packages/vite/src/node/server/transformRequest.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import getEtag from 'etag'
55
import convertSourceMap from 'convert-source-map'
66
import type { SourceDescription, SourceMap } from 'rollup'
77
import colors from 'picocolors'
8-
import type { ViteDevServer } from '..'
8+
import type { ModuleNode, ViteDevServer } from '..'
99
import {
1010
blankReplacer,
1111
cleanUrl,
@@ -227,8 +227,15 @@ async function loadAndTransform(
227227
`going through the plugin transforms, and therefore should not be ` +
228228
`imported from source code. It can only be referenced via HTML tags.`
229229
: `Does the file exist?`
230+
const importerMod: ModuleNode | undefined = server.moduleGraph.idToModuleMap
231+
.get(id)
232+
?.importers.values()
233+
.next().value
234+
const importer = importerMod?.file || importerMod?.url
230235
const err: any = new Error(
231-
`Failed to load url ${url} (resolved id: ${id}). ${msg}`,
236+
`Failed to load url ${url} (resolved id: ${id})${
237+
importer ? ` in ${importer}` : ''
238+
}. ${msg}`,
232239
)
233240
err.code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL
234241
throw err

‎packages/vite/src/node/ssr/__tests__/ssrLoadModule.spec.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { fileURLToPath } from 'node:url'
2+
import path from 'node:path'
23
import { expect, test } from 'vitest'
34
import { createServer } from '../../server'
5+
import { normalizePath } from '../../utils'
46

57
const root = fileURLToPath(new URL('./', import.meta.url))
68

@@ -13,11 +15,13 @@ async function createDevServer() {
1315
test('ssrLoad', async () => {
1416
expect.assertions(1)
1517
const server = await createDevServer()
18+
const moduleRelativePath = '/fixtures/modules/has-invalid-import.js'
19+
const moduleAbsolutePath = normalizePath(path.join(root, moduleRelativePath))
1620
try {
17-
await server.ssrLoadModule('/fixtures/modules/has-invalid-import.js')
21+
await server.ssrLoadModule(moduleRelativePath)
1822
} catch (e) {
1923
expect(e.message).toBe(
20-
'Failed to load url ./non-existent.js (resolved id: ./non-existent.js). Does the file exist?',
24+
`Failed to load url ./non-existent.js (resolved id: ./non-existent.js) in ${moduleAbsolutePath}. Does the file exist?`,
2125
)
2226
}
2327
})

0 commit comments

Comments
 (0)
Please sign in to comment.