Skip to content

[static class properties] pull them into compiled closure #5902

@Andarist

Description

@Andarist

INPUT

class MyClass {
  static myProperty = value;
  method() {}
}

ACTUAL OUTPUT

var MyClass = function () {
  function MyClass() {
    _classCallCheck(this, MyClass);
  }

  _createClass(MyClass, [{
    key: "method",
    value: function method() {}
  }]);

  return MyClass;
}();

MyClass.myProperty = value;

EXPECTED OUTPUT

var MyClass = function () {
  function MyClass() {
    _classCallCheck(this, MyClass);
  }

  _createClass(MyClass, [{
    key: "method",
    value: function method() {}
  }]);

+  MyClass.myProperty = value;

  return MyClass;
}();

- MyClass.myProperty = value;

PROBLEM

Tree-shaking flat bundles. By assigning property to an unused variable we prevent possibility to tree-shake the unused variable as its treated as side effect. More can be read here.

This won't immediately allow for tree-shaking classes as they are still usually transpiled to IIFEs which means for uglify a potential side effect risk (hard to statically determine if running the function will cause a side effect) and needs for it to work the function call needs to be decorated with /*#__PURE__*/ comment - the issue for this is already created. The issue described here (statics) is also mentioned in the other thread, but I'm creating this to separate those 2 as they can be treated as unrelated.

In the other thread the simple implementation of this was even proposed by @kzc here. It's a good start, however there is a problem with the approach - we need to support both usages:

  • babel-plugin-transform-class-properties + babel-plugin-transform-es2015-classes
  • babel-plugin-transform-class-properties alone

Where the proposed approach deals only with the first one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions