Oxc Formatter (oxfmt)

0.45.0 · active · verified Sun Apr 19

Oxfmt is a high-performance, zero-configuration code formatter for JavaScript and TypeScript, built as part of the broader Oxidation Compiler (Oxc) project. It offers a fast alternative to tools like Prettier, leveraging Rust for superior performance. The package is currently at `v0.45.0` and is under active development, with frequent releases often tied to its companion linter, Oxlint, and the underlying Oxc Rust crates (typically weekly to bi-weekly for the monorepo). Its primary differentiators are its exceptional speed, minimal configuration overhead (enforcing an opinionated style), and its tight integration within the Oxc ecosystem, which aims to provide a complete suite of JavaScript tooling written in Rust. It ships with TypeScript types, supporting both programmatic usage via `formatSync` and `formatAsync` functions, and a direct command-line interface.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `oxfmt` programmatically to format both a string synchronously and a local file asynchronously, showcasing its core API functions.

import { formatSync, formatAsync } from 'oxfmt';
import { readFileSync, writeFileSync, rmSync } from 'node:fs';
import { join } from 'node:path';

// Example: Synchronously format a string
const messyCode = `function  add ( a, b )  { return  a  +  b; }`;
const formattedCodeSync = formatSync(messyCode);
console.log('Synchronously formatted code:', formattedCodeSync);
// Expected output: "function add(a, b) { return a + b; }"

// Example: Asynchronously format a file
async function formatFile(filePath: string) {
  try {
    const sourceText = readFileSync(filePath, 'utf-8');
    const formattedText = await formatAsync(sourceText);
    console.log(`Formatted ${filePath}:\n`, formattedText);
    // Optionally write back to file
    // writeFileSync(filePath, formattedText);
  } catch (error) {
    console.error(`Error formatting file ${filePath}:`, error);
  }
}

// Create a dummy file for demonstration
const dummyFilePath = join(process.cwd(), 'temp-file.js');
writeFileSync(dummyFilePath, `
  const   foo    =     "bar"   ;
  if   (  true  )  { console.log(foo)  ; }`);

console.log('Attempting to format a dummy file...');
formatFile(dummyFilePath).then(() => {
  console.log('Formatting complete for dummy file.');
  // Clean up dummy file
  rmSync(dummyFilePath);
});

view raw JSON →