diff --git a/lib/repl.js b/lib/repl.js index 368d442c3b6e8d..728d0e07771d47 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -237,15 +237,16 @@ const toDynamicImport = (codeLine) => { const ast = acornParse(codeLine, { sourceType: 'module', ecmaVersion: 'latest' }); acornWalk.ancestor(ast, { ImportDeclaration: (node) => { - const importedModules = node.source.value; - const importedSpecifiers = node.specifiers.map((specifier) => specifier.local.name); - if (importedSpecifiers.length > 1) { - moduleName = `{${importedSpecifiers.join(',')}}`; + const awaitDynamicImport = `await import(${JSONStringify(node.source.value)});`; + if (node.specifiers.length === 0) { + dynamicImportStatement += awaitDynamicImport; } else { - const formattedSpecifiers = importedSpecifiers.length ? ArrayPrototypeToString(importedSpecifiers) : ''; - moduleName = toCamelCase(formattedSpecifiers || importedModules); + const toObjectNotation = ({ local, imported }) => local.name === imported.name ? local.name : `${imported.name}: ${local.name}`; + const importNames = node.specifiers.length === 1 ? + toObjectNotation(node.specifiers[0]) : + ArrayPrototypeJoin(ArrayPrototypeMap(node.specifiers, toObjectNotation), ', '); + dynamicImportStatement += `const ${importNames} = ${awaitDynamicImport}`; } - dynamicImportStatement += `const ${moduleName} = await import('${importedModules}');`; }, }); return dynamicImportStatement;