purgecss

  • Version 6.0.0
  • Published
  • 56.9 kB
  • 4 dependencies
  • MIT license

Install

npm i purgecss
yarn add purgecss
pnpm add purgecss

Overview

Core package of PurgeCSS

Contains the core methods to analyze the files, remove unused CSS.

Index

Variables

variable defaultOptions

const defaultOptions: Options;
  • Modifiers

    • @public

Functions

function mergeExtractorSelectors

mergeExtractorSelectors: (
...extractors: (ExtractorResultDetailed | ExtractorResultSets)[]
) => ExtractorResultSets;
  • Merge two extractor selectors

    Parameter extractorSelectorsA

    extractor selectors A

    Parameter extractorSelectorsB

    extractor selectors B

    Returns

    the merged extractor result sets

    Modifiers

    • @public

function setOptions

setOptions: (configFile?: string) => Promise<Options>;
  • Load the configuration file from the path

    Parameter configFile

    Path of the config file

    Returns

    The options from the configuration file

    Throws

    Error This exception is thrown if the configuration file was not imported

    Modifiers

    • @public

function standardizeSafelist

standardizeSafelist: (
userDefinedSafelist?: UserDefinedSafelist
) => Required<ComplexSafelist>;
  • Format the user defined safelist into a standardized safelist object

    Parameter userDefinedSafelist

    the user defined safelist

    Returns

    the formatted safelist object that can be used in the PurgeCSS options

    Modifiers

    • @public

Classes

class ExtractorResultSets

class ExtractorResultSets {}
  • Modifiers

    • @public

constructor

constructor(er: ExtractorResult);

    method hasAttrName

    hasAttrName: (name: string) => boolean;

      method hasAttrPrefix

      hasAttrPrefix: (prefix: string) => boolean;

        method hasAttrSubstr

        hasAttrSubstr: (substr: string) => boolean;

          method hasAttrSuffix

          hasAttrSuffix: (suffix: string) => boolean;

            method hasAttrValue

            hasAttrValue: (value: string) => boolean;

              method hasClass

              hasClass: (name: string) => boolean;

                method hasId

                hasId: (id: string) => boolean;

                  method hasTag

                  hasTag: (tag: string) => boolean;

                    method merge

                    merge: (that: ExtractorResult | ExtractorResultSets) => this;

                      class PurgeCSS

                      class PurgeCSS {}
                      • Class used to instantiate PurgeCSS and can then be used to purge CSS files.

                        Example 1

                        await new PurgeCSS().purge({
                        content: ['index.html'],
                        css: ['css/app.css']
                        })

                        Modifiers

                        • @public

                      property options

                      options: Options;

                        property removedNodes

                        removedNodes: postcss.Node[];

                          property selectorsRemoved

                          selectorsRemoved: Set<string>;

                            property variablesStructure

                            variablesStructure: VariablesStructure;

                              method extractSelectorsFromFiles

                              extractSelectorsFromFiles: (
                              files: string[],
                              extractors: Extractors[]
                              ) => Promise<ExtractorResultSets>;
                              • Extract the selectors present in the files using a PurgeCSS extractor

                                Parameter files

                                Array of files path or glob pattern

                                Parameter extractors

                                Array of extractors

                              method extractSelectorsFromString

                              extractSelectorsFromString: (
                              content: RawContent[],
                              extractors: Extractors[]
                              ) => Promise<ExtractorResultSets>;
                              • Extract the selectors present in the passed string using a PurgeCSS extractor

                                Parameter content

                                Array of content

                                Parameter extractors

                                Array of extractors

                              method getPurgedCSS

                              getPurgedCSS: (
                              cssOptions: Array<string | RawCSS>,
                              selectors: ExtractorResultSets
                              ) => Promise<ResultPurge[]>;
                              • Get the purged version of the css based on the files

                                Parameter cssOptions

                                css options, files or raw strings

                                Parameter selectors

                                set of extracted css selectors

                              method purge

                              purge: (
                              userOptions: UserDefinedOptions | string | undefined
                              ) => Promise<ResultPurge[]>;
                              • Remove unused CSS

                                Parameter userOptions

                                PurgeCSS options or path to the configuration file

                                Returns

                                an array of object containing the filename and the associated CSS

                                Example 1

                                Using a configuration file named purgecss.config.js

                                const purgeCSSResults = await new PurgeCSS().purge()

                                Example 2

                                Using a custom path to the configuration file

                                const purgeCSSResults = await new PurgeCSS().purge('./purgecss.config.js')

                                Example 3

                                Using the PurgeCSS options

                                const purgeCSSResults = await new PurgeCSS().purge({
                                content: ['index.html', '**\/*.js', '**\/*.html', '**\/*.vue'],
                                css: ['css/app.css']
                                })

                              method removeUnusedCSSVariables

                              removeUnusedCSSVariables: () => void;
                              • Remove unused CSS variables

                              method removeUnusedFontFaces

                              removeUnusedFontFaces: () => void;
                              • Remove unused font-faces

                              method removeUnusedKeyframes

                              removeUnusedKeyframes: () => void;
                              • Remove unused keyframes

                              method walkThroughCSS

                              walkThroughCSS: (root: postcss.Root, selectors: ExtractorResultSets) => void;
                              • Walk through the CSS AST and remove unused CSS

                                Parameter root

                                root node of the postcss AST

                                Parameter selectors

                                selectors used in content files

                              class VariableNode

                              class VariableNode {}
                              • Modifiers

                                • @public

                              constructor

                              constructor(declaration: postcss.Declaration);

                                property isUsed

                                isUsed: boolean;

                                  property nodes

                                  nodes: VariableNode[];

                                    property value

                                    value: postcss.Declaration;

                                      class VariablesStructure

                                      class VariablesStructure {}
                                      • Modifiers

                                        • @public

                                      property nodes

                                      nodes: Map<string, VariableNode[]>;

                                        property safelist

                                        safelist: StringRegExpArray;

                                          property usedVariables

                                          usedVariables: Set<string>;

                                            method addVariable

                                            addVariable: (declaration: postcss.Declaration) => void;

                                              method addVariableUsage

                                              addVariableUsage: (
                                              declaration: postcss.Declaration,
                                              matchedVariables: IterableIterator<RegExpMatchArray>
                                              ) => void;

                                                method addVariableUsageInProperties

                                                addVariableUsageInProperties: (
                                                matchedVariables: IterableIterator<RegExpMatchArray>
                                                ) => void;

                                                  method isVariablesSafelisted

                                                  isVariablesSafelisted: (variable: string) => boolean;

                                                    method removeUnused

                                                    removeUnused: () => void;

                                                      method setAsUsed

                                                      setAsUsed: (variableName: string) => void;

                                                        Interfaces

                                                        interface ExtractorResultDetailed

                                                        interface ExtractorResultDetailed {}
                                                        • Modifiers

                                                          • @public

                                                        property attributes

                                                        attributes: {
                                                        names: string[];
                                                        values: string[];
                                                        };

                                                          property classes

                                                          classes: string[];

                                                            property ids

                                                            ids: string[];

                                                              property tags

                                                              tags: string[];

                                                                property undetermined

                                                                undetermined: string[];

                                                                  interface Extractors

                                                                  interface Extractors {}
                                                                  • Modifiers

                                                                    • @public

                                                                  property extensions

                                                                  extensions: string[];

                                                                    property extractor

                                                                    extractor: ExtractorFunction;

                                                                      interface Options

                                                                      interface Options {}
                                                                      • Options used by PurgeCSS to remove unused CSS Those options are used internally

                                                                        See Also

                                                                        Modifiers

                                                                        • @public

                                                                      property blocklist

                                                                      blocklist: StringRegExpArray;
                                                                      • Blocklist will block the CSS selectors from appearing in the final output CSS. The selectors will be removed even when they are seen as used by PurgeCSS.

                                                                      property content

                                                                      content: Array<string | RawContent>;
                                                                      • You can specify content that should be analyzed by PurgeCSS with an array of filenames or globs. The files can be HTML, Pug, Blade, etc.

                                                                        Example 1

                                                                        await new PurgeCSS().purge({
                                                                        content: ['index.html', '*.js', '*.html', '*.vue'],
                                                                        css: ['css/app.css']
                                                                        })

                                                                        Example 2

                                                                        PurgeCSS also works with raw content. To do this, you need to pass an object with the raw property instead of a filename. To work properly with custom extractors you need to pass the extension property along with the raw content.

                                                                        await new PurgeCSS().purge({
                                                                        content: [
                                                                        {
                                                                        raw: '<html><body><div class="app"></div></body></html>',
                                                                        extension: 'html'
                                                                        },
                                                                        '*.js',
                                                                        '*.html',
                                                                        '*.vue'
                                                                        ],
                                                                        css: [
                                                                        {
                                                                        raw: 'body { margin: 0 }'
                                                                        },
                                                                        'css/app.css'
                                                                        ]
                                                                        })

                                                                      property css

                                                                      css: Array<string | RawCSS>;
                                                                      • Similar to content, you can specify css that should be processed by PurgeCSS with an array of filenames or globs

                                                                      property defaultExtractor

                                                                      defaultExtractor: ExtractorFunction;

                                                                        property dynamicAttributes

                                                                        dynamicAttributes: string[];
                                                                        • Option to add custom CSS attribute selectors like "aria-selected", "data-selected", ...etc.

                                                                        property extractors

                                                                        extractors: Array<Extractors>;

                                                                          property fontFace

                                                                          fontFace: boolean;
                                                                          • If there are any unused @font-face rules in your css, you can remove them by setting the fontFace option to true.

                                                                            Example 1

                                                                            await new PurgeCSS().purge({
                                                                            content: ['index.html', '*.js', '*.html', '*.vue'],
                                                                            css: ['css/app.css'],
                                                                            fontFace: true
                                                                            })

                                                                          property keyframes

                                                                          keyframes: boolean;

                                                                            property output

                                                                            output?: string;

                                                                              property rejected

                                                                              rejected: boolean;

                                                                                property rejectedCss

                                                                                rejectedCss: boolean;

                                                                                  property safelist

                                                                                  safelist: Required<ComplexSafelist>;
                                                                                  • You can indicate which selectors are safe to leave in the final CSS. This can be accomplished with the option safelist.

                                                                                  property skippedContentGlobs

                                                                                  skippedContentGlobs: Array<string>;
                                                                                  • If you provide globs for the content parameter, you can use this option to exclude certain files or folders that would otherwise be scanned. Pass an array of globs matching items that should be excluded. (Note: this option has no effect if content is not globs.)

                                                                                  property sourceMap

                                                                                  sourceMap:
                                                                                  | boolean
                                                                                  | (postcss.SourceMapOptions & {
                                                                                  to?: string;
                                                                                  });

                                                                                  property stdin

                                                                                  stdin: boolean;

                                                                                    property stdout

                                                                                    stdout: boolean;

                                                                                      property variables

                                                                                      variables: boolean;

                                                                                        interface RawContent

                                                                                        interface RawContent<T = string> {}
                                                                                        • Modifiers

                                                                                          • @public

                                                                                        property extension

                                                                                        extension: string;

                                                                                          property raw

                                                                                          raw: T;

                                                                                            interface RawCSS

                                                                                            interface RawCSS {}
                                                                                            • Modifiers

                                                                                              • @public

                                                                                            property name

                                                                                            name?: string;

                                                                                              property raw

                                                                                              raw: string;

                                                                                                interface ResultPurge

                                                                                                interface ResultPurge {}
                                                                                                • Modifiers

                                                                                                  • @public

                                                                                                property css

                                                                                                css: string;

                                                                                                  property file

                                                                                                  file?: string;

                                                                                                    property rejected

                                                                                                    rejected?: string[];

                                                                                                      property rejectedCss

                                                                                                      rejectedCss?: string;

                                                                                                        property sourceMap

                                                                                                        sourceMap?: string;
                                                                                                        • sourceMap property will be empty if UserDefinedOptions.sourceMap inline is not set to false, as the source map will be contained within the text of ResultPurge.css

                                                                                                        interface UserDefinedOptions

                                                                                                        interface UserDefinedOptions {}
                                                                                                        • Options used by PurgeCSS to remove unused CSS

                                                                                                          Modifiers

                                                                                                          • @public

                                                                                                        property blocklist

                                                                                                        blocklist?: StringRegExpArray;

                                                                                                        property content

                                                                                                        content: Array<string | RawContent>;

                                                                                                        property css

                                                                                                        css: Array<string | RawCSS>;

                                                                                                        property defaultExtractor

                                                                                                        defaultExtractor?: ExtractorFunction;

                                                                                                        property dynamicAttributes

                                                                                                        dynamicAttributes?: string[];

                                                                                                        property extractors

                                                                                                        extractors?: Array<Extractors>;

                                                                                                        property fontFace

                                                                                                        fontFace?: boolean;

                                                                                                        property keyframes

                                                                                                        keyframes?: boolean;

                                                                                                        property output

                                                                                                        output?: string;

                                                                                                        property rejected

                                                                                                        rejected?: boolean;

                                                                                                        property rejectedCss

                                                                                                        rejectedCss?: boolean;

                                                                                                        property safelist

                                                                                                        safelist?: UserDefinedSafelist;

                                                                                                        property skippedContentGlobs

                                                                                                        skippedContentGlobs?: Array<string>;

                                                                                                        property sourceMap

                                                                                                        sourceMap?:
                                                                                                        | boolean
                                                                                                        | (postcss.SourceMapOptions & {
                                                                                                        to?: string;
                                                                                                        });

                                                                                                        property stdin

                                                                                                        stdin?: boolean;

                                                                                                        property stdout

                                                                                                        stdout?: boolean;

                                                                                                        property variables

                                                                                                        variables?: boolean;

                                                                                                        Type Aliases

                                                                                                        type ComplexSafelist

                                                                                                        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?: RegExp[];
                                                                                                        greedy?: RegExp[];
                                                                                                        variables?: StringRegExpArray;
                                                                                                        keyframes?: StringRegExpArray;
                                                                                                        };
                                                                                                        • Modifiers

                                                                                                          • @public

                                                                                                        type ExtractorFunction

                                                                                                        type ExtractorFunction<T = string> = (content: T) => ExtractorResult;
                                                                                                        • Modifiers

                                                                                                          • @public

                                                                                                        type ExtractorResult

                                                                                                        type ExtractorResult = ExtractorResultDetailed | string[];
                                                                                                        • Modifiers

                                                                                                          • @public

                                                                                                        type PostCSSRoot

                                                                                                        type PostCSSRoot = postcss.Root;
                                                                                                        • Modifiers

                                                                                                          • @public

                                                                                                        type StringRegExpArray

                                                                                                        type StringRegExpArray = Array<RegExp | string>;
                                                                                                        • Modifiers

                                                                                                          • @public

                                                                                                        type UserDefinedSafelist

                                                                                                        type UserDefinedSafelist = StringRegExpArray | ComplexSafelist;
                                                                                                        • Modifiers

                                                                                                          • @public

                                                                                                        Package Files (1)

                                                                                                        Dependencies (4)

                                                                                                        Dev Dependencies (1)

                                                                                                        Peer Dependencies (0)

                                                                                                        No peer dependencies.

                                                                                                        Badge

                                                                                                        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                                                                                        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/purgecss.

                                                                                                        • Markdown
                                                                                                          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/purgecss)
                                                                                                        • HTML
                                                                                                          <a href="https://www.jsdocs.io/package/purgecss"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>