Reasoning for the new decorator import syntax #2122
-
Forgive me if this is the wrong category, but this has been bugging me since moving to lit3. Obviously, its not a real issue, but I was curious as to the reasoning behind this change and what the motivations were. In the previous version, you imported decorators such as import {LitElement, html, css, query, property} from 'lit-element'; In the new version, the imports are now in two different lines, and you have to specify import {LitElement, html, css} from 'lit';
import {query, property} from `lit/decorators.js`; My questions are,
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
The only reason is so that non-decorator users don't pay the payload costs of the decorator implementations. This also applies to future transforms that compile the decorators out - once the decorators are removed we can remove the import. I don't think there are any impacts to TypeScript parsing, but I might not understand the question. Not loading the files improves native tree-shaking, ie, the browser doesn't load what you don't import. I'm not sure how well code-rewriting tree-shaking worked before, but this is strictly better since it doesn't rely on it. |
Beta Was this translation helpful? Give feedback.
-
The DX is negatively impacted by unbundling lit exports. Autocomplete is flaky with many different import paths; developers need to look up the docs where something is imported from, and the import list quickly gets long. I assume that most lit developers use a bundler. Hence, three-shaking is on by default. If that's the case, I propose to switch the default import case from unbundled to bundled. -import { html, nothing } from "lit";
-import { customElement, state } from "lit/decorators.js";
-import { Task } from "@lit/task";
-import { classMap } from "lit/directives/class-map.js";
-import { repeat } from "lit/directives/repeat.js";
+import { html, repeat, state, Task, classMap, customElement, nothing } from "lit" If JS devs care about minimizing their import size, they should use a bundler or import unbundled imports. -import { html, nothing } from "lit"
+import { html, nothing } from "lit/html.js" |
Beta Was this translation helpful? Give feedback.
The only reason is so that non-decorator users don't pay the payload costs of the decorator implementations. This also applies to future transforms that compile the decorators out - once the decorators are removed we can remove the import.
I don't think there are any impacts to TypeScript parsing, but I might not understand the question.
Not loading the files improves native tree-shaking, ie, the browser doesn't load what you don't import. I'm not sure how well code-rewriting tree-shaking worked before, but this is strictly better since it doesn't rely on it.