Remove unused css selectors
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
The flexible extractor system for analyzing different file types and extracting CSS selectors from content files.
Function signature for content extractors that analyze files and return selectors.
/**
* Function type for extracting selectors from content
* @param content - Content string to analyze
* @returns Extracted selectors as detailed object or string array
*/
type ExtractorFunction<T = string> = (content: T) => ExtractorResult;Union type representing the output of extractor functions.
type ExtractorResult = ExtractorResultDetailed | string[];Comprehensive result object with categorized selectors extracted from content.
interface ExtractorResultDetailed {
/** HTML/CSS attributes */
attributes: {
/** Attribute names (e.g., 'class', 'id', 'data-toggle') */
names: string[];
/** Attribute values */
values: string[];
};
/** CSS class names */
classes: string[];
/** CSS ID selectors */
ids: string[];
/** HTML tag names */
tags: string[];
/** Selectors that couldn't be categorized */
undetermined: string[];
}Configuration object linking file extensions to their corresponding extractor functions.
interface Extractors {
/** File extensions this extractor handles (e.g., ['.html', '.vue']) */
extensions: string[];
/** Extractor function for processing content */
extractor: ExtractorFunction;
}Usage Example:
import { PurgeCSS, ExtractorFunction } from "purgecss";
// Custom extractor for Vue files
const vueExtractor: ExtractorFunction = (content: string) => {
const classes = content.match(/class="([^"]+)"/g) || [];
return classes.map(cls => cls.replace(/class="([^"]+)"/, '$1').split(' ')).flat();
};
// Configure extractor
const extractors = [{
extensions: ['.vue'],
extractor: vueExtractor
}];
const results = await new PurgeCSS().purge({
content: ['src/**/*.vue'],
css: ['styles/*.css'],
extractors
});Management class for organizing and querying extracted selectors from content analysis.
/**
* ExtractorResultSets constructor
* @param er - Initial extractor result to populate the sets
*/
constructor(er: ExtractorResult);
/**
* Merge another extractor result or result set into this one
* @param that - ExtractorResult or ExtractorResultSets to merge
* @returns This instance for method chaining
*/
merge(that: ExtractorResult | ExtractorResultSets): this;Methods for checking the presence of specific selector types.
/**
* Check if a CSS class name exists in the extracted selectors
* @param name - Class name to check
* @returns True if class is found
*/
hasClass(name: string): boolean;
/**
* Check if a CSS ID selector exists in the extracted selectors
* @param id - ID selector to check
* @returns True if ID is found
*/
hasId(id: string): boolean;
/**
* Check if an HTML tag name exists in the extracted selectors
* @param tag - Tag name to check
* @returns True if tag is found
*/
hasTag(tag: string): boolean;
/**
* Check if an attribute name exists in the extracted selectors
* @param name - Attribute name to check
* @returns True if attribute name is found
*/
hasAttrName(name: string): boolean;
/**
* Check if an attribute value exists in the extracted selectors
* @param value - Attribute value to check
* @returns True if attribute value is found
*/
hasAttrValue(value: string): boolean;Methods for sophisticated attribute selector matching.
/**
* Check if any attribute values start with the given prefix
* @param prefix - Prefix to match against attribute values
* @returns True if matching prefix is found
*/
hasAttrPrefix(prefix: string): boolean;
/**
* Check if any attribute values end with the given suffix
* @param suffix - Suffix to match against attribute values
* @returns True if matching suffix is found
*/
hasAttrSuffix(suffix: string): boolean;
/**
* Check if any attribute values contain the given substring
* @param substr - Substring to match (supports space-separated words)
* @returns True if matching substring is found
*/
hasAttrSubstr(substr: string): boolean;Usage Examples:
import { PurgeCSS, ExtractorResultSets } from "purgecss";
const purgeCSS = new PurgeCSS();
// Extract selectors from files
const selectors = await purgeCSS.extractSelectorsFromFiles(
['src/**/*.html', 'src/**/*.js'],
[{ extensions: ['.html', '.js'], extractor: defaultExtractor }]
);
// Query the extracted selectors
if (selectors.hasClass('btn-primary')) {
console.log('btn-primary class found in content');
}
if (selectors.hasAttrPrefix('data-')) {
console.log('Data attributes found in content');
}
// Extract from raw content
const rawSelectors = await purgeCSS.extractSelectorsFromString([
{ raw: '<div class="hero-banner" id="main"></div>', extension: 'html' }
], extractors);
// Merge multiple result sets
const combinedSelectors = new ExtractorResultSets([])
.merge(selectors)
.merge(rawSelectors);Utility function for merging multiple extractor results into a single set.
/**
* Merge multiple extractor selectors into a single result set
* @param extractors - Variable number of ExtractorResultDetailed or ExtractorResultSets
* @returns Merged ExtractorResultSets containing all selectors
*/
function mergeExtractorSelectors(
...extractors: (ExtractorResultDetailed | ExtractorResultSets)[]
): ExtractorResultSets;Usage Example:
import { mergeExtractorSelectors } from "purgecss";
const htmlSelectors = await purgeCSS.extractSelectorsFromFiles(['*.html'], htmlExtractors);
const jsSelectors = await purgeCSS.extractSelectorsFromFiles(['*.js'], jsExtractors);
const combinedSelectors = mergeExtractorSelectors(htmlSelectors, jsSelectors);The default extractor used when no specific extractor is configured for a file type.
Default Implementation:
const defaultExtractor = (content: string): ExtractorResult =>
content.match(/[A-Za-z0-9_-]+/g) || [];The default extractor uses a simple regex pattern to match alphanumeric sequences, underscores, and hyphens - suitable for basic CSS class names and IDs.
Install with Tessl CLI
npx tessl i tessl/npm-purgecss