Skip to content
Merged
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/clever-spiders-pick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro-purgecss': patch
---

Fix broken path handling on Windows
12 changes: 6 additions & 6 deletions packages/astro-purgecss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AstroConfig, AstroIntegration } from 'astro';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { join, relative } from 'node:path';
import { randomUUID } from 'node:crypto';
import { PurgeCSS, type UserDefinedOptions, type RawContent } from 'purgecss';

Expand Down Expand Up @@ -140,7 +140,7 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration {
await Promise.all(
purgedCssFiles.map(async ({ css, file }) => {
await writeCssFile(file, css, file);
success(file.replace(outDir, ''));
success(relative(outDir, file));
})
);
logger.info('🎉 Purging completed successfully!');
Expand All @@ -156,7 +156,7 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration {
// ex: assets/styles/light.css
if (!isAssetFile) {
await writeCssFile(file, css, file);
const relativePath = file.replace(outDir, '');
const relativePath = relative(outDir, file);
success(relativePath);
return {
oldFilename: relativePath,
Expand All @@ -168,8 +168,8 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration {
const hashedFilename = generateFileHash(file, css);
await writeCssFile(hashedFilename, css, file);

const relativeOldPath = file.replace(outDir, '');
const relativeNewPath = hashedFilename.replace(outDir, '');
const relativeOldPath = relative(outDir, file);
const relativeNewPath = relative(outDir, hashedFilename);
success(relativeNewPath);

return {
Expand Down Expand Up @@ -251,7 +251,7 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration {
}

await writeFileContent(htmlFile, content);
success(htmlFile.replace(outDir, ''));
success(relative(outDir, htmlFile));
})
);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/astro-purgecss/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ export function generateFileHash(filePath: string, content: string) {
return `${filePath.slice(0, -13)}.${hash}.css`;
}

// Clean from extra slash on windows and trailing forward slash on non-windows
// Clean from trailing slash and extra leading slash on windows
export function cleanPath(file: URL): string {
if (!(file instanceof URL)) {
throw new TypeError('Expected a URL object');
}

// Remove trailing forward slash if present
let path = fileURLToPath(file).replace(/\/+$/, '');
// Remove trailing slash if present
let path = fileURLToPath(file).replace(/(?:\/+|\\+)$/, '');

// Remove leading forward slash on windows if present
return process.platform === 'win32' ? path.replace(/^\/+/, '') : path;
Expand Down
Loading