@babel/plugin-transform-private-methods

private-methods - 图1info

This plugin is included in @babel/preset-env, in ES2022

History

VersionChanges
v7.3.0Support private accessors (getters and setters)
v7.2.0Initial Release

Example

JavaScript

  1. class Counter extends HTMLElement {
  2. #xValue = 0;
  3. get #x() {
  4. return this.#xValue;
  5. }
  6. set #x(value) {
  7. this.#xValue = value;
  8. window.requestAnimationFrame(this.#render.bind(this));
  9. }
  10. #clicked() {
  11. this.#x++;
  12. }
  13. }

Installation

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

Usage

Without options:

babel.config.json

  1. {
  2. "plugins": ["@babel/plugin-transform-private-methods"]
  3. }

With options:

babel.config.json

  1. {
  2. "plugins": [["@babel/plugin-transform-private-methods", { "loose": true }]]
  3. }

Via CLI

Shell

  1. $ babel --plugins @babel/plugin-transform-private-methods script.js

Via Node API

JavaScript

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

Options

loose

boolean, defaults to false.

private-methods - 图2note

The loose mode configuration setting must be the same as @babel/plugin-transform-class-properties.

When true, private methods will be assigned directly on its parent via Object.defineProperty rather than a WeakSet. This results in improved performance and debugging (normal property access vs .get()) at the expense of potentially leaking “privates” via things like Object.getOwnPropertyNames.

private-methods - 图3caution

Consider migrating to the top level privateFieldsAsProperties assumption.

babel.config.json

  1. {
  2. "assumptions": {
  3. "privateFieldsAsProperties": true,
  4. "setPublicClassFields": true
  5. }
  6. }

Note that both privateFieldsAsProperties and setPublicClassFields must be set to true.

Let’s use the following as an example:

JavaScript

  1. class Foo {
  2. constructor() {
  3. this.publicField = this.#privateMethod();
  4. }
  5. #privateMethod() {
  6. return 42;
  7. }
  8. }

By default, this becomes:

JavaScript

  1. var Foo = function Foo() {
  2. "use strict";
  3. babelHelpers.classCallCheck(this, Foo);
  4. _privateMethod.add(this);
  5. this.publicField = babelHelpers
  6. .classPrivateMethodGet(this, _privateMethod, _privateMethod2)
  7. .call(this);
  8. };
  9. var _privateMethod = new WeakSet();
  10. var _privateMethod2 = function _privateMethod2() {
  11. return 42;
  12. };

With { privateFieldsAsProperties: true }, it becomes:

JavaScript

  1. var Foo = function Foo() {
  2. "use strict";
  3. babelHelpers.classCallCheck(this, Foo);
  4. Object.defineProperty(this, _privateMethod, {
  5. value: _privateMethod2,
  6. });
  7. this.publicField = babelHelpers
  8. .classPrivateFieldLooseBase(this, _privateMethod)
  9. [_privateMethod]();
  10. };
  11. var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod");
  12. var _privateMethod2 = function _privateMethod2() {
  13. return 42;
  14. };

private-methods - 图4tip

You can read more about configuring plugin options here

References