Skip to content

Suggestion: remove bare import statements #724

@magic-akari

Description

@magic-akari

The only one dependence in lit-element is lit-html. (Thanks for removing @polymer/polymer.)

There are three methods to use lit-element

  • use bundlers(such as webpack)
  • use unpkg query parameters(?module)
  • use import-maps

But it is painful to use lit-element as es modules without any extra specific effort.

  • I cannot import lit-element form ./node_modules as my demo project.
  • I cannot import lit-element from other CDN. They don't support to rewrite bare import statements as unpkg doing.
  • CDN rewriting will cause SRI verification to fail.
  • import-maps is perfect in the future, not now.

Maybe it is better to remove bare import statement from lit-element.

There are only two functions/classes used in lit-element which is imported from lit-html.
render function:
https://github.com/Polymer/lit-element/blob/46c8f404937672ed10587124ec3ddee82fd005fe/src/lit-element.ts#L76
TemplateResult:
https://github.com/Polymer/lit-element/blob/46c8f404937672ed10587124ec3ddee82fd005fe/src/lit-element.ts#L213

I did some try, here is my idea.

- static render = render;
+ static render: typeof render;

or

- static render = render;
+ static render: typeof render = () => {
+     throw new Error("not implemented");
+ };

When the users are going to use LitElement, they have to inject the render function themselves first.

import { render, html } from "https://some.cdn/lit-html.js";
import { LitElement } from "https://some.cdn/lit-element.js";

LitElement.render = render;

class MyElement extends LitElement {
  // some other details...
}
customElements.define("my-element", MyElement);

It's ok to create a wrapper file which imports lit-html and injects render function automatically. It will keep compatibility.

import { render } from "lit-html";
import { LitElement } from "./lit-element";

LitElement.render = render;

export { LitElement };
export { html, svg, TemplateResult, SVGTemplateResult } from "lit-html/lit-html.js";
// ...

As for TemplateResult, we may need a flag in lit-html.

- if (templateResult instanceof TemplateResult) {
+ if (templateResult.flagOrAnotherName === Symbol.for("TemplateResult")) {

It is my rough idea. There will, of course, be a lot more details when we get started for real.

Do you have any ideas?

Related issue: #140

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions