1.1.0 • Published 3 months ago

@detra-lab/esbuild-plugin-lit-css v1.1.0

Weekly downloads
-
License
Apache License 2....
Repository
github
Last release
3 months ago

esbuild-plugin-lit-css

NPM Version NPM Downloads CI Status

An esbuild plugin to transform a Lit component styles via Lightning CSS: an extremely fast CSS parser, bundler, transformer, and minifier.

Tested using esbuild v0.19 and v0.20.

About Lightning CSS and the plugin

Lightning CSS is a tool written in Rust that empowers developers to use modern CSS features and future syntax today. It offers the following syntax transformations:

Furthermore, Lightning CSS offers a range of built-in features, including:

The plugin will process the styles and return them as a JavaScript/TypeScript string, giving you a versatile way to deal with them.

Installation and usage

  1. Install the NPM package of the plugin, along with esbuild itself:

    pnpm add -D @detra-lab/esbuild-plugin-lit-css esbuild
  2. Add the plugin to your esbuild configuration:

    // ./esbuild.js
    
    import { litCssPlugin } from '@detra-lab/esbuild-plugin-lit-css'
    // You can also use the default export if you prefer:
    // import litCssPlugin from '@detra-lab/esbuild-plugin-lit-css'
    
    await esbuild.build({
      // Other esbuild options...
      plugins: [litCssPlugin()]
    })
  3. Import the CSS within the file that hosts the Lit component. If you encounter type issues with CSS file imports, check out the TypeScript section.

    // ./src/components/hello-world.ts
    
    import { html, LitElement } from 'lit'
    import { customElement, property } from 'lit/decorators.js'
    
    import css from './styles.css'
    
    @customElement('hello-world')
    export class SampleElement extends LitElement {
      static styles = css
    
      @property()
      name = 'World'
    
      render() {
        return html`<h1>Hello, ${this.name}!</h1>`
      }
    }
  4. Run the esbuild script with Node.js >=16. You should be able to compile the component's styles successfully.

    node ./esbuild.js

Plugin options

You can pass several options to the plugin, which by default reference those passed to esbuild.

OptionTypeDefault
browserlistQuerystring> 0.5%, last 2 versions, Firefox ESR, not dead.
debugbooleanThe esbuild logLevel property is set to debug or false if not specified.
includeDraftSpecsbooleanLightning CSS draft syntax transpilation is enabled by default. Set this property to true to include the draft specifications in the generated code.
minifybooleanThe esbuild minify property or false if not specified.
sourceMapbooleanThe esbuild sourceMap property or false if not specified.

TypeScript support

If you encounter type issues when importing a CSS file within TypeScript code, you can add this line to your tsconfig.json file:

{
  "compilerOptions": {
    // ...
    "types": ["@detra-lab/esbuild-plugin-lit-css/declarations"]
  }
}

Alternatively, you can use a *.d.ts file (e.g. esbuild-env.d.ts), in the root of your project and use this syntax:

/// <reference types="@detra-lab/esbuild-plugin-lit-css/declarations" />

Code of Conduct

Help us keep the project open and inclusive. Please read and follow our Code of Conduct.

Contributing

Read our contributing guide to learn about our development process, how to propose bug fixes and improvements, and how to build and test your changes.

License

Apache License 2.0

1.1.0

3 months ago

1.0.2

3 months ago

1.0.1

5 months ago

1.0.0

6 months ago

0.1.0

7 months ago