npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lit-labs/compiler

v1.1.0

Published

Compiler to prepare Lit templates at build time

Downloads

3,298

Readme

@lit-labs/compiler

A compiler for optimizing Lit templates.

Warning @lit-labs/compiler is part of the Lit Labs set of packages – it is published in order to get feedback on the design and may receive breaking changes. RFC: https://github.com/lit/rfcs/pull/21

Give feedback: https://github.com/lit/lit/discussions/4117

Overview

@lit-labs/compiler exports a TypeScript Transformer that can be run over your JavaScript or TypeScript files to optimize away the lit-html prepare render phase. For template heavy applications this can result in a quicker first render.

Usage

This transformer can be used anywhere TypeScript transformers are accepted, which is dependent on your build setup.

Below is an example using Rollup with the plugin @rollup/plugin-typescript:

// File: rollup.config.js
import typescript from '@rollup/plugin-typescript';
import {compileLitTemplates} from '@lit-labs/compiler';

export default {
  // ...
  plugins: [
    typescript({
      transformers: {
        before: [compileLitTemplates()],
      },
    }),
    // other rollup plugins
  ],
};

See an example of the transformer in use in this project's test for source-maps validity in this rollup config file.

FAQ

What are the tradeoffs for using the compiler?

  1. Running the compiler requires a build step that can accept a TypeScript transformer.

  2. The very first template render is faster (sometimes up to 45% faster for template heavy pages), but currently the output file is about 5% larger (gzipped).

How do I know optimizations have been applied?

Given your original source code containing the html tag function to declare templates:

const hi = (name) => html`<h1>Hello ${name}!</h1>`;

This code should have been emitted at the end of your build without the html tag function. E.g. the above authored example is transformed into something like:

const b = (s) => s;
const lit_template_1 = {h: b`<h1>Hello <?></h1>`, parts: [{type: 2, index: 1}]};
const hi = (name) => ({_$litType$: lit_template_1, values: [name]});

What templates are optimized by the compiler?

In order for a template to be optimized by the compiler, it must be:

  1. A well-formed template that wouldn't raise runtime diagnostics in development builds of lit-html. For example, templates with expressions in invalid locations will not be compiled.
  2. Use html imported directly from the module lit or lit-html. Re-exports of html from other modules are not supported. The following imports are supported:
    1. import {html} from 'lit'; Usage: html`...`
    2. import {html as litHtml} from 'lit'; Usage: litHtml`...`
    3. import * as litModule from 'lit' Usage: litModule.html`...`
  3. Cannot contain dynamic bindings within the raw text elements: textarea, title, style, and script. This is due to these elements containing raw text nodes as children & the limitation that raw text nodes cannot be placed as adjacent children in HTML markup.

Does the compiler work on JavaScript files?

Because JavaScript is a subset of TypeScript, the TypeScript transform has been implemented and tested such that it handles JavaScript.

You will need to run the compiler transformer over your JavaScript files.

Future work

  • Investigate if it's possible to reduce the file size increase on the compilers output.
  • Expand compilation so the complete optimization of a lit application can also tree-shake the relevant lit-html runtime that is no longer needed.
  • Explore more optimizations than just the prepare phase.
  • Provide different ways of consuming and using the compiler.