Skip to content

Commit

Permalink
use toObjectNotation
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine du Hamel <[email protected]>
  • Loading branch information
hemanth and aduh95 authored May 30, 2023
1 parent 3bb9b4e commit c9a4d6b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c9a4d6b

Please sign in to comment.