@babel/plugin-transform-classes

classes - 图1info

This plugin is included in @babel/preset-env

Caveats

When extending a native class (e.g., class extends Array {}), the super class needs to be wrapped. This is needed to workaround two problems:

  • Babel transpiles classes using SuperClass.apply(/* ... */), but native classes aren’t callable and thus throw in this case.
  • Some built-in functions (like Array) always return a new object. Instead of returning it, Babel should treat it as the new this.

The wrapper works on IE11 and every other browser with Object.setPrototypeOf or __proto__ as fallback. There is NO IE ≤ 10 support. If you need IE ≤ 10 it’s recommended that you don’t extend natives.

Babel needs to statically know if you are extending a built-in class. For this reason, the “mixin pattern” doesn’t work:

JavaScript

  1. class Foo extends mixin(Array) {}
  2. function mixin(Super) {
  3. return class extends Super {
  4. mix() {}
  5. };
  6. }

To workaround this limitation, you can add another class in the inheritance chain so that Babel can wrap the native class:

JavaScript

  1. const ExtensibleArray = class extends Array {};
  2. class Foo extends mixin(ExtensibleArray) {}

Examples

In

JavaScript

  1. class Test {
  2. constructor(name) {
  3. this.name = name;
  4. }
  5. logger() {
  6. console.log("Hello", this.name);
  7. }
  8. }

Out

JavaScript

  1. function _classCallCheck(instance, Constructor) {
  2. if (!(instance instanceof Constructor)) {
  3. throw new TypeError("Cannot call a class as a function");
  4. }
  5. }
  6. var Test = (function() {
  7. function Test(name) {
  8. _classCallCheck(this, Test);
  9. this.name = name;
  10. }
  11. Test.prototype.logger = function logger() {
  12. console.log("Hello", this.name);
  13. };
  14. return Test;
  15. })();

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-transform-classes
  1. yarn add --dev @babel/plugin-transform-classes
  1. pnpm add --save-dev @babel/plugin-transform-classes

Usage

JavaScript

  1. // without options
  2. {
  3. "plugins": ["@babel/plugin-transform-classes"]
  4. }
  5. // with options
  6. {
  7. "plugins": [
  8. ["@babel/plugin-transform-classes", {
  9. "loose": true
  10. }]
  11. ]
  12. }

Via CLI

Shell

  1. babel --plugins @babel/plugin-transform-classes script.js

Via Node API

JavaScript

  1. require("@babel/core").transformSync("code", {
  2. plugins: ["@babel/plugin-transform-classes"],
  3. });

Options

loose

boolean, defaults to false.

classes - 图2caution

Consider migrating to the top level assumptions which offers granular control over various loose mode deductions Babel has applied.

babel.config.json

  1. {
  2. "assumptions": {
  3. "constantSuper": true,
  4. "noClassCalls": true,
  5. "setClassMethods": true,
  6. "superIsCallableConstructor": true
  7. }
  8. }

Method enumerability

Please note that in loose mode class methods are enumerable. This is not in line with the spec and you may run into issues.

Method assignment

Under loose mode, methods are defined on the class prototype with simple assignments instead of being defined. This can result in the following not working:

JavaScript

  1. class Foo {
  2. set bar() {
  3. throw new Error("foo!");
  4. }
  5. }
  6. class Bar extends Foo {
  7. bar() {
  8. // will throw an error when this method is defined
  9. }
  10. }

When Bar.prototype.foo is defined it triggers the setter on Foo. This is a case that is very unlikely to appear in production code however it’s something to keep in mind.

classes - 图3tip

You can read more about configuring plugin options here