All the Lit packages are published with development and production builds, using Node's support for export conditions.
The production build is optimized with very aggressive minification settings. The development build is unminified for easier debugging and includes extra checks and warnings. The default build is the production build, so that projects don't accidentally deploy the larger development build.
You must opt into the development build by specifying the "development" export condition in tools that support export conditions, such as Rollup, Webpack, and Web Dev Server. This is done differently for each tool.
For example, in Rollup, using the @rollup/node-resolve plugin, you can select the development build with exportConditions option:
The development builds of ReactiveElement and LitElement support extra runtime warnings that can help identify issues that would be costly to check for in production builds.
Some warnings are always displayed. There are also two categories of optional warnings that can be turned on or off:
'migration'. Warnings related to migration from LitElement 2.x. Off by default.
'change-in-update'. Warnings related to changing reactive state during an update. On by default.
You can control the optional warnings using the ReactiveElement.disableWarning() and ReactiveElement.enableWarning() methods. You can call them on any subclass of ReactiveElement, including LitElement and your own classes. Calling the methods on a given class turns warnings on or off for and the warnings for that class and any subclasses. For instance, you can turn off a category of warnings on all ReactiveElement classes, on all LitElement classes, or on a specific LitElement subclass.
These methods are only available in development builds, so be sure to guard their access. We recommend using optional chaining.
Examples:
import {LitElement, ReactiveElement} from'lit';
// Turn off migration warnings on all ReactiveElements,
// including LitElements
ReactiveElement.disableWarning?.('migration');
// Turn off update warnings on all LitElements
LitElement.disableWarning?.('change-in-update');
// Turn off update warnings on one element
MyElement.disableWarning?.('change-in-update');
You can also control warnings within a single class by defining a static enabledWarnings property:
classMyElementextendsLitElement {
staticenabledWarnings= ['migration'];
}
It's best for code size if the code to control warnings is eliminated in your own production builds.
A dev mode only warning is triggered when multiple versions, or even multiple copies of the same version, of any of the Lit core packages – lit-html, lit-element, @lit/reactive-element – are detected.
If Lit is being used as an internal dependency of elements, elements can use different versions of Lit and are completely interoperable. We also take care to ensure that Lit 2 and Lit 3 are mostly compatible with each other. For example, you can pass a Lit 2 template into a Lit 3 render function and vice-versa.
So, why the warning? Lit is sometimes compared to frameworks which often break if components using different framework versions are mixed together. Thus, it's easier to accidentally install multiple duplicated versions of Lit without realizing.
Loading multiple compatible versions of Lit is non-optimal because extra duplicated bytes must be sent to the user.
If you’re publishing a library that uses Lit, follow our publishing best practices so consumers of your library are able to de-duplicate Lit in their projects.
Resolving multiple versions of Lit
It is possible to follow the steps below, and not be able to de-duplicate Lit, e.g., a library you depend on is bundling a specific version of Lit. In these cases the warning can be ignored.
If you’re seeing a Multiple versions of Lit loaded development mode warning, there are a couple things you can try:
Find out which Lit libraries have multiple versions loaded by checking the following variables in your browser console: window.litElementVersions, window.reactiveElementVersions, and window.litHtmlVersions.
Use npm ls (note, you can specify exact libraries to look for, e.g. npm ls @lit/reactive-element) to narrow down which dependencies are loading multiple different versions of Lit.
Try to use npm dedupe to de-duplicate Lit. Use npm ls to verify if the duplicated Lit package was successfully de-duped.
It is possible to nudge npm to hoist particular versions of the core Lit packages by installing them as direct dependencies of your project with npm i @lit/reactive-element@latest lit-element@latest lit-html@latest. Replace latest with the version you want to de-dupe to.
If there is still duplication, you may need to delete your package lock and node_modules. Then install the version of lit you want explicitly, followed by your dependencies.
Lit is packaged as JavaScript modules, and it uses bare module specifiers that are not yet natively supported in most browsers. Bare specifiers are commonly used, and you may want to use them in your own code as well. For example:
import {LitElement, html, css} from'lit';
To run this code in the browser, the bare specifier ('lit') needs to be transformed to a URL that the browser can load (such as '/node_modules/lit/lit.js').
There are many development servers that can deal with module specifiers. If you already have a dev server that does this and integrates with your build process, that should be sufficient.
For older browsers like IE11, Web Dev Server can transform JavaScript modules to use the backwards-compatible SystemJS module loader, and automatically serve the web components polyfills. You'll need to configure the @web/dev-server-legacy package to support older browsers.
Lit support developing components in TypeScript, including full type declarations for the Lit APIs, standard and experimental decorators, and community tools for template type-checking and linting.
Because Lit is just a library, and doesn't require a compiler or use non-standard language syntax, there are no specific TypeScript tools that are required. Lit works with the official TypeScript compiler, tsc, with TypeScript wrappers such as those for Rollup, Vite, or Webpack, and alternate compilers like esbuild.
The main requirements of a TypeScript project are:
Enabling a modern JavaScript language level, like with the "ES2021"lib.
We also recommend the eslint-plugin-lit for ESLint which provides linting for Lit's HTML templates, inlcluding common HTML linting checks plus Lit-specific rules.
Integrating linting into your IDE workflow can help catch errors as early as possible. See Lit-specific IDE plugins to configure your IDE for Lit.
Using a code formatter can help ensure code is consistent and readable. Integrating your formatter of choice with your IDE ensures your code is always clean and tidy.
There are a number of IDE plugins that may be useful when developing with Lit. In particular, we recommend using a syntax highlighter that works with Lit templates.
ESLint has integrations for a number of code editors. If you have eslint-plugin-lit for ESLint installed in your ESLint configuration, your IDE will show the Lit specific errors and warnings.