Building for production
This page focuses on recommendations for building an application that uses Lit components for production. For recommendations on build steps to perform on source code prior to publishing a reusable Lit component to npm, see Publishing.
See Requirements for a full list of requirements for building Lit code, which apply to both development and production.
In addition to those minimum requirements, this page describes optimizations you should consider when preparing code for production, as well as a concrete Rollup configuration that implements them.
Preparing code for productionPermalink to “Preparing code for production”
Lit projects benefit from the same build-time optimizations as other web projects. The following optimizations are recommended when serving Lit applications in production:
- Serving modern code to modern browsers as it is generally smaller and faster, and falling back to compiled code on older browsers.
- Enabling serve-time compression (such as gzip or brotli) for fewer bytes over the wire.
- Rollup: rollup-plugin-minify-html-literals
- Webpack: minify-html-literals-loader
Building with RollupPermalink to “Building with Rollup”
There are many tools you can use to perform the required and optional build steps necessary to serve Lit code, and Lit does not require any one specific tool. However, we recommend Rollup because it's designed to work with the standard ES module format and output optimal code that leverages native modules on the client.
There are many ways to set up Rollup to bundle your project. The Modern Web project maintains an excellent Rollup plugin
@web/rollup-plugin-html that helps tie a number of best-practices for building applications together into an easy-to-use package. Example configurations using this plugin are described below.
Modern-only buildPermalink to “Modern-only build”
rollup.config.js file below will build an application that meets the modern browser build requirements and production optimizations described on this page. This configuration is suitable for serving to modern browsers that can run ES2019 JS without polyfills.
Required node modules:
Running the rollup build:
Modern + legacy buildPermalink to “Modern + legacy build”
The following configuration generates a hybrid build with two sets of JS bundles, one for modern browsers, and one for legacy browsers. The modern bundles are optimistically pre-fetched, and client-side feature-detection is used to determine whether to load the smaller/faster modern builds or the legacy build (and any required polyfills), per the legacy browser build requirements.
Required node modules:
Building with standalone lit-htmlPermalink to “Building with standalone lit-html”
If you're using lit-html as a standalone templating library, you can follow almost all of the guidance for building with Lit. The only difference is that lit-html doesn't require the full Web Components polyfills. You'll only need the template polyfill.
Using the template polyfillPermalink to “Using the template polyfill”
To run lit-html on Internet Explorer 11, which doesn't support the
<template> element, you'll need a polyfill. You can use the template polyfill included with the Web Components polyfills.
Install the template polyfill:
Use the template polyfill:
Note: when compiling for IE11, the Babel polyfills need to be bundled separately from the application code, and loaded before the template polyfill.