1
0
Форкнуть 0
Compile ES6 HTMLElement class extensions to ES5
Перейти к файлу
Joshua Peek f5067aaea2 Fix typo 2017-01-18 16:19:28 -08:00
lib Only include helper element once 2016-11-23 00:31:53 -08:00
test Test constructor did run 2016-11-23 01:03:27 -08:00
.gitignore Initial commit 2016-11-22 18:42:47 -08:00
.npmignore Ignore test dir 2016-11-23 00:59:53 -08:00
.travis.yml Add Travis config 2016-11-22 18:44:59 -08:00
LICENSE Initial commit 2016-11-22 18:42:47 -08:00
README.md Fix typo 2017-01-18 16:19:28 -08:00
package.json babel-plugin-transform-custom-element-classes 0.1.0 2016-11-23 01:05:37 -08:00

README.md

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

Chrome Firefox IE Opera Safari
Latest 10.1+ TP

With Custom Elements V1 polyfill

Chrome Firefox IE Opera Safari
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"
  ]
}