Remove unused css selectors
npx @tessl/cli install tessl/npm-purgecss@7.0.0PurgeCSS is a comprehensive CSS optimization library and command-line tool that analyzes content files (HTML, JavaScript, etc.) and CSS files to identify and remove unused CSS selectors, significantly reducing bundle sizes. It features a powerful selector matching engine using PostCSS, customizable extractors for different file types, safelist functionality to protect specific selectors from removal, and extensive integration options through plugins for popular build tools.
npm install purgecssimport {
PurgeCSS,
defaultOptions,
standardizeSafelist,
setOptions,
mergeExtractorSelectors,
ExtractorResultSets,
VariablesStructure,
VariableNode
} from "purgecss";For CommonJS:
const {
PurgeCSS,
defaultOptions,
standardizeSafelist,
setOptions,
mergeExtractorSelectors,
ExtractorResultSets,
VariablesStructure,
VariableNode
} = require("purgecss");import { PurgeCSS } from "purgecss";
// Basic CSS purging
const purgeCSSResults = await new PurgeCSS().purge({
content: ['index.html', '**/*.js', '**/*.html', '**/*.vue'],
css: ['css/app.css']
});
console.log(purgeCSSResults[0].css); // Purged CSS
// Advanced usage with safelist
const results = await new PurgeCSS().purge({
content: ['src/**/*.html', 'src/**/*.js'],
css: ['styles/*.css'],
safelist: ['btn', 'btn-primary', /^modal-/],
variables: true,
keyframes: true,
fontFace: true
});PurgeCSS is built around several key components:
Main PurgeCSS class providing comprehensive CSS optimization functionality with selector extraction, matching, and removal operations.
class PurgeCSS {
purge(userOptions?: UserDefinedOptions | string): Promise<ResultPurge[]>;
extractSelectorsFromFiles(files: string[], extractors: Extractors[]): Promise<ExtractorResultSets>;
extractSelectorsFromString(content: RawContent[], extractors: Extractors[]): Promise<ExtractorResultSets>;
}
interface UserDefinedOptions {
content: Array<string | RawContent>;
css: Array<string | RawCSS>;
defaultExtractor?: ExtractorFunction;
extractors?: Array<Extractors>;
fontFace?: boolean;
keyframes?: boolean;
variables?: boolean;
rejected?: boolean;
rejectedCss?: boolean;
sourceMap?: boolean | (postcss.SourceMapOptions & { to?: string });
safelist?: UserDefinedSafelist;
blocklist?: StringRegExpArray;
skippedContentGlobs?: Array<string>;
dynamicAttributes?: string[];
}
interface ResultPurge {
css: string;
sourceMap?: string;
rejectedCss?: string;
file?: string;
rejected?: string[];
}Flexible extractor system for analyzing different file types and extracting CSS selectors from content files.
type ExtractorFunction<T = string> = (content: T) => ExtractorResult;
type ExtractorResult = ExtractorResultDetailed | string[];
interface ExtractorResultDetailed {
attributes: {
names: string[];
values: string[];
};
classes: string[];
ids: string[];
tags: string[];
undetermined: string[];
}
interface Extractors {
extensions: string[];
extractor: ExtractorFunction;
}
class ExtractorResultSets {
constructor(er: ExtractorResult);
merge(that: ExtractorResult | ExtractorResultSets): this;
hasClass(name: string): boolean;
hasId(id: string): boolean;
hasTag(tag: string): boolean;
hasAttrName(name: string): boolean;
hasAttrValue(value: string): boolean;
}Advanced safelist system with multiple patterns for protecting selectors from removal, plus blocklist functionality.
type UserDefinedSafelist = StringRegExpArray | ComplexSafelist;
interface ComplexSafelist {
standard?: StringRegExpArray;
deep?: RegExp[];
greedy?: RegExp[];
variables?: StringRegExpArray;
keyframes?: StringRegExpArray;
}
type StringRegExpArray = Array<RegExp | string>;
function standardizeSafelist(userDefinedSafelist?: UserDefinedSafelist): Required<ComplexSafelist>;CSS custom properties (variables) dependency tracking and unused variable removal functionality.
class VariablesStructure {
nodes: Map<string, VariableNode[]>;
usedVariables: Set<string>;
safelist: StringRegExpArray;
addVariable(declaration: postcss.Declaration): void;
addVariableUsage(declaration: postcss.Declaration, matchedVariables: IterableIterator<RegExpMatchArray>): void;
removeUnused(): void;
isVariablesSafelisted(variable: string): boolean;
}
class VariableNode {
nodes: VariableNode[];
value: postcss.Declaration;
isUsed: boolean;
constructor(declaration: postcss.Declaration);
}Configuration management, default options, and utility functions for PurgeCSS operations.
const defaultOptions: Options;
function setOptions(configFile?: string): Promise<Options>;
function standardizeSafelist(userDefinedSafelist?: UserDefinedSafelist): Required<ComplexSafelist>;
function mergeExtractorSelectors(...extractors: (ExtractorResultDetailed | ExtractorResultSets)[]): ExtractorResultSets;interface RawContent<T = string> {
extension: string;
raw: T;
}
interface RawCSS {
raw: string;
name?: string;
}type PostCSSRoot = postcss.Root;