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

postcss-lit

v1.1.1

Published

Lit support for PostCSS and related tooling

Downloads

39,623

Readme

postcss-lit

A PostCSS and stylelint custom syntax for parsing CSS inside lit templates.

For example:

class MyElement extends LitElement {
  static styles = css`
    .foo { color: hotpink; }
  `;
}

Install

npm i -D postcss-lit

Usage with PostCSS

In your postcss.config.js:

module.exports = {
  syntax: 'postcss-lit',
  plugins: [...]
};

PostCSS with webpack

If you use webpack to execute postcss, you must ensure the right order of loaders, like so:

module.exports = {
  entry: './src/my-element.ts',
  module: {
    rules: [
      {
        test: /\.ts$/,
        use: ['postcss-loader', 'ts-loader'],
        exclude: /node_modules/
      }
    ]
  },
  resolve: {
    extensions: ['.ts']
  },
  output: {
    filename: 'bundle.js'
  }
};

This is important as postcss will transform your CSS before typescript transpiles to JS (which is what you want to happen).

Usage with stylelint

In your .stylelintrc.json (or other stylelint config file):

{
  "customSyntax": "postcss-lit"
}

Or with the CLI:

stylelint --custom-syntax postcss-lit

Usage with vscode-stylelint

In order to make the vscode-stylelint extension work with this syntax correctly, you must configure it to validate JS and/or TypeScript files.

You can do this by following these instructions.

For example:

{
  "stylelint.validate": ["css", "javascript", "typescript"]
}

Usage with tailwind

In your postcss.config.js:

module.exports = {
  syntax: 'postcss-lit',
  plugins: {
    tailwindcss: {}
  }
};

In your tailwind.config.js:

const {tailwindTransform} = require('postcss-lit');

module.exports = {
  content: {
    files: ['./src/**/*.{js,ts}'],
    transform: {
      ts: tailwindTransform
    }
  }
};

You may then use tailwind's directives and classes in your elements:

class MyElement extends LitElement {
  static styles = css`
    @tailwind base;
    @tailwind utilities;
  `;

  render() {
    return html`
      <div class="text-xs">Small text</div>
    `;
  }
}

You must specify all tailwind directives you intend to use in your CSS, otherwise their replacement CSS will be incorrectly appended to the end of the document.

For example, in the code above, @tailwind base and @tailwind utilities were specified to make text-xs available. Without them, the code would not build.

Tailwind with webpack

See the same advice as with postcss standalone, here.

Disable specific templates

You can use postcss-lit-disable-next-line to disable a particular template from being processed:

// postcss-lit-disable-next-line
css`some template`;

These templates will be left as-is and won't make their way through postcss.

Note on expressions/interpolation

Often you may end up with expressions in your templates. For example:

css`
  .foo {
    color: ${expr};
  }
`;

These can be very difficult to support at build-time since we have no useful run-time information for what the expression might be.

Due to these difficulties, we officially support complete syntax being interpolated, though other cases may still work.

A few supported examples:

// Entire selector bound in
css`
  ${expr} {
    color: hotpink;
  }
`;

// Entire property bound in
css`
  .foo {
    ${expr}: hotpink;
  }
`;

// Entire value bound in
css`
  .foo {
    color: ${expr};
  }
`;

// Entire statement bound in
css`
  .foo {
    ${expr}
  }
`;

// Entire block bound in
css`
  .foo {}
  ${expr}
`;

And a few unsupported examples (though some may work, they are not officially supported):

// Part of a selector bound in
css`
  .foo, ${expr} {
    color: hotpink;
  }
`;

// Part of a value bound in
css`
  .foo {
    color: hot${expr};
  }
`;

// Part of a property bound in
css`
  .foo {
    col${expr}: hotpink;
  }
`;

In cases we fail to parse, we will raise a warning in the console and skip the template (i.e. leave it untouched and won't process it).

You can then use a // postcss-lit-disable-next-line comment to silence the warning.

Custom babel options

You may customise the babel options via your package.json:

{
  "postcss-lit": {
    "babelOptions": {
    }
  }
}

The available options are listed here.