diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index 5574550a90..1ae00f1c3c 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -300,6 +300,7 @@ 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()) { @@ -310,14 +311,25 @@ export default function () { for (let i = 0; i < id.node.properties.length; i++) { const prop = id.node.properties[i]; if (!t.isRestProperty(prop)) { - addTo(exports, prop.value.name, prop.value); - path.insertAfter(buildExportsAssignment(prop.value, prop.value)); - nonHoistedExportNames[prop.value.name] = true; + const propValue = prop.value; + 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++) { + const elem = id.node.elements[i]; + if (!elem) continue; + if (!t.isRestElement(elem)) { + const name = elem.name; + addTo(exports, name, elem); + exportsToInsert.push(buildExportsAssignment(elem, elem)); + nonHoistedExportNames[name] = true; } } - } else { - // todo } + path.insertAfter(exportsToInsert); } path.replaceWith(declaration.node); } diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-array/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-array/actual.js new file mode 100644 index 0000000000..1b7752d6c5 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-array/actual.js @@ -0,0 +1 @@ +export const [ foo, bar ] = []; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-array/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-array/expected.js new file mode 100644 index 0000000000..e2fafbd5ca --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-array/expected.js @@ -0,0 +1,5 @@ +"use strict"; + +const [foo, bar] = []; +exports.foo = foo; +exports.bar = bar; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object/actual.js similarity index 100% rename from packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring/actual.js rename to packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object/actual.js diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object/expected.js similarity index 100% rename from packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring/expected.js rename to packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/strict/export-const-destructuring-object/expected.js