Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve dynamic import variable failure error message #16519

Merged
merged 3 commits into from
May 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions packages/vite/src/node/plugins/dynamicImportVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,27 @@ interface DynamicImportPattern {
rawPattern: string
}

const dynamicImportHelper = (glob: Record<string, any>, path: string) => {
const dynamicImportHelper = (
glob: Record<string, any>,
path: string,
segs: number,
) => {
const v = glob[path]
if (v) {
return typeof v === 'function' ? v() : Promise.resolve(v)
}
return new Promise((_, reject) => {
;(typeof queueMicrotask === 'function' ? queueMicrotask : setTimeout)(
reject.bind(null, new Error('Unknown variable dynamic import: ' + path)),
reject.bind(
null,
new Error(
'Unknown variable dynamic import: ' +
path +
(path.split('/').length !== segs
? '. Note that variables only represent file names one level deep.'
: ''),
),
),
)
})
}
Expand Down Expand Up @@ -246,7 +259,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
s.overwrite(
expStart,
expEnd,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, ${rawPattern.split('/').length})`,
)
}

Expand Down