|
1 | | -import postcss from "postcss"; |
2 | | -import { createPurgeCSSPlugin } from "./postCSSPurgeCSS"; |
| 1 | +import { Helpers, PluginCreator, Root } from "postcss"; |
3 | 2 |
|
4 | | -export default createPurgeCSSPlugin(postcss); |
| 3 | +import PurgeCSS, { |
| 4 | + defaultOptions, |
| 5 | + mergeExtractorSelectors, |
| 6 | + standardizeSafelist, |
| 7 | +} from "purgecss"; |
| 8 | + |
| 9 | +import { RawContent, UserDefinedOptions } from "./types"; |
| 10 | + |
| 11 | +const PLUGIN_NAME = "postcss-purgecss"; |
| 12 | + |
| 13 | +async function purgeCSS( |
| 14 | + opts: UserDefinedOptions, |
| 15 | + root: Root, |
| 16 | + { result }: Helpers |
| 17 | +): Promise<void> { |
| 18 | + const purgeCSS = new PurgeCSS(); |
| 19 | + const options = { |
| 20 | + ...defaultOptions, |
| 21 | + ...opts, |
| 22 | + safelist: standardizeSafelist(opts?.safelist), |
| 23 | + }; |
| 24 | + |
| 25 | + if (opts && typeof opts.contentFunction === "function") { |
| 26 | + options.content = opts.contentFunction( |
| 27 | + (root.source && root.source.input.file) || "" |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + purgeCSS.options = options; |
| 32 | + |
| 33 | + const { content, extractors } = options; |
| 34 | + |
| 35 | + const fileFormatContents = content.filter( |
| 36 | + (o) => typeof o === "string" |
| 37 | + ) as string[]; |
| 38 | + const rawFormatContents = content.filter( |
| 39 | + (o) => typeof o === "object" |
| 40 | + ) as RawContent[]; |
| 41 | + |
| 42 | + const cssFileSelectors = await purgeCSS.extractSelectorsFromFiles( |
| 43 | + fileFormatContents, |
| 44 | + extractors |
| 45 | + ); |
| 46 | + const cssRawSelectors = await purgeCSS.extractSelectorsFromString( |
| 47 | + rawFormatContents, |
| 48 | + extractors |
| 49 | + ); |
| 50 | + |
| 51 | + const selectors = mergeExtractorSelectors(cssFileSelectors, cssRawSelectors); |
| 52 | + |
| 53 | + //purge unused selectors |
| 54 | + purgeCSS.walkThroughCSS(root, selectors); |
| 55 | + |
| 56 | + if (purgeCSS.options.fontFace) purgeCSS.removeUnusedFontFaces(); |
| 57 | + if (purgeCSS.options.keyframes) purgeCSS.removeUnusedKeyframes(); |
| 58 | + if (purgeCSS.options.variables) purgeCSS.removeUnusedCSSVariables(); |
| 59 | + |
| 60 | + if (purgeCSS.options.rejected && purgeCSS.selectorsRemoved.size > 0) { |
| 61 | + result.messages.push({ |
| 62 | + type: "purgecss", |
| 63 | + plugin: "postcss-purgecss", |
| 64 | + text: `purging ${purgeCSS.selectorsRemoved.size} selectors: |
| 65 | + ${Array.from(purgeCSS.selectorsRemoved) |
| 66 | + .map((selector) => selector.trim()) |
| 67 | + .join("\n ")}`, |
| 68 | + }); |
| 69 | + purgeCSS.selectorsRemoved.clear(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +const purgeCSSPlugin: PluginCreator<UserDefinedOptions> = function (opts) { |
| 74 | + if (typeof opts === "undefined") |
| 75 | + throw new Error("PurgeCSS plugin does not have the correct options"); |
| 76 | + return { |
| 77 | + postcssPlugin: PLUGIN_NAME, |
| 78 | + Once(root, helpers) { |
| 79 | + return purgeCSS(opts, root, helpers); |
| 80 | + }, |
| 81 | + }; |
| 82 | +}; |
| 83 | +purgeCSSPlugin.postcss = true; |
| 84 | + |
| 85 | +export default purgeCSSPlugin; |
0 commit comments