Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions apps/oxfmt/src-js/libs/prettier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ export async function formatEmbeddedCode({
// SAFETY: `options` is created in Rust side, so it's safe to mutate here
options.parser = parserName;

// Enable Tailwind CSS plugin for embedded code (e.g., html`...` in JS)
// when `options._tailwindPluginEnabled` is set
// Enable Tailwind CSS plugin for embedded code (e.g., html`...` in JS) if needed
await setupTailwindPlugin(options);

// NOTE: This will throw if:
Expand Down Expand Up @@ -92,8 +91,7 @@ export async function formatFile({
// But some plugins rely on `filepath`, so we set it too
options.filepath = fileName;

// Enable Tailwind CSS plugin for non-JS files
// when `options._tailwindPluginEnabled` is set
// Enable Tailwind CSS plugin for non-JS files if needed
await setupTailwindPlugin(options);

return prettier.format(code, options);
Expand All @@ -118,22 +116,29 @@ async function loadTailwindPlugin(): Promise<typeof import("prettier-plugin-tail

// ---

const TAILWIND_RELEVANT_PARSERS = new Set(["html", "vue", "angular", "glimmer"]);

/**
* Set up Tailwind CSS plugin for Prettier when _tailwindPluginEnabled is set.
* Set up Tailwind CSS plugin for Prettier when:
* - `options._tailwindPluginEnabled` is set
* - And, the parser is relevant for Tailwind CSS
* Loads the plugin lazily. Option mapping is done in Rust side.
*/
async function setupTailwindPlugin(
options: Options & { _tailwindPluginEnabled?: boolean },
): Promise<void> {
if (!options._tailwindPluginEnabled) return;

// Clean up internal flag
delete options._tailwindPluginEnabled;

// PERF: Skip loading Tailwind plugin for parsers that don't use it
if (!TAILWIND_RELEVANT_PARSERS.has(options.parser as string)) return;

const tailwindPlugin = await loadTailwindPlugin();

options.plugins = options.plugins || [];
options.plugins.push(tailwindPlugin as Plugin);

// Clean up internal flag for sure
delete options._tailwindPluginEnabled;
}

// ---
Expand Down
Loading