Skip to content

Commit

Permalink
Treeshake functions properly (#2418)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic authored and devongovett committed Dec 17, 2018
1 parent cc33b97 commit e653e66
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
@@ -0,0 +1,2 @@
import {add} from './b';
export default add(4, 5);
@@ -0,0 +1,7 @@
export function add(a, b) {
return a + b;
}

export function sub(a, b) {
return a - b;
}
20 changes: 20 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -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(
Expand Down
13 changes: 7 additions & 6 deletions packages/core/parcel-bundler/src/scope-hoisting/shake.js
Expand Up @@ -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();
Expand All @@ -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();
}
Expand Down

0 comments on commit e653e66

Please sign in to comment.