Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
203 additions
and
139 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
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,74 +1,4 @@ | ||
import postcss from "postcss"; | ||
import PurgeCSS, { | ||
defaultOptions, | ||
mergeExtractorSelectors, | ||
standardizeSafelist, | ||
} from "purgecss"; | ||
import { createPurgeCSSPlugin } from "./postCSSPurgeCSS"; | ||
|
||
import { RawContent, UserDefinedOptions } from "./types"; | ||
|
||
const purgeCSSPlugin = postcss.plugin<Omit<UserDefinedOptions, "css">>( | ||
"postcss-plugin-purgecss", | ||
function (opts) { | ||
return async function (root, result): 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(); | ||
} | ||
}; | ||
} | ||
); | ||
|
||
export default purgeCSSPlugin; | ||
export default createPurgeCSSPlugin(postcss); |
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { Helpers, PluginCreator, Root } from "postcss"; | ||
|
||
import PurgeCSS, { | ||
defaultOptions, | ||
mergeExtractorSelectors, | ||
standardizeSafelist, | ||
} from "purgecss"; | ||
|
||
import { | ||
PostCSSv7, | ||
PostCSSv8, | ||
PostCSSVersions, | ||
RawContent, | ||
UserDefinedOptions, | ||
} from "./types"; | ||
|
||
const PLUGIN_NAME = "postcss-purgecss"; | ||
|
||
function isPostCSSv8(postcss: PostCSSVersions): postcss is PostCSSv8 { | ||
return typeof (postcss as PostCSSv8).Root !== "undefined"; | ||
} | ||
|
||
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; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export function createPurgeCSSPlugin(postCSS: PostCSSVersions) { | ||
if (isPostCSSv8(postCSS)) { | ||
return purgeCSSPlugin; | ||
} | ||
|
||
return (postCSS as PostCSSv7).plugin( | ||
"postcss-purgecss", | ||
(opts?: UserDefinedOptions) => { | ||
if (typeof opts === "undefined") | ||
throw new Error("PurgeCSS plugin does not have the correct options"); | ||
return async function (root, helpers): Promise<void> { | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return purgeCSS(opts, root, helpers); | ||
}; | ||
} | ||
); | ||
} |
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
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
Oops, something went wrong.