Skip to content

Commit f8feeea

Browse files
authoredMay 2, 2024··
feat: improve dynamic import variable failure error message (#16519)
1 parent c071eb3 commit f8feeea

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed
 

‎packages/vite/src/node/plugins/dynamicImportVars.ts

+16-3
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,27 @@ interface DynamicImportPattern {
3939
rawPattern: string
4040
}
4141

42-
const dynamicImportHelper = (glob: Record<string, any>, path: string) => {
42+
const dynamicImportHelper = (
43+
glob: Record<string, any>,
44+
path: string,
45+
segs: number,
46+
) => {
4347
const v = glob[path]
4448
if (v) {
4549
return typeof v === 'function' ? v() : Promise.resolve(v)
4650
}
4751
return new Promise((_, reject) => {
4852
;(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(
49-
reject.bind(null, new Error('Unknown variable dynamic import: ' + path)),
53+
reject.bind(
54+
null,
55+
new Error(
56+
'Unknown variable dynamic import: ' +
57+
path +
58+
(path.split('/').length !== segs
59+
? '. Note that variables only represent file names one level deep.'
60+
: ''),
61+
),
62+
),
5063
)
5164
})
5265
}
@@ -247,7 +260,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
247260
s.overwrite(
248261
expStart,
249262
expEnd,
250-
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`,
263+
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, ${rawPattern.split('/').length})`,
251264
)
252265
}
253266

0 commit comments

Comments
 (0)
Please sign in to comment.