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

Let terser eliminate transpiled classes #3611

Merged
merged 2 commits into from
Oct 6, 2019
Merged

Conversation

mischnic
Copy link
Member

@mischnic mischnic commented Oct 6, 2019

↪️ Pull Request

A statement like this doesn't get tree shaken because it does look pure (without looking at the comment).

var $fa7e8a5a8249ace53223ae89975fffbd$export$default =
/*#__PURE__*/
function () {
  function XYZ() {
    $fa7e8a5a8249ace53223ae89975fffbd$var$_classCallCheck(this, XYZ);
  }

  $fa7e8a5a8249ace53223ae89975fffbd$var$_createClass(XYZ, [{
    key: "method",
    value: function method() {
      return "abc";
    }
  }]);
  return XYZ;
}();

By outputting comments when generating the code after scopehoisting (and before terser), we can leverage terser to remove everything.

This also makes it possible to allow certain comments to be preserved by terser (e.g. license headers): fixes #3322


We could a handle these comments in treeShake, but I'm not sure if there is an advantage to doing that?
+ Smaller cache size, less data to serialize (both to cache and AST->code)
- larger error surface, would need more tests

diff --git a/packages/shared/scope-hoisting/src/shake.js b/packages/shared/scope-hoisting/src/shake.js
index c3b3d09a..7a87f25d 100644
--- a/packages/shared/scope-hoisting/src/shake.js
+++ b/packages/shared/scope-hoisting/src/shake.js
@@ -67,7 +67,9 @@ function isPure(binding) {
       init.isPure() ||
       init.isIdentifier() ||
       init.isThisExpression() ||
-      binding.path.node.id.name === '$parcel$global'
+      binding.path.node.id.name === '$parcel$global' ||
+      (init.isCallExpression() &&
+        init.node.leadingComments.filter(v => v.value === '#__PURE__'))
     );
   }

(it might be more complicated than that: rollup/rollup#2429)

@mischnic mischnic changed the title Let terser minify Let terser minify transpiled classes Oct 6, 2019
@parcel-benchmark
Copy link

Benchmark Results

packages/benchmarks/kitchen-sink

Timings

Description Time Difference
Cold 10.28s -427.00ms
Cached 8.20s +283.00ms

Cold Bundles

Bundle Size Difference Time Difference
dist/legacy/parcel.ff215454.webp 102.94kb +0.00b 172.00ms -39.00ms 🚀
dist/modern/parcel.76ee4591.webp 102.94kb +0.00b 171.00ms -39.00ms 🚀
dist/modern/index.js 794.00b +0.00b 462.00ms -24.00ms 🚀
dist/legacy/index.js 252.00b +0.00b 457.00ms -32.00ms 🚀

Cached Bundles

Bundle Size Difference Time Difference
dist/legacy/parcel.ff215454.webp 102.94kb +0.00b 12.00ms -1.00ms 🚀
dist/modern/parcel.76ee4591.webp 102.94kb +0.00b 10.00ms -2.00ms 🚀
dist/legacy/index.js 252.00b +0.00b 23.00ms -3.00ms 🚀
dist/legacy/index.css 29.00b +0.00b 10.00ms -2.00ms 🚀
dist/modern/index.css 29.00b +0.00b 10.00ms -1.00ms 🚀

packages/benchmarks/react-hn

Timings

Description Time Difference
Cold 14.71s -90.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundle changes detected.

Cached Bundles

No bundles found, this is probably a failed build...

Click here to view a detailed benchmark overview.

@mischnic mischnic changed the title Let terser minify transpiled classes Let terser eliminate transpiled classes Oct 6, 2019
@devongovett devongovett merged commit 669d4ed into v2 Oct 6, 2019
@devongovett devongovett deleted the scopehoist-terser-classes branch October 6, 2019 16:39
@@ -12,7 +12,7 @@ export function generate(
) {
let {code} = babelGenerate(ast, {
minified: options.minify,
Copy link
Contributor

Choose a reason for hiding this comment

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

@mischnic what does this tell babel to do? Should it also just be left to terser?

Copy link
Member Author

Choose a reason for hiding this comment

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

AFAICT, it only compresses whitespaces:

var generate = require("@babel/generator").default
var parse = require("@babel/parser").parse

const AST = parse(`const ABCDEF = "hel" + "lo";`);
console.log(generate(AST, {minified: true}).code);
// const ABCDEF="hel"+"lo";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants