@fullhuman/postcss-purgecss
Version:
PostCSS plugin for PurgeCSS
240 lines (224 loc) • 7.14 kB
TypeScript
/**
* PostCSS Plugin for PurgeCSS
*
* Most bundlers and frameworks to build websites are using PostCSS.
* The easiest way to configure PurgeCSS is with its PostCSS plugin.
*
* @packageDocumentation
*/
import * as postcss from 'postcss';
/**
* @public
*/
export declare type ComplexSafelist = {
standard?: StringRegExpArray;
/**
* You can safelist selectors and their children based on a regular
* expression with `safelist.deep`
*
* @example
*
* ```ts
* const purgecss = await new PurgeCSS().purge({
* content: [],
* css: [],
* safelist: {
* deep: [/red$/]
* }
* })
* ```
*
* In this example, selectors such as `.bg-red .child-of-bg` will be left
* in the final CSS, even if `child-of-bg` is not found.
*
*/
deep?: StringRegExpArray;
/**
* You can safelist whole selectors if any part of that selector matches a
* pattern with `safelist.greedy`
*
* @example
*
* ```ts
* const purgecss = await new PurgeCSS().purge({
* content: [],
* css: [],
* safelist: {
* greedy: [/red$/]
* }
* })
* ```
*
* In this example, selectors such as `button.bg-red.nonexistent-class`
* will be left in the final CSS, even if `button` and `nonexistent-class`
* are not found.
*
*/
greedy?: StringRegExpArray;
variables?: StringRegExpArray;
keyframes?: StringRegExpArray;
};
/**
* Options of the dependency messages of the content
*
* @public
*/
export declare interface DependenciesOptions {
/**
* Root directory of the bundler, absolute or relative to the working
* directory: the content paths outside it get no dependency message.
*
* @defaultValue under Turbopack, the root Next.js infers from the workspace
* markers and lock files, otherwise no limit
*/
root?: string;
}
/**
* @public
*/
export declare type ExtractorFunction<T = string> = (content: T) => ExtractorResult;
/**
* @public
*/
export declare type ExtractorResult = ExtractorResultDetailed | string[];
/**
* @public
*/
export declare interface ExtractorResultDetailed {
attributes: {
names: string[];
values: string[];
};
classes: string[];
ids: string[];
tags: string[];
undetermined: string[];
}
/**
* @public
*/
export declare interface Extractors {
extensions: string[];
extractor: ExtractorFunction;
}
/**
* PostCSS Plugin for PurgeCSS
*
* @param opts - PurgeCSS Options
* @returns the postCSS plugin
*
* @public
*/
declare const purgeCSSPlugin: postcss.PluginCreator<UserDefinedOptions>;
export default purgeCSSPlugin;
/**
* Options used by PurgeCSS to remove unused CSS
*
* @public
*/
export declare interface PurgeCSSUserDefinedOptions {
/** {@inheritDoc purgecss#Options.content} */
content: Array<string | RawContent>;
/** {@inheritDoc purgecss#Options.css} */
css: Array<string | RawCSS>;
/** {@inheritDoc purgecss#Options.defaultExtractor} */
defaultExtractor?: ExtractorFunction;
/** {@inheritDoc purgecss#Options.extractors} */
extractors?: Array<Extractors>;
/** {@inheritDoc purgecss#Options.fontFace} */
fontFace?: boolean;
/** {@inheritDoc purgecss#Options.keyframes} */
keyframes?: boolean;
/** {@inheritDoc purgecss#Options.output} */
output?: string;
/** {@inheritDoc purgecss#Options.rejected} */
rejected?: boolean;
/** {@inheritDoc purgecss#Options.rejectedCss} */
rejectedCss?: boolean;
/** {@inheritDoc purgecss#Options.sourceMap } */
sourceMap?: boolean | (postcss.SourceMapOptions & { to?: string });
/** {@inheritDoc purgecss#Options.stdin} */
stdin?: boolean;
/** {@inheritDoc purgecss#Options.stdout} */
stdout?: boolean;
/** {@inheritDoc purgecss#Options.variables} */
variables?: boolean;
/** {@inheritDoc purgecss#Options.safelist} */
safelist?: UserDefinedSafelist;
/** {@inheritDoc purgecss#Options.blocklist} */
blocklist?: StringRegExpArray;
/** {@inheritDoc purgecss#Options.skippedContentGlobs} */
skippedContentGlobs?: Array<string>;
/** {@inheritDoc purgecss#Options.dynamicAttributes} */
dynamicAttributes?: StringRegExpArray;
}
/**
* @public
*/
export declare interface RawContent<T = string> {
extension: string;
raw: T;
}
/**
* @public
*/
export declare interface RawCSS {
/** css to purge */
raw: string;
/**
* name of the css, returned as the `file` of its result. It is used as the
* path of the css in source maps, like the path of a css file: the map
* `sources` are relative to the directory of `sourceMap.to`, which
* defaults to the name.
*/
name?: string;
}
/**
* Strings and regular expressions an option is matched against. A string
* matches a value exactly, except a string written between slashes, like
* `"/^btn-/"` or `"/^btn-/i"`, which is parsed as a regular expression. This
* form lets configuration files that cannot contain regular expressions, such
* as `postcss.config.json`, use patterns.
*
* @public
*/
export declare type StringRegExpArray = Array<RegExp | string>;
/**
* {@inheritDoc purgecss#UserDefinedOptions}
*
* @public
*/
export declare interface UserDefinedOptions extends Omit<PurgeCSSUserDefinedOptions, "content" | "css"> {
content?: PurgeCSSUserDefinedOptions["content"];
contentFunction?: (sourceFile: string) => Array<string | RawContent>;
/**
* Add PostCSS dependency messages for the content files and globs, so that
* the dev servers (Vite, webpack and Rspack with postcss-loader, Next.js,
* Parcel) run the plugin again when the content changes. Set it to `false`
* to add no message, e.g. when a bundler rejects a path of the content.
*
* Pass an object to set the root directory of the bundler: the paths
* outside it get no message. Under Turbopack (Next.js), which stops with an
* error for a path outside its root, the root defaults to the one Next.js
* infers (the outermost directory with a workspace marker or a lock file).
* Set it to the value of `outputFileTracingRoot` or `turbopack.root` when
* the Next.js config sets one of them.
*
* @defaultValue `true`
*/
dependencies?: boolean | DependenciesOptions;
/**
* Keep the selectors extracted from the content files between the CSS
* files and the runs of the plugin in the process: a content file is only
* read and extracted again when it changed. Set it to `false` to read and
* extract the content files for every CSS file.
*
* @defaultValue `true`
*/
contentCache?: boolean;
}
/**
* @public
*/
export declare type UserDefinedSafelist = StringRegExpArray | ComplexSafelist;
export { }