UNPKG

purgecss-webpack-plugin

Version:

PurgeCSS plugin for webpack and Rspack - Remove unused css

258 lines (238 loc) • 7 kB
import * as postcss from 'postcss'; /** * @public */ export declare type BlocklistFunction = () => StringRegExpArray; /** * Compilation of webpack or Rspack * * @public */ export declare type CompilationLike = object; /** * Compiler of webpack or Rspack, the part of it the plugin is applied to. * The types of the plugin do not depend on webpack, which Rspack projects * don't install. * * @public */ export declare interface CompilerLike { hooks: { compilation: { tap(name: string, callback: (compilation: CompilationLike) => void): void; }; }; } /** * @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; }; /** * @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; } /** * @public */ export declare type PathFunction = () => string[]; /** * @public */ export declare class PurgeCSSPlugin { options: UserDefinedOptions; purgedStats: PurgedStats; /** * Selectors extracted from the content files, kept from one compilation to * the next one in watch mode: only the files that changed are read again */ private contentCache; constructor(options: UserDefinedOptions); /** * Apply the plugin to a webpack or Rspack compiler * * @param compiler - compiler of webpack or Rspack */ apply(compiler: CompilerLike): void; /** * Purge the CSS assets of a compilation of webpack or Rspack * * @param compilationLike - compilation of webpack or Rspack */ initializePlugin(compilationLike: CompilationLike): void; /** * Purge the CSS assets of a compilation * * @param compilationLike - compilation of webpack or Rspack * @param entryPaths - content files */ runPluginHook(compilationLike: CompilationLike, entryPaths: string[]): Promise<void>; } /** * 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 type PurgedStats = { [index: string]: string[]; }; /** * @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; } /** * @public */ export declare type SafelistFunction = () => ComplexSafelist; /** * 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>; /** * @public */ export declare type UserDefinedOptions = Omit<PurgeCSSUserDefinedOptions, "css" | "content" | "safelist" | "blocklist" | "sourceMap"> & { paths: string[] | PathFunction; moduleExtensions?: string[]; verbose?: boolean; safelist?: PurgeCSSUserDefinedOptions["safelist"] | SafelistFunction; blocklist?: PurgeCSSUserDefinedOptions["blocklist"] | BlocklistFunction; only?: string[]; }; /** * @public */ export declare type UserDefinedSafelist = StringRegExpArray | ComplexSafelist; export { }