1. babel-plugin-transform-custom-element-classes
Compile ES6 HTMLElement class extensions to ES5
babel-plugin-transform-custom-element-classes
Package: babel-plugin-transform-custom-element-classes
Created by: github
Last modified: Sat, 16 Sep 2023 00:10:09 GMT
Version: 0.1.0
License: MIT
Downloads: 12,299
Repository: https://github.com/github/babel-plugin-transform-custom-element-classes

Install

npm install babel-plugin-transform-custom-element-classes
yarn add babel-plugin-transform-custom-element-classes

babel-plugin-transform-custom-element-classes

Compile Custom Element classes that extend HTMLElement for ES5 environments.

Problem

Custom Elements were designed for native ES6 classes that can extend host objects in ways prototypical inheritance can not. Using the Babel transform-es2015-classes transform will not work as is. You'll likely see a runtime error like "Uncaught TypeError: Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function." when attempting to extend HTMLElement in a Babel transpiled class.

Solution

A workaround is to use Reflect.construct to build a new HTMLElement instance expected by the constructor. This transform wraps any ES6 classes extending HTMLElement with an intermediary class that shims this behavior.

Compatibility

Installation

 $ npm install babel-plugin-transform-custom-element-classes

Usage

Via .babelrc

.babelrc

 // include before transform-es2015-classes
{
  "plugins": [
    "transform-custom-element-classes",
    "transform-es2015-classes"
  ]
}

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code