Skip to content

[Bug]: Stage 3 decorators: initializers are undefined in Custom Element constructors #15325

@trusktr

Description

@trusktr

💻

  • Would you like to work on a fix?

How are you using Babel?

babel-loader (webpack)

Input code

const el = document.createElement('foo-el')

document.body.append(el)

function element(name) {
  return (Class) => {
    customElements.define(name, Class) // Uncaught TypeError: undefined is not a function
  }
}

function attribute(_, ctx) {
  return (initial) => initial
}

@element('foo-el')
class Foo extends HTMLElement {
  @attribute foo = 123
}

Configuration file name

babel.config.json

Configuration

module.exports = {
	plugins: [
		['@babel/plugin-transform-typescript', {allowDeclareFields: true}],
		['@babel/plugin-proposal-decorators', {version: '2022-03'}],
		['@babel/plugin-proposal-class-static-block'],
	],

	presets: [
		['@babel/preset-typescript', {isTSX: true, allExtensions: true, onlyRemoveTypeImports: true}],
		['babel-preset-solid'],
	]
}

Current and expected behavior

Initializers in the output are undefined when constructor runs during the customElements.define() call (element upgrade) inside the @element decorator.

Environment

  System:
    OS: macOS 13.0.1
  Binaries:
    Node: 18.12.1 - ~/.n-node-versions/bin/node
    npm: 8.19.3 - ~/.npm-packages/bin/npm
  npmPackages:
    @babel/cli: 7.18.10 => 7.18.10 
    @babel/core: 7.19.1 => 7.19.1 
    @babel/plugin-proposal-class-static-block: 7.18.6 => 7.18.6 
    @babel/plugin-proposal-decorators: 7.19.1 => 7.19.1 
    @babel/preset-typescript: 7.18.6 => 7.18.6 
    @babel/runtime: 7.19.0 => 7.19.0 
    babel-loader: 8.2.5 => 8.2.5 
    babel-preset-solid: 1.5.4 => 1.5.4 
    webpack: 5.74.0 => 5.74.0 

Possible solution

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Spec: DecoratorsoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions