Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shake exports with pure property assignments #2979

Merged
merged 2 commits into from May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,3 @@
import {foo} from './b';

output = foo;
@@ -0,0 +1,4 @@
export const foo = 2;

export function bar() {}
bar.displayName = 'hello';
19 changes: 19 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Expand Up @@ -549,6 +549,25 @@ describe('scope hoisting', function() {
let output = await run(b);
assert.deepEqual(output, 'bar');
});

it('should shake pure property assignments', async function() {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/pure-assignment/a.js'
)
);

let output = await run(b);
assert.deepEqual(output, 2);

let contents = await fs.readFile(
path.join(__dirname, 'dist/a.js'),
'utf8'
);
assert(!/bar/.test(contents));
assert(!/displayName/.test(contents));
});
});

describe('commonjs', function() {
Expand Down
30 changes: 15 additions & 15 deletions packages/core/parcel-bundler/src/scope-hoisting/shake.js
@@ -1,7 +1,5 @@
const t = require('@babel/types');

const EXPORTS_RE = /^\$([^$]+)\$exports$/;

/**
* This is a small small implementation of dead code removal specialized to handle
* removing unused exports. All other dead code removal happens in workers on each
Expand Down Expand Up @@ -44,31 +42,24 @@ function getUnusedBinding(path, name) {
return null;
}

if (isPure(binding)) {
let pure = isPure(binding);
if (!binding.referenced && pure) {
return binding;
}

if (!EXPORTS_RE.test(name)) {
return null;
}

// Is there any references which aren't simple assignments?
let bailout = binding.referencePaths.some(
path => !isExportAssignment(path) && !isUnusedWildcard(path)
);

if (bailout) {
return null;
} else {
if (!bailout && pure) {
return binding;
}

return null;
}

function isPure(binding) {
if (binding.referenced) {
return false;
}

if (
binding.path.isVariableDeclarator() &&
binding.path.get('id').isIdentifier()
Expand Down Expand Up @@ -122,6 +113,15 @@ function remove(path) {
} else if (isUnusedWildcard(path)) {
remove(path.parentPath);
} else if (!path.removed) {
path.remove();
if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code duplication isn't ideal but I don't see a cleaner way without doing some extra if checks

path.parentPath.isSequenceExpression() &&
path.parent.expressions.length === 1
) {
// replace sequence expression with it's sole child
path.parentPath.replaceWith(path);
remove(path.parentPath);
} else {
path.remove();
}
}
}