lit / lit-element

LEGACY REPO. This repository is for maintenance of the legacy LitElement library. The LitElement base class is now part of the Lit library, which is developed in the lit monorepo.
https://lit-element.polymer-project.org
BSD 3-Clause "New" or "Revised" License
4.49k stars 318 forks source link

Use webpack and babel-loader, @query is undefined #1121

Closed WUSO01 closed 2 years ago

WUSO01 commented 3 years ago

Description

I wirted lit-element with typescript, i use webpack and babel compile my project. When i use babel-loader, console.log(@query('xxx')) is undefined, but i find ts-loader won't occur this problem.

This is the minimalist demo:

index.ts:

import { customElement, LitElement, html, query } from 'lit-element'

@customElement('custom-container')
export class CustomContainer extends LitElement {
  @query('#btn') btnEle: HTMLButtonElement

  firstUpdated () {
    console.log('this.btn is:', this.btnEle)
    this.btnEle.addEventListener('click', () => {
      console.log('click')
    })
  }

  render () {
    return html`
      <div>
        <button id="btn">button</button>
      </div>
    `
  }
}

webapck.config.js:

module.exports = {
  // ...
  module: {
   rules: [
      {
        test: /\.ts?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
        options: {
          cacheDirectory: true,
          cacheCompression: false
        }
      }
    ]
  }
}

babel.config.json:

{
  "presets": [
    "@babel/env",
    "@babel/preset-typescript"
  ],
  "plugins": [
    [
      "@babel/plugin-proposal-decorators", {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-proposal-class-properties"
    ]
  ]
}

Demo source file

The issues:

image

sorvell commented 3 years ago

Can you see if the decorator settings here work? This is what we use for testing: https://github.com/Polymer/lit-element/blob/master/.babelrc#L3.

If you'd like additional help debugging the issue, it'd be great if you could provide a minimal runnable reproduction. A simple github repo would be fine.

Hope that helps.

sorvell commented 2 years ago

Closing due to age. Please open a new issue with a reproduction if the problem still exists. Thanks.