From d78225f440a99e0c6d43679212e1e5b874443cac Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Wed, 6 Mar 2019 23:06:20 +0100 Subject: [PATCH] Make treeshaking handle "multiassignments" --- .../es6/tree-shaking-multiassignment/a.js | 2 ++ .../es6/tree-shaking-multiassignment/b.js | 9 +++++++++ .../integration-tests/test/scope-hoisting.js | 19 +++++++++++++++++++ .../src/scope-hoisting/shake.js | 4 +++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/a.js create mode 100644 packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/b.js diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/a.js new file mode 100644 index 00000000000..9293a7b2545 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/a.js @@ -0,0 +1,2 @@ +import x, {y} from "./b"; +export default [x, y]; diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/b.js new file mode 100644 index 00000000000..0204ca5f91a --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-multiassignment/b.js @@ -0,0 +1,9 @@ +exports.default = exports.y = exports.z = undefined; +var y = 'y'; +exports.y = y; +var z = 'z'; +exports.z = z; + +Object.defineProperty(exports, "__esModule", { + value: true +}); diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index da271fece17..df12a4a00ec 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -485,6 +485,25 @@ describe('scope hoisting', function() { assert(!/.-./.test(contents)); }); + it('handle export multi-assignment', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/es6/tree-shaking-multiassignment/a.js' + ), + {minify: true} + ); + + let output = await run(b); + assert.deepEqual(output.default, [undefined, 'y']); + + // let contents = await fs.readFile( + // path.join(__dirname, '/dist/a.js'), + // 'utf8' + // ); + // assert(!/["']z["']/.test(contents)); + }); + it('support exporting a ES6 module exported as CommonJS', async function() { let b = await bundle( path.join( diff --git a/packages/core/parcel-bundler/src/scope-hoisting/shake.js b/packages/core/parcel-bundler/src/scope-hoisting/shake.js index 0e736e9a17a..d8f37b09e8a 100644 --- a/packages/core/parcel-bundler/src/scope-hoisting/shake.js +++ b/packages/core/parcel-bundler/src/scope-hoisting/shake.js @@ -106,7 +106,9 @@ function remove(path) { if (path.isAssignmentExpression()) { if (path.parentPath.isSequenceExpression()) { if (path.parent.expressions.length == 1) { - path.parentPath.remove(); + // replace sequence expression with it's sole child + path.parentPath.replaceWith(path); + remove(path.parentPath); } else { path.remove(); }