Skip to content

Commit

Permalink
Merge pull request #3374 from dallyh/unplugin-win
Browse files Browse the repository at this point in the history
Fix paraglide-js unplugin watcher for Windows systems
  • Loading branch information
samuelstroschein authored Jan 26, 2025
2 parents 10fb717 + ee0b9fe commit 04ae164
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { UnpluginFactory } from "unplugin";
import { compile, type CompilerOptions } from "../compiler/compile.js";
import fs from "node:fs";
import { resolve } from "node:path";
import { nodeNormalizePath } from "../utilities/node-normalize-path.js";

const PLUGIN_NAME = "unplugin-paraglide-js";

Expand All @@ -19,7 +20,8 @@ export const unpluginFactory: UnpluginFactory<CompilerOptions> = (args) => ({
}
},
async watchChange(path) {
if (readFiles.has(path)) {
const shouldCompile = readFiles.has(path) && !path.includes("cache");
if (shouldCompile) {
readFiles.clear();
await compile({
fs: wrappedFs,
Expand Down Expand Up @@ -50,15 +52,15 @@ const wrappedFs: typeof import("node:fs") = {
options: { encoding?: null; flag?: string } | null | undefined,
callback: (err: NodeJS.ErrnoException | null, data: Buffer) => void
) => {
readFiles.add(resolve(process.cwd(), path.toString()));
readFiles.add(nodeNormalizePath(resolve(process.cwd(), path.toString())));
return fs.readFile(path, options, callback);
},
// @ts-expect-error - Node's fs has too many overloads
readFileSync: (
path: fs.PathLike | number,
options?: { encoding?: null; flag?: string } | null | undefined
) => {
readFiles.add(resolve(process.cwd(), path.toString()));
readFiles.add(nodeNormalizePath(resolve(process.cwd(), path.toString())));
return fs.readFileSync(path, options);
},
promises: {
Expand All @@ -68,7 +70,7 @@ const wrappedFs: typeof import("node:fs") = {
path: fs.PathLike,
options?: { encoding?: null; flag?: string } | null
): Promise<Buffer> => {
readFiles.add(resolve(process.cwd(), path.toString()));
readFiles.add(nodeNormalizePath(resolve(process.cwd(), path.toString())));
return fs.promises.readFile(path, options);
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import path from "node:path";

const windowsSlashRE = /\\/g;
function slash(p: string): string {
return p.replace(windowsSlashRE, "/");
}

const isWindows =
typeof process !== "undefined" && process.platform === "win32";

export function nodeNormalizePath(id: string) {
return path.posix.normalize(isWindows ? slash(id) : id);
}

0 comments on commit 04ae164

Please sign in to comment.