Skip to content

Commit

Permalink
Allow overriding purgecss options (#370)
Browse files Browse the repository at this point in the history
* feat: allow to override purgecss options

* docs(changeset): allow to customize any purgecss avaiable options
  • Loading branch information
gavar authored Jan 20, 2024
1 parent 3fbf5c8 commit 063add6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/green-houses-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro-purgecss": minor
---

Allow to override any of purgecss options
22 changes: 4 additions & 18 deletions packages/astro-purgecss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import type { AstroIntegration } from 'astro';
import {
PurgeCSS,
type RawContent,
type StringRegExpArray,
type UserDefinedSafelist
} from 'purgecss';
import { PurgeCSS, type UserDefinedOptions } from 'purgecss';
import { writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

export type PurgeCSSOptions = {
fontFace?: boolean;
keyframes?: boolean;
rejected?: boolean;
rejectedCss?: boolean;
variables?: boolean;
safelist?: UserDefinedSafelist;
blocklist?: StringRegExpArray;
content?: Array<string | RawContent>;
};
export interface PurgeCSSOptions extends Partial<UserDefinedOptions> {}

function handleWindowsPath(outputPath: string): string {
if (process.platform !== 'win32') return outputPath;
Expand All @@ -37,14 +23,14 @@ export default function (options: PurgeCSSOptions = {}): AstroIntegration {
'astro:build:done': async ({ dir }) => {
const outDir = handleWindowsPath(fileURLToPath(dir));
const purged = await new PurgeCSS().purge({
css: [`${outDir}/**/*.css`],
defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || [],
...options,
content: [
`${outDir}/**/*.html`,
`${outDir}/**/*.js`,
...(options.content || [])
],
css: [`${outDir}/**/*.css`],
defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) || []
});
await Promise.all(
purged
Expand Down

0 comments on commit 063add6

Please sign in to comment.