diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-functions/a.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-functions/a.js new file mode 100644 index 00000000000..6ed48a716b1 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-functions/a.js @@ -0,0 +1,2 @@ +import {add} from './b'; +export default add(4, 5); diff --git a/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-functions/b.js b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-functions/b.js new file mode 100644 index 00000000000..5e77acdbdd6 --- /dev/null +++ b/packages/core/integration-tests/test/integration/scope-hoisting/es6/tree-shaking-functions/b.js @@ -0,0 +1,7 @@ +export function add(a, b) { + return a + b; +} + +export function sub(a, b) { + return a - b; +} diff --git a/packages/core/integration-tests/test/scope-hoisting.js b/packages/core/integration-tests/test/scope-hoisting.js index 5a37bda97d8..139397dcf07 100644 --- a/packages/core/integration-tests/test/scope-hoisting.js +++ b/packages/core/integration-tests/test/scope-hoisting.js @@ -433,6 +433,26 @@ describe('scope hoisting', function() { assert(!contents.includes('bar')); }); + it('removes unused function exports when minified', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/es6/tree-shaking-functions/a.js' + ), + {minify: true} + ); + + let output = await run(b); + assert.deepEqual(output.default, 9); + + let contents = await fs.readFile( + path.join(__dirname, '/dist/a.js'), + 'utf8' + ); + assert(/.\+./.test(contents)); + assert(!/.-./.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 231fd169baf..0e736e9a17a 100644 --- a/packages/core/parcel-bundler/src/scope-hoisting/shake.js +++ b/packages/core/parcel-bundler/src/scope-hoisting/shake.js @@ -104,7 +104,13 @@ function isUnusedWildcard(path) { function remove(path) { if (path.isAssignmentExpression()) { - if (!path.parentPath.isExpressionStatement()) { + if (path.parentPath.isSequenceExpression()) { + if (path.parent.expressions.length == 1) { + path.parentPath.remove(); + } else { + path.remove(); + } + } else if (!path.parentPath.isExpressionStatement()) { path.replaceWith(path.node.right); } else { path.remove(); @@ -113,11 +119,6 @@ function remove(path) { remove(path.parentPath.parentPath); } else if (isUnusedWildcard(path)) { remove(path.parentPath); - } else if ( - path.parentPath.isSequenceExpression() && - path.parent.expressions.length === 1 - ) { - remove(path.parentPath); } else if (!path.removed) { path.remove(); }