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

Treeshake functions properly #2418

Merged
merged 4 commits into from
Dec 17, 2018

Conversation

mischnic
Copy link
Member

↪️ Pull Request

This fixes an issue where functions weren't getting treeshaken if two or more function were exported.
This doesn't seem to have a big effect on larger apps.

Closes #2414, closes #2297

🔦 Context

Before treeshake() runs in the concat packager, this is the generated asset javascript:

	var $RK1r$exports = {};
	function $RK1r$export$add(r, $) {
		return r + $;
	}
	function $RK1r$export$sub(r, $) {
		return r - $;
	}
	($RK1r$exports.add = $RK1r$export$add),
		($RK1r$exports.sub = $RK1r$export$sub);

After treeshaking: (neither add nor sub are actually used!)

	function $RK1r$export$add(r, $) {
		return r + $;
	}
	function $RK1r$export$sub(r, $) {
		return r - $;
	}
	$RK1r$export$add, $RK1r$export$sub;

Notice how ($RK1r$exports.add = $RK1r$export$add), ($RK1r$exports.sub = $RK1r$export$sub); prevented the sub function from being removed.

That line was introduced by running terser in JSAsset:

// ...
$RK1r$exports.add = $RK1r$export$add;
//...
$RK1r$exports.sub = $RK1r$export$sub;

becomes:

($RK1r$exports.add = $RK1r$export$add), ($RK1r$exports.sub = $RK1r$export$sub);

That comma expression isn't getting handled properly by the treeshaker.
This is why bundling with --no-minify behaved correctly: terser didn't run and the assignments are multiple expressions.

✔️ PR Todo

  • Added/updated unit tests for this change
  • Included links to related issues/PRs

Copy link
Member

@devongovett devongovett left a comment

Choose a reason for hiding this comment

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

Thanks for rebasing!

@devongovett devongovett merged commit e653e66 into parcel-bundler:master Dec 17, 2018
@mischnic mischnic deleted the treeshake-functions branch December 17, 2018 06:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Experimental scope hoisting not removing dead code Scope hoisting: larger with minify
2 participants