Add destructuring case for modules-commonjs.

This commit is contained in:
Artem Yavorsky 2017-06-26 14:33:58 +03:00
parent 851d2cb6e0
commit ddba7ba89f

View File

@ -300,15 +300,43 @@ export default function () {
const id = decl.get("id");
const init = decl.get("init");
const exportsToInsert = [];
if (!init.node) init.replaceWith(t.identifier("undefined"));
if (id.isIdentifier()) {
addTo(exports, id.node.name, id.node);
init.replaceWith(buildExportsAssignment(id.node, init.node).expression);
nonHoistedExportNames[id.node.name] = true;
} else {
// todo
} else if (id.isObjectPattern()) {
for (let i = 0; i < id.node.properties.length; i++) {
const prop = id.node.properties[i];
let propValue = prop.value;
if (t.isAssignmentPattern(propValue)) {
propValue = propValue.left;
} else if (t.isRestProperty(prop)) {
propValue = prop.argument;
}
addTo(exports, propValue.name, propValue);
exportsToInsert.push(buildExportsAssignment(propValue, propValue));
nonHoistedExportNames[propValue.name] = true;
}
} else if (id.isArrayPattern() && id.node.elements) {
for (let i = 0; i < id.node.elements.length; i++) {
let elem = id.node.elements[i];
if (!elem) continue;
if (t.isAssignmentPattern(elem)) {
elem = elem.left;
} else if (t.isRestElement(elem)) {
elem = elem.argument;
}
const name = elem.name;
addTo(exports, name, elem);
exportsToInsert.push(buildExportsAssignment(elem, elem));
nonHoistedExportNames[name] = true;
}
}
path.insertAfter(exportsToInsert);
}
path.replaceWith(declaration.node);
}