Skip to content

Commit

Permalink
feat: support string literal dynamic imports
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 3, 2020
1 parent 63b4de6 commit 8ef6d4d
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/node/serverPluginModuleRewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,16 @@ function rewriteImports(
importeeMap.set(importer, currentImportees)

imports.forEach(({ s: start, e: end, d: dynamicIndex }) => {
const id = source.substring(start, end)
if (dynamicIndex === -1) {
let id = source.substring(start, end)
let hasLiteralDynamicId = false
if (dynamicIndex >= 0) {
const literalIdMatch = id.match(/^(?:'([^']+)'|"([^"]+)")$/)
if (literalIdMatch) {
hasLiteralDynamicId = true
id = literalIdMatch[1] || literalIdMatch[2]
}
}
if (dynamicIndex === -1 || hasLiteralDynamicId) {
let resolved
if (/^[^\/\.]/.test(id)) {
resolved = resolver.idToRequest(id) || `/@modules/${id}`
Expand Down Expand Up @@ -178,7 +186,11 @@ function rewriteImports(

if (resolved !== id) {
debug(` "${id}" --> "${resolved}"`)
s.overwrite(start, end, resolved)
s.overwrite(
start,
end,
hasLiteralDynamicId ? `'${resolved}'` : resolved
)
hasReplaced = true
}

Expand All @@ -187,9 +199,8 @@ function rewriteImports(
currentImportees.add(importee)
debugHmr(` ${importer} imports ${importee}`)
ensureMapEntry(importerMap, importee).add(importer)
} else if (dynamicIndex >= 0) {
// TODO dynamic import
debug(` dynamic import "${id}" (ignored)`)
} else {
console.log(`[vite] ignored dynamic import(${id})`)
}
})

Expand Down

0 comments on commit 8ef6d4d

Please sign in to comment.