Skip to content

Commit

Permalink
feat: improve dynamic import variable failure error message (#16519)
Browse files Browse the repository at this point in the history
  • Loading branch information
btea authored May 2, 2024
1 parent c071eb3 commit f8feeea
Showing 1 changed file with 16 additions and 3 deletions.
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 @@ -247,7 +260,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
s.overwrite(
expStart,
expEnd,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, ${rawPattern.split('/').length})`,
)
}

Expand Down

0 comments on commit f8feeea

Please sign in to comment.