From dc336a16350804e1bd991c9598ae73cec559a77e Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Sat, 15 Dec 2018 12:05:20 +0100 Subject: [PATCH 1/3] Add test for tree shaking functions properly --- .../es6/tree-shaking-functions/a.js | 2 ++ .../es6/tree-shaking-functions/b.js | 7 +++++++ .../parcel-bundler/test/scope-hoisting.js | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 packages/core/parcel-bundler/test/integration/scope-hoisting/es6/tree-shaking-functions/a.js create mode 100644 packages/core/parcel-bundler/test/integration/scope-hoisting/es6/tree-shaking-functions/b.js diff --git a/packages/core/parcel-bundler/test/integration/scope-hoisting/es6/tree-shaking-functions/a.js b/packages/core/parcel-bundler/test/integration/scope-hoisting/es6/tree-shaking-functions/a.js new file mode 100644 index 00000000000..6ed48a716b1 --- /dev/null +++ b/packages/core/parcel-bundler/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/parcel-bundler/test/integration/scope-hoisting/es6/tree-shaking-functions/b.js b/packages/core/parcel-bundler/test/integration/scope-hoisting/es6/tree-shaking-functions/b.js new file mode 100644 index 00000000000..5e77acdbdd6 --- /dev/null +++ b/packages/core/parcel-bundler/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/parcel-bundler/test/scope-hoisting.js b/packages/core/parcel-bundler/test/scope-hoisting.js index 5a37bda97d8..3bc3b341408 100644 --- a/packages/core/parcel-bundler/test/scope-hoisting.js +++ b/packages/core/parcel-bundler/test/scope-hoisting.js @@ -433,6 +433,25 @@ describe('scope hoisting', function() { assert(!contents.includes('bar')); }); + it('removes unused function exports', async function() { + let b = await bundle( + path.join( + __dirname, + '/integration/scope-hoisting/es6/tree-shaking-functions/a.js' + ) + ); + + let output = await run(b); + assert.deepEqual(output.default, 9); + + let contents = await fs.readFile( + path.join(__dirname, '/dist/a.js'), + 'utf8' + ); + assert(contents.includes('add')); + assert(!contents.includes('sub')); + }); + it('support exporting a ES6 module exported as CommonJS', async function() { let b = await bundle( path.join( From d63a46e05b8a633d82098a8a07384f866dc82670 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Sat, 15 Dec 2018 12:05:54 +0100 Subject: [PATCH 2/3] Tree shake multiple es6 function exports --- .../core/parcel-bundler/src/scope-hoisting/shake.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) 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(); } From f23550b3dfe668dd46643dbcf86b9ce22a4a2134 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig Date: Sat, 15 Dec 2018 13:05:24 +0100 Subject: [PATCH 3/3] Make test meaningfull --- packages/core/parcel-bundler/test/scope-hoisting.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core/parcel-bundler/test/scope-hoisting.js b/packages/core/parcel-bundler/test/scope-hoisting.js index 3bc3b341408..139397dcf07 100644 --- a/packages/core/parcel-bundler/test/scope-hoisting.js +++ b/packages/core/parcel-bundler/test/scope-hoisting.js @@ -433,12 +433,13 @@ describe('scope hoisting', function() { assert(!contents.includes('bar')); }); - it('removes unused function exports', async function() { + 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); @@ -448,8 +449,8 @@ describe('scope hoisting', function() { path.join(__dirname, '/dist/a.js'), 'utf8' ); - assert(contents.includes('add')); - assert(!contents.includes('sub')); + assert(/.\+./.test(contents)); + assert(!/.-./.test(contents)); }); it('support exporting a ES6 module exported as CommonJS', async function() {