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

vite-plugin-svelte-purgecss

v0.2.7

Published

Vite plugin for PurgeCSS for Svelte

Downloads

1,088

Readme

Notice!

This package has been moved to vite-plugin-tailwind-purge as the internals have been refactored to be more generalized. Worry not though, Svelte will still be properly supported!


vite-plugin-svelte-purgecss

npm version license

A simple vite plugin that thoroughly purges excess CSS from Svelte projects using PurgeCSS. This package should be used in combination with Tailwind and a Tailwind specific UI library such as Skeleton.

Motivation

PurgeCSS is an excellent package that removes unused CSS. Their provided plugins for extraction do a decent enough job for simple projects. However, plugins such as postcss-purgecss and rollup-plugin-purgecss take a rather naive approach to selector extraction. They only analyze the content that is passed to them through their content fields, which means that if you pass a UI library to it, none of the selectors that are unused in your project (such as components that aren't imported) will be properly purged. Leaving you with a larger than necessary CSS bundle.

Ideally, we'd like to keep the selectors that are only used in your project. We accomplish by analyzing the emitted JS chunks that are generated by Rollup, and extracting out any of their possible selectors. From there, we can pass along the selectors to PurgeCSS for final processing.

⚠ Caveats ⚠

This package is still very experimental. Breaking changes can occur at any time. We'll stabilize once we hit a 1.0.0 release.

Usage

Installation

npm i -D vite-plugin-svelte-purgecss

Add to Vite

// vite.config.ts
import { purgeCss } from 'vite-plugin-svelte-purgecss';

const config: UserConfig = {
	plugins: [sveltekit(), purgeCss()],
};

Safelisting

If selectors that shouldn't be purged are being removed, simply add them to the safelist.

// vite.config.ts
import { purgeCss } from 'vite-plugin-svelte-purgecss';

const config: UserConfig = {
	plugins: [
		sveltekit(),
		purgeCss({
			safelist: {
				// any selectors that begin with "hljs-" will not be purged
				greedy: [/^hljs-/],
			},
		}),
	],
};

Alternatively, if you'd like to safelist selectors directly in your stylesheets, you can do so by adding special comments:

/*! purgecss start ignore */

h1 {
	color: red;
}

h2 {
	color: blue;
}

/*! purgecss end ignore */

You can also safelist selectors directly in the declaration block as well:

h3 {
	/*! purgecss ignore current */
	color: pink;
}

For further configuration, you can learn more here.