babel-plugin-transform-custom-element-classes

Compile ES6 HTMLElement class extensions to ES5

Showing:

Popularity

Downloads/wk

4.7K

GitHub Stars

81

Maintenance

Last Commit

5yrs ago

Contributors

0

Package

Dependencies

1

Size (min+gzip)

98.6KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

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

ChromeFirefoxIEOperaSafari
Latest ✅10.1+ TP ✅

With Custom Elements V1 polyfill

ChromeFirefoxIEOperaSafari
Latest ✅Latest ✅11 ✅Latest ✅9.1+ ✅

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"
  ]
}

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial