Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-dogs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro-purgecss': minor
---

Add `includeDefaultContent` option.
26 changes: 20 additions & 6 deletions packages/astro-purgecss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import {
* Extended PurgeCSS options interface that allows partial configuration
* of the standard PurgeCSS options
*/
export interface PurgeCSSOptions extends Partial<UserDefinedOptions> {}
export interface PurgeCSSOptions extends Partial<UserDefinedOptions> {
/**
* @default true
*/
includeDefaultContent?: boolean;
}

const INTEGRATION_NAME = 'astro-purgecss' as const;

Expand Down Expand Up @@ -59,17 +64,26 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration {
return;
}

const { includeDefaultContent, ...purgeOptions } = options;
const normalizePath = (path: string) =>
path.replace('{outDir}', outDir).replace(/\\/g, '/');
const tryNormalizePath = <T>(input: T) =>
typeof input === 'string' ? normalizePath(input) : input;

// Run PurgeCSS on all CSS files
// Replace is needed to make sure to pass correct glob format on windows machines
const purgeResults = await new PurgeCSS().purge({
css: [`${outDir}/**/*.css`.replace(/\\/g, '/')],
defaultExtractor,
...options,
...purgeOptions,
css: [...(options.css || ['{outDir}/**/*.css'])].map(
tryNormalizePath
),
content: [
`${outDir}/**/*.html`.replace(/\\/g, '/'),
`${outDir}/**/*.js`.replace(/\\/g, '/'),
...((includeDefaultContent ?? true)
? [`{outDir}/**/*.html`, `{outDir}/**/*.js`]
: []),
...(options.content || [])
]
].map(tryNormalizePath)
});

// Filter out non-CSS files from purge results
Expand Down