Skip to content
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, '')
})
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