1 | import * as postcss from "postcss";
|
2 | interface RawContent<T = string> {
|
3 | extension: string;
|
4 | raw: T;
|
5 | }
|
6 | interface RawCSS {
|
7 | raw: string;
|
8 | }
|
9 | interface ExtractorResultDetailed {
|
10 | attributes: {
|
11 | names: string[];
|
12 | values: string[];
|
13 | };
|
14 | classes: string[];
|
15 | ids: string[];
|
16 | tags: string[];
|
17 | undetermined: string[];
|
18 | }
|
19 | type ExtractorResult = ExtractorResultDetailed | string[];
|
20 | type ExtractorFunction<T = string> = (content: T) => ExtractorResult;
|
21 | interface Extractors {
|
22 | extensions: string[];
|
23 | extractor: ExtractorFunction;
|
24 | }
|
25 | type StringRegExpArray = Array<RegExp | string>;
|
26 | type ComplexSafelist = {
|
27 | standard?: StringRegExpArray;
|
28 | deep?: RegExp[];
|
29 | greedy?: RegExp[];
|
30 | variables?: StringRegExpArray;
|
31 | keyframes?: StringRegExpArray;
|
32 | };
|
33 | type UserDefinedSafelist = StringRegExpArray | ComplexSafelist;
|
34 | interface UserDefinedOptions {
|
35 | content: Array<string | RawContent>;
|
36 | css: Array<string | RawCSS>;
|
37 | defaultExtractor?: ExtractorFunction;
|
38 | extractors?: Array<Extractors>;
|
39 | fontFace?: boolean;
|
40 | keyframes?: boolean;
|
41 | output?: string;
|
42 | rejected?: boolean;
|
43 | stdin?: boolean;
|
44 | stdout?: boolean;
|
45 | variables?: boolean;
|
46 | safelist?: UserDefinedSafelist;
|
47 | blocklist?: StringRegExpArray;
|
48 | skippedContentGlobs?: Array<string>;
|
49 | dynamicAttributes?: string[];
|
50 | }
|
51 | interface Options {
|
52 | content: Array<string | RawContent>;
|
53 | css: Array<string | RawCSS>;
|
54 | defaultExtractor: ExtractorFunction;
|
55 | extractors: Array<Extractors>;
|
56 | fontFace: boolean;
|
57 | keyframes: boolean;
|
58 | output?: string;
|
59 | rejected: boolean;
|
60 | stdin: boolean;
|
61 | stdout: boolean;
|
62 | variables: boolean;
|
63 | safelist: Required<ComplexSafelist>;
|
64 | blocklist: StringRegExpArray;
|
65 | skippedContentGlobs: Array<string>;
|
66 | dynamicAttributes: string[];
|
67 | }
|
68 | interface ResultPurge {
|
69 | css: string;
|
70 | file?: string;
|
71 | rejected?: string[];
|
72 | }
|
73 | declare class ExtractorResultSets {
|
74 | private undetermined;
|
75 | private attrNames;
|
76 | private attrValues;
|
77 | private classes;
|
78 | private ids;
|
79 | private tags;
|
80 | constructor(er: ExtractorResult);
|
81 | merge(that: ExtractorResult | ExtractorResultSets): this;
|
82 | hasAttrName(name: string): boolean;
|
83 | private someAttrValue;
|
84 | hasAttrPrefix(prefix: string): boolean;
|
85 | hasAttrSuffix(suffix: string): boolean;
|
86 | hasAttrSubstr(substr: string): boolean;
|
87 | hasAttrValue(value: string): boolean;
|
88 | hasClass(name: string): boolean;
|
89 | hasId(id: string): boolean;
|
90 | hasTag(tag: string): boolean;
|
91 | }
|
92 | declare const defaultOptions: Options;
|
93 | declare function standardizeSafelist(userDefinedSafelist?: UserDefinedSafelist): Required<ComplexSafelist>;
|
94 | /**
|
95 | * Load the configuration file from the path
|
96 | * @param configFile Path of the config file
|
97 | */
|
98 | declare function setOptions(configFile?: string): Promise<Options>;
|
99 | /**
|
100 | * Merge two extractor selectors
|
101 | * @param extractorSelectorsA extractor selectors A
|
102 | * @param extractorSelectorsB extractor selectors B
|
103 | */
|
104 | declare function mergeExtractorSelectors(...extractors: (ExtractorResultDetailed | ExtractorResultSets)[]): ExtractorResultSets;
|
105 | declare class PurgeCSS {
|
106 | private ignore;
|
107 | private atRules;
|
108 | private usedAnimations;
|
109 | private usedFontFaces;
|
110 | selectorsRemoved: Set<string>;
|
111 | private variablesStructure;
|
112 | options: Options;
|
113 | private collectDeclarationsData;
|
114 | /**
|
115 | * Get the extractor corresponding to the extension file
|
116 | * @param filename Name of the file
|
117 | * @param extractors Array of extractors definition
|
118 | */
|
119 | /**
|
120 | * Get the extractor corresponding to the extension file
|
121 | * @param filename Name of the file
|
122 | * @param extractors Array of extractors definition
|
123 | */
|
124 | private getFileExtractor;
|
125 | /**
|
126 | * Extract the selectors present in the files using a purgecss extractor
|
127 | * @param files Array of files path or glob pattern
|
128 | * @param extractors Array of extractors
|
129 | */
|
130 | /**
|
131 | * Extract the selectors present in the files using a purgecss extractor
|
132 | * @param files Array of files path or glob pattern
|
133 | * @param extractors Array of extractors
|
134 | */
|
135 | extractSelectorsFromFiles(files: string[], extractors: Extractors[]): Promise<ExtractorResultSets>;
|
136 | /**
|
137 | * Extract the selectors present in the passed string using a PurgeCSS extractor
|
138 | * @param content Array of content
|
139 | * @param extractors Array of extractors
|
140 | */
|
141 | /**
|
142 | * Extract the selectors present in the passed string using a PurgeCSS extractor
|
143 | * @param content Array of content
|
144 | * @param extractors Array of extractors
|
145 | */
|
146 | extractSelectorsFromString(content: RawContent[], extractors: Extractors[]): Promise<ExtractorResultSets>;
|
147 | /**
|
148 | * Evaluate at-rule and register it for future reference
|
149 | * @param node node of postcss AST
|
150 | */
|
151 | /**
|
152 | * Evaluate at-rule and register it for future reference
|
153 | * @param node node of postcss AST
|
154 | */
|
155 | private evaluateAtRule;
|
156 | /**
|
157 | * Evaluate css selector and decide if it should be removed or not
|
158 | * @param node node of postcss AST
|
159 | * @param selectors selectors used in content files
|
160 | */
|
161 | /**
|
162 | * Evaluate css selector and decide if it should be removed or not
|
163 | * @param node node of postcss AST
|
164 | * @param selectors selectors used in content files
|
165 | */
|
166 | private evaluateRule;
|
167 | /**
|
168 | * Get the purged version of the css based on the files
|
169 | * @param cssOptions css options, files or raw strings
|
170 | * @param selectors set of extracted css selectors
|
171 | */
|
172 | /**
|
173 | * Get the purged version of the css based on the files
|
174 | * @param cssOptions css options, files or raw strings
|
175 | * @param selectors set of extracted css selectors
|
176 | */
|
177 | getPurgedCSS(cssOptions: Array<string | RawCSS>, selectors: ExtractorResultSets): Promise<ResultPurge[]>;
|
178 | /**
|
179 | * Check if the keyframe is safelisted with the option safelist keyframes
|
180 | * @param keyframesName name of the keyframe animation
|
181 | */
|
182 | /**
|
183 | * Check if the keyframe is safelisted with the option safelist keyframes
|
184 | * @param keyframesName name of the keyframe animation
|
185 | */
|
186 | private isKeyframesSafelisted;
|
187 | /**
|
188 | * Check if the selector is blocklisted with the option blocklist
|
189 | * @param selector css selector
|
190 | */
|
191 | /**
|
192 | * Check if the selector is blocklisted with the option blocklist
|
193 | * @param selector css selector
|
194 | */
|
195 | private isSelectorBlocklisted;
|
196 | /**
|
197 | * Check if the selector is safelisted with the option safelist standard
|
198 | * @param selector css selector
|
199 | */
|
200 | /**
|
201 | * Check if the selector is safelisted with the option safelist standard
|
202 | * @param selector css selector
|
203 | */
|
204 | private isSelectorSafelisted;
|
205 | /**
|
206 | * Check if the selector is safelisted with the option safelist deep
|
207 | * @param selector selector
|
208 | */
|
209 | /**
|
210 | * Check if the selector is safelisted with the option safelist deep
|
211 | * @param selector selector
|
212 | */
|
213 | private isSelectorSafelistedDeep;
|
214 | /**
|
215 | * Check if the selector is safelisted with the option safelist greedy
|
216 | * @param selector selector
|
217 | */
|
218 | /**
|
219 | * Check if the selector is safelisted with the option safelist greedy
|
220 | * @param selector selector
|
221 | */
|
222 | private isSelectorSafelistedGreedy;
|
223 | /**
|
224 | * Remove unused css
|
225 | * @param userOptions PurgeCSS options
|
226 | */
|
227 | /**
|
228 | * Remove unused css
|
229 | * @param userOptions PurgeCSS options
|
230 | */
|
231 | purge(userOptions: UserDefinedOptions | string | undefined): Promise<ResultPurge[]>;
|
232 | /**
|
233 | * Remove unused CSS variables
|
234 | */
|
235 | /**
|
236 | * Remove unused CSS variables
|
237 | */
|
238 | removeUnusedCSSVariables(): void;
|
239 | /**
|
240 | * Remove unused font-faces
|
241 | */
|
242 | /**
|
243 | * Remove unused font-faces
|
244 | */
|
245 | removeUnusedFontFaces(): void;
|
246 | /**
|
247 | * Remove unused keyframes
|
248 | */
|
249 | /**
|
250 | * Remove unused keyframes
|
251 | */
|
252 | removeUnusedKeyframes(): void;
|
253 | /**
|
254 | * Transform a selector node into a string
|
255 | */
|
256 | /**
|
257 | * Transform a selector node into a string
|
258 | */
|
259 | private getSelectorValue;
|
260 | /**
|
261 | * Determine if the selector should be kept, based on the selectors found in the files
|
262 | * @param selector set of css selectors found in the content files or string
|
263 | * @param selectorsFromExtractor selectors in the css rule
|
264 | */
|
265 | /**
|
266 | * Determine if the selector should be kept, based on the selectors found in the files
|
267 | * @param selector set of css selectors found in the content files or string
|
268 | * @param selectorsFromExtractor selectors in the css rule
|
269 | */
|
270 | private shouldKeepSelector;
|
271 | /**
|
272 | * Walk through the CSS AST and remove unused CSS
|
273 | * @param root root node of the postcss AST
|
274 | * @param selectors selectors used in content files
|
275 | */
|
276 | /**
|
277 | * Walk through the CSS AST and remove unused CSS
|
278 | * @param root root node of the postcss AST
|
279 | * @param selectors selectors used in content files
|
280 | */
|
281 | walkThroughCSS(root: postcss.Root, selectors: ExtractorResultSets): void;
|
282 | }
|
283 | export { PurgeCSS as default, PurgeCSS, defaultOptions, standardizeSafelist, setOptions, mergeExtractorSelectors };
|