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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 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,31 @@ interface DynamicImportPattern {
rawPattern: string
}

const dynamicImportHelper = (glob: Record<string, any>, path: string) => {
const dynamicImportHelper = (
glob: Record<string, any>,
path: string,
rawPath: string,
) => {
const v = glob[path]
if (v) {
return typeof v === 'function' ? v() : Promise.resolve(v)
}
let _path = path
rawPath.split(/\$\{.+\}/).forEach((str) => {
_path = _path.replace(str, '')
})
bluwy marked this conversation as resolved.
Show resolved Hide resolved
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.includes('/')
? '. Note that variables only represent file names one level deep.'
: ''),
),
),
)
})
}
Expand Down Expand Up @@ -246,7 +263,7 @@ export function dynamicImportVarsPlugin(config: ResolvedConfig): Plugin {
s.overwrite(
expStart,
expEnd,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`)`,
`__variableDynamicImportRuntimeHelper(${glob}, \`${rawPattern}\`, "${rawPattern}")`,
)
}

Expand Down