Oxc Minify

0.126.0 · active · verified Sun Apr 19

Oxc Minify is a JavaScript minifier built in Rust, providing a Node.js API for synchronous and asynchronous code minification. Currently at version 0.126.0, it is under active and rapid development, with frequent releases often introducing breaking changes as the project matures. It is designed for high performance, already outperforming `esbuild` in some benchmarks, and aims to eventually include advanced minification techniques like constant inlining and dead code removal. A key differentiator is its Rust-based architecture, offering potential speed advantages over JavaScript-based minifiers like Terser or UglifyJS. However, it is explicitly alpha software, making assumptions about semantically correct input and using a fast parsing mode that skips some semantic error checks to maximize performance.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates both synchronous and asynchronous minification of a JavaScript code string, showcasing common options for compression, mangling, and code generation, along with sourcemap generation.

import { minifySync } from "oxc-minify";

const filename = "test.js";
const code = "const x = 'a' + 'b'; console.log(x); function sum(a, b) { return a + b; } console.log(sum(1,2));";
const options = {
  compress: {
    target: "esnext",
    // Example of a common compression option
    inline: 2 // Inline small functions where possible
  },
  mangle: {
    toplevel: false,
    // Example of a common mangling option
    properties: false // Do not mangle properties for now
  },
  codegen: {
    removeWhitespace: true,
    // Example of a common code generation option
    quote: 'single' // Use single quotes for strings
  },
  sourcemap: true,
};

// Synchronous minification
const resultSync = minifySync(filename, code, options);
console.log('Synchronous result:');
console.log(resultSync.code);
// console.log(resultSync.map); // Source map can be logged if needed

// Asynchronous minification (uncomment to use)
// (async () => {
//   const resultAsync = await minify(filename, code, options);
//   console.log('\nAsynchronous result:');
//   console.log(resultAsync.code);
//   // console.log(resultAsync.map);
// })();

view raw JSON →