-
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(postcss-purgecss): remove compatibility with postcss 7
- Loading branch information
Showing
4 changed files
with
88 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,85 @@ | ||
import postcss from "postcss"; | ||
import { createPurgeCSSPlugin } from "./postCSSPurgeCSS"; | ||
import { Helpers, PluginCreator, Root } from "postcss"; | ||
|
||
export default createPurgeCSSPlugin(postcss); | ||
import PurgeCSS, { | ||
defaultOptions, | ||
mergeExtractorSelectors, | ||
standardizeSafelist, | ||
} from "purgecss"; | ||
|
||
import { RawContent, UserDefinedOptions } from "./types"; | ||
|
||
const PLUGIN_NAME = "postcss-purgecss"; | ||
|
||
async function purgeCSS( | ||
opts: UserDefinedOptions, | ||
root: Root, | ||
{ result }: Helpers | ||
): Promise<void> { | ||
const purgeCSS = new PurgeCSS(); | ||
const options = { | ||
...defaultOptions, | ||
...opts, | ||
safelist: standardizeSafelist(opts?.safelist), | ||
}; | ||
|
||
if (opts && typeof opts.contentFunction === "function") { | ||
options.content = opts.contentFunction( | ||
(root.source && root.source.input.file) || "" | ||
); | ||
} | ||
|
||
purgeCSS.options = options; | ||
|
||
const { content, extractors } = options; | ||
|
||
const fileFormatContents = content.filter( | ||
(o) => typeof o === "string" | ||
) as string[]; | ||
const rawFormatContents = content.filter( | ||
(o) => typeof o === "object" | ||
) as RawContent[]; | ||
|
||
const cssFileSelectors = await purgeCSS.extractSelectorsFromFiles( | ||
fileFormatContents, | ||
extractors | ||
); | ||
const cssRawSelectors = await purgeCSS.extractSelectorsFromString( | ||
rawFormatContents, | ||
extractors | ||
); | ||
|
||
const selectors = mergeExtractorSelectors(cssFileSelectors, cssRawSelectors); | ||
|
||
//purge unused selectors | ||
purgeCSS.walkThroughCSS(root, selectors); | ||
|
||
if (purgeCSS.options.fontFace) purgeCSS.removeUnusedFontFaces(); | ||
if (purgeCSS.options.keyframes) purgeCSS.removeUnusedKeyframes(); | ||
if (purgeCSS.options.variables) purgeCSS.removeUnusedCSSVariables(); | ||
|
||
if (purgeCSS.options.rejected && purgeCSS.selectorsRemoved.size > 0) { | ||
result.messages.push({ | ||
type: "purgecss", | ||
plugin: "postcss-purgecss", | ||
text: `purging ${purgeCSS.selectorsRemoved.size} selectors: | ||
${Array.from(purgeCSS.selectorsRemoved) | ||
.map((selector) => selector.trim()) | ||
.join("\n ")}`, | ||
}); | ||
purgeCSS.selectorsRemoved.clear(); | ||
} | ||
} | ||
|
||
const purgeCSSPlugin: PluginCreator<UserDefinedOptions> = function (opts) { | ||
if (typeof opts === "undefined") | ||
throw new Error("PurgeCSS plugin does not have the correct options"); | ||
return { | ||
postcssPlugin: PLUGIN_NAME, | ||
Once(root, helpers) { | ||
return purgeCSS(opts, root, helpers); | ||
}, | ||
}; | ||
}; | ||
purgeCSSPlugin.postcss = true; | ||
|
||
export default purgeCSSPlugin; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters