diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js new file mode 100644 index 00000000000..00fea395d12 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js @@ -0,0 +1,3 @@ +(function(){ + output = require('./b'); +})(); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js new file mode 100644 index 00000000000..71411c79d3b --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/b.js @@ -0,0 +1,4 @@ +const x = [1, 2] +const [a, b] = x; + +module.exports = [a, b]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/package.json new file mode 100644 index 00000000000..93281f0b785 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-array/package.json @@ -0,0 +1,4 @@ +{ + "private": true, + "browserslist": ["node 8"] +} diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js new file mode 100644 index 00000000000..00fea395d12 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js @@ -0,0 +1,3 @@ +(function(){ + output = require('./b'); +})(); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/b.js new file mode 100644 index 00000000000..321586ca6d8 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/b.js @@ -0,0 +1,7 @@ +const x = { + a: 4, + b: 2 +} +const {a, b} = x; + +module.exports = [a, b]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json new file mode 100644 index 00000000000..93281f0b785 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/commonjs/wrap-destructuring-object/package.json @@ -0,0 +1,4 @@ +{ + "private": true, + "browserslist": ["node 8"] +} diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 30cb58191de..70e7fea09b0 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -1275,5 +1275,29 @@ describe('scope hoisting', function() { let output = await run(b); assert.equal(output, true); }); + + it('should support wrapping array destructuring declarations', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/commonjs/wrap-destructuring-array/a.js' + ) + ); + + let output = await run(b); + assert.deepEqual(output, [1, 2]); + }); + + it('should support wrapping object destructuring declarations', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/commonjs/wrap-destructuring-object/a.js' + ) + ); + + let output = await run(b); + assert.deepEqual(output, [4, 2]); + }); }); }); diff --git a/packages/shared/scope-hoisting/src/concat.js b/packages/shared/scope-hoisting/src/concat.js index eabbd269838..66a67b0b5ba 100644 --- a/packages/shared/scope-hoisting/src/concat.js +++ b/packages/shared/scope-hoisting/src/concat.js @@ -271,13 +271,30 @@ function wrapModule(asset: Asset, statements) { // so that they can be referenced by other modules directly. if (t.isVariableDeclaration(node)) { for (let decl of node.declarations) { - decls.push(t.variableDeclarator(decl.id)); - if (decl.init) { - body.push( - t.expressionStatement( - t.assignmentExpression('=', t.identifier(decl.id.name), decl.init) - ) - ); + if (t.isObjectPattern(decl.id) || t.isArrayPattern(decl.id)) { + for (let prop of Object.values(t.getBindingIdentifiers(decl.id))) { + decls.push(t.variableDeclarator(prop)); + } + if (decl.init) { + body.push( + t.expressionStatement( + t.assignmentExpression('=', decl.id, decl.init) + ) + ); + } + } else { + decls.push(t.variableDeclarator(decl.id)); + if (decl.init) { + body.push( + t.expressionStatement( + t.assignmentExpression( + '=', + t.identifier(decl.id.name), + decl.init + ) + ) + ); + } } } } else if (t.isFunctionDeclaration(node)) {