ionic-team / stencil

A toolchain for building scalable, enterprise-ready component systems on top of TypeScript and Web Component standards. Stencil components can be distributed natively to React, Angular, Vue, and traditional web developers from a single, framework-agnostic codebase.
https://stenciljs.com
Other
12.41k stars 777 forks source link

New custom-elements transform #2067

Open manucorporat opened 4 years ago

manucorporat commented 4 years ago

GOAL

Emit getter and setters

@Prop() car;

becomes:

get car() {
 return getValue(this, 'car');
}
set car(value) {
 setValue(this, 'car', value);
}

Optional, we could inline the call to the watchers inlined in the setter

Emit observedAttributes static getter

@Prop()str:string;
@Prop() nu: number;

becomes:

static get observedAttributes() {
 return ['str', 'nu'];

Inject super call in connectedCallback and disconnectedCallback

connectedCallback() {
  console.log('hola');
}

becomes:

connectedCallback() {
  super.connectedCallback();
 console.log('hola');
}

Reflected properties??

@Prop({reflect: true}) prop: string;

Emit attributeChangedCallback()

Metadata for listeners?

Metadata for members

Example

@Component({
tagName: 'my-cmp',
styleUrl: './style.css'
})
export class MyCmp {
  @Element() el: HTMLElement;
  @Prop() prop:  string;
  @Prop({reflect: true}) reflected: number;
  @Event() myEvent;

  @Listen('click')
  onClick() {
    console.log('click');
  }

  connectedCallback() {
    console.log('hello');
  }

  @Watch('prop')
  propDidChange(newValue, oldValue) {
     console.log(newValue, oldValue);
  }

  render() {
    return (
      <Host><p>{this.prop}</p>
   )
 }

becomes:

export class MyCmp extends HTMLElement {
  constructor() {
  super();
    __stencil_registerHost(this);
    this.myEvent = __stencil_createEvent('myEvent', 2);
  }
  get el() {
    return this;
  }
  get prop() {
    return __stencil_getValue(this, 'prop');
  }
  set prop(value) {
    const oldValue = __stencil_setValue(this, 'prop', value, 1 /*string*/);
    if (oldValue) {
      this.propDidChange(value, oldValue);
    }
  } 
  get reflected() {
    return __stencil_getValue(this, 'reflected');
  }
  set reflected(value) {
    __stencil_setValue(this, 'reflected', value, 2 /*number*/);
  } 

  onClick() {
    console.log('click');
  }

  connectedCallback() {
   __stencil_connectedCallback(this);
    registerEvents(this, [['click', 'onClick', 0]]);
    console.log('hello');
  }

  disconnectedCallback() {
   __stencil_disconnectedCallback(this);
  }

  propDidChange(newValue, oldValue) {
     console.log(newValue, oldValue);
  }

  render() {
    return (
      <Host reflected={this.reflected}><p>{this.prop}</p>
   )
 }
romulocintra commented 4 years ago

My concerns are :

eriklharper commented 4 years ago

Will this proposal eliminate the console warnings when trying to declare a reserved word attribute/property name like title?