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 46b470489db..2b8c8d405d4 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -1229,5 +1229,29 @@ describe('scope hoisting', function() { let output = await run(b); assert.deepEqual(output, 2); }); + + 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/core/parcel-bundler/src/packagers/JSConcatPackager.js b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js index 2c26b52d22a..f4504cbfc5d 100644 --- a/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js +++ b/packages/core/parcel-bundler/src/packagers/JSConcatPackager.js @@ -283,17 +283,30 @@ class JSConcatPackager extends Packager { // 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)) {