Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate TS from HBS templates #6558

Merged
merged 29 commits into from
May 4, 2023
Merged

feat: generate TS from HBS templates #6558

merged 29 commits into from
May 4, 2023

Conversation

pskelin
Copy link
Contributor

@pskelin pskelin commented Feb 21, 2023

HBS templates are now compiled to TypeScript

  • this is bound the the component instance and usage of members and events is type checked

  • generated template signature now looks like this:
    function block0 (this: Button, context: UI5Element, tags: string[], suffix: string | undefined)
    context is left for compatibility reasons, but the template uses this internally.

new render method

staitc getters are only used by the framework to retrieve the template. if writing a template by hand, it would be a class method and this usage would be correct. This is now also possible, a component can implement a render method, access this in it and return a template literal

class Button extends UI5Element {
    ...
    render() {
        return html`<button>${this.icon}</button>`
    }
}

Linking templates to components

There is no way to directly express which component is used for which template from the .hbs file itself. The current tooling relies on convention, so the template and the component must comply to the following rules:

  • they must have the same name
  • they must both be in the src folder (not in any subfolder)

example:

src/
   Input.ts
   Input.hbs

The tooling will generate a .ts file from the .hbs file, it will be created in src/generated/templates which is in .gitignore and it will have the correct import to src/Input.d.ts

Linking static area templates (popovers) to components

Components that have a static area template for popovers will have the following file structure

src/
   Input.ts
   Input.hbs
   InputPopover.hbs

There is no way to express the connection from InputPopover.hbs to Input.ts (the input component instance will be passed to the template as this). Instead of making up complex naming conventions, the tooling will look for a InputPopover.d.ts file in the same folder. It is up to the developer to create this file and export the Input type from it.

src/
   Input.ts
   Input.hbs
   InputPopover.d.ts
   InputPopover.hbs

and the content of InputPopover.d.ts

import type Input from "./Input";
export default Input;

fixes: #6394

@ilhan007
Copy link
Member

ilhan007 commented Feb 23, 2023

There is also an issue #6394 to link

@github-actions github-actions bot added the Stale label Mar 24, 2023
@github-actions github-actions bot removed the Stale label Mar 25, 2023
@pskelin pskelin merged commit 02611b2 into main May 4, 2023
@ilhan007 ilhan007 deleted the hbs2ts branch May 4, 2023 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Typescript: generate types for component templates
2 participants