diff --git a/.changeset/clever-spiders-pick.md b/.changeset/clever-spiders-pick.md new file mode 100644 index 00000000..5cd33dfe --- /dev/null +++ b/.changeset/clever-spiders-pick.md @@ -0,0 +1,5 @@ +--- +'astro-purgecss': patch +--- + +Fix broken path handling on Windows diff --git a/packages/astro-purgecss/src/index.ts b/packages/astro-purgecss/src/index.ts index 0214b9ea..2ba5d012 100644 --- a/packages/astro-purgecss/src/index.ts +++ b/packages/astro-purgecss/src/index.ts @@ -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'; @@ -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!'); @@ -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, @@ -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 { @@ -251,7 +251,7 @@ function Plugin(options: PurgeCSSOptions = {}): AstroIntegration { } await writeFileContent(htmlFile, content); - success(htmlFile.replace(outDir, '')); + success(relative(outDir, htmlFile)); }) ); } diff --git a/packages/astro-purgecss/src/utils.ts b/packages/astro-purgecss/src/utils.ts index 51f84edf..7517be42 100644 --- a/packages/astro-purgecss/src/utils.ts +++ b/packages/astro-purgecss/src/utils.ts @@ -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;