Skip to content

Treeshake functions properly#2418

Merged
devongovett merged 4 commits into
parcel-bundler:masterfrom
mischnic:treeshake-functions
Dec 17, 2018
Merged

Treeshake functions properly#2418
devongovett merged 4 commits into
parcel-bundler:masterfrom
mischnic:treeshake-functions

Conversation

@mischnic

Copy link
Copy Markdown
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

@mischnic mischnic force-pushed the treeshake-functions branch from c2de4d4 to f23550b Compare December 15, 2018 13:02

@devongovett devongovett left a comment

Copy link
Copy Markdown
Member

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