From 9c0ad14cf5115fe4db06c881d988668bbe77d392 Mon Sep 17 00:00:00 2001 From: Jorge Florentino Date: Tue, 25 Nov 2025 00:58:26 +0100 Subject: [PATCH] fix: escape Windows paths in esbuild plugin regex filter - Add escapeForRegExp function to safely escape special regex characters - Fix issue where Windows paths with backslashes caused esbuild to fail - Ensures compatibility with esbuild's Go/RE2 regex engine --- .../htmldocs/src/utils/htmldocs-esbuild-plugin.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/htmldocs/src/utils/htmldocs-esbuild-plugin.ts b/packages/htmldocs/src/utils/htmldocs-esbuild-plugin.ts index 8e129ba..f4bdc4f 100644 --- a/packages/htmldocs/src/utils/htmldocs-esbuild-plugin.ts +++ b/packages/htmldocs/src/utils/htmldocs-esbuild-plugin.ts @@ -5,6 +5,17 @@ import { Documentation, parse } from "react-docgen"; import * as tsj from "ts-json-schema-generator"; import { DOCUMENT_SCHEMAS_DIR } from "./paths"; +/** + * Escapes special regex characters in a string to make it safe for use in RegExp. + * This is necessary for Windows paths that contain backslashes and other special characters. + * + * @param s - The string to escape + * @returns The escaped string safe for use in RegExp + */ +export function escapeForRegExp(s: string): string { + return String(s).replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&"); +} + /** * Made to export the `renderAsync` function out of the user's email template * so that issues like https://github.com/resend/react-email/issues/649 don't @@ -18,8 +29,10 @@ import { DOCUMENT_SCHEMAS_DIR } from "./paths"; export const htmldocsPlugin = (documentTemplates: string[], isBuild: boolean) => ({ name: "htmldocs-plugin", setup: (b: PluginBuild) => { + // Escape each path to handle Windows backslashes and other special characters + const escapedPaths = documentTemplates.map(escapeForRegExp); b.onLoad( - { filter: new RegExp(documentTemplates.join("|")) }, + { filter: new RegExp(escapedPaths.join("|")) }, async ({ path: pathToFile }) => { let contents = await fs.promises.readFile(pathToFile, "utf8"); await generateAndWriteSchema(contents, pathToFile);