Skip to content

Commit

Permalink
[astro-purgecss] Fix regression with windows paths (#894)
Browse files Browse the repository at this point in the history
* [astro-purgecss] Fix regression with windows paths

* fix code style

* docs(changeset): fix regression in handling windows paths
  • Loading branch information
mhdcodes authored Feb 9, 2025
1 parent 39bddfd commit 796821b
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 245 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-camels-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro-purgecss': minor
---

fix regression in handling windows paths
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@
"changeset:publish": "pnpm run build && changeset publish"
},
"devDependencies": {
"@arethetypeswrong/cli": "^0.17.2",
"@arethetypeswrong/cli": "^0.17.3",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.11",
"@types/node": "^22.10.5",
"astro": "^5.2.2",
"commander": "^13.0.0",
"@changesets/cli": "^2.27.12",
"@types/node": "^22.13.1",
"astro": "^5.2.5",
"commander": "^13.1.0",
"esbuild": "^0.25.0",
"esbuild-plugin-clean": "^1.0.1",
"kleur": "^4.1.5",
"ora": "^8.1.1",
"ora": "^8.2.0",
"pkg-pr-new": "^0.0.39",
"prettier": "^3.4.2",
"prettier": "^3.5.0",
"prettier-plugin-astro": "^0.14.1",
"tiny-glob": "^0.2.9",
"tsup": "^8.3.5",
"turbo": "^2.3.3",
"typescript": "^5.7.2",
"tsup": "^8.3.6",
"turbo": "^2.4.0",
"typescript": "^5.7.3",
"vite-tsconfig-paths": "^5.1.4",
"vitest": "^3.0.5"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro-purgecss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { AstroConfig, AstroIntegration } from 'astro';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { PurgeCSS, type UserDefinedOptions } from 'purgecss';

import {
cleanPath,
generateFileHash,
readFileContent,
success,
Expand Down Expand Up @@ -46,7 +46,7 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration {
logger.info(`📦 Running in '${buildMode}' mode`);

// Convert the URL to a filesystem path
const outDir = fileURLToPath(dir);
const outDir = cleanPath(dir);

// Used to skip file rehashing for SSR/Hybrid modes
const isSSR = buildMode !== 'static';
Expand Down
14 changes: 14 additions & 0 deletions packages/astro-purgecss/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { dim, green, red } from 'kleur/colors';
import { createHash } from 'node:crypto';
import { existsSync } from 'node:fs';
import { readFile, unlink, writeFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';

export async function readFileContent(filePath: string): Promise<string> {
try {
Expand Down Expand Up @@ -43,6 +44,19 @@ 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
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 leading forward slash on windows if present
return process.platform === 'win32' ? path.replace(/^\/+/, '') : path;
}

export const dt = new Intl.DateTimeFormat('en-us', {
hour: '2-digit',
minute: '2-digit',
Expand Down
Loading

0 comments on commit 796821b

Please sign in to comment.