diff --git a/apps/oxfmt/src-js/prettier-proxy.ts b/apps/oxfmt/src-js/prettier-proxy.ts index 1a4d2baf888f8..e567fcc07f819 100644 --- a/apps/oxfmt/src-js/prettier-proxy.ts +++ b/apps/oxfmt/src-js/prettier-proxy.ts @@ -2,8 +2,12 @@ import Tinypool from "tinypool"; import type { WorkerData, FormatEmbeddedCodeArgs, FormatFileArgs } from "./prettier-worker.ts"; // Worker pool for parallel Prettier formatting +// Used by each exported function let pool: Tinypool | null = null; +type SetupResult = string[]; +let setupCache: SetupResult | null = null; + // --- /** @@ -13,14 +17,17 @@ let pool: Tinypool | null = null; * @param numThreads - Number of worker threads to use (same as Rayon thread count) * @returns Array of loaded plugin's `languages` info * */ -export async function setupConfig(configJSON: string, numThreads: number): Promise { +export async function setupConfig(configJSON: string, numThreads: number): Promise { + // NOTE: When called from CLI, it's only called once at the beginning. + // However, when called via API, like `format(fileName, code)`, it may be called multiple times. + // Therefore, allow it by returning cached result. + if (setupCache !== null) return setupCache; + const workerData: WorkerData = { // SAFETY: Always valid JSON constructed by Rust side prettierConfig: JSON.parse(configJSON), }; - if (pool) throw new Error("`setupConfig()` has already been called"); - // Initialize worker pool for parallel Prettier formatting // Pass config via workerData so all workers get it on initialization pool = new Tinypool({ @@ -34,7 +41,9 @@ export async function setupConfig(configJSON: string, numThreads: number): Promi // - Read `plugins` field // - Load plugins dynamically and parse `languages` field // - Map file extensions and filenames to Prettier parsers - return []; + setupCache = []; + + return setupCache; } // ---