Oxc Minify
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
-
Error: The 'filename' argument must be of type string. Received type undefined
cause The `filename` parameter in `minifySync` or `minify` was not provided or was `undefined`.fixAlways provide a string for the `filename` argument, even if it's a placeholder like `'input.js'`. -
SyntaxError: Expected an identifier but found 'class'
cause Input code contains syntax errors that `oxc-minify`'s parser cannot handle, potentially due to unsupported syntax or malformed code.fixEnsure the input `sourceText` is valid JavaScript/TypeScript. `oxc-minify` expects semantically correct code and may not gracefully handle all malformed inputs. -
TypeError: Cannot read properties of undefined (reading 'code')
cause The `minifySync` or `minify` function might have thrown an error, causing `result` to be undefined, and then you tried to access `result.code`.fixWrap your minification call in a `try...catch` block for `minifySync` or use `.catch()` for `minify` to handle potential errors during the minification process.
Warnings
- breaking The `allocator` module's `Box` and `Vec` methods were renamed, which can affect custom Rust-based extensions or deeper integrations.
- breaking The `oxc_str` crate underwent multiple breaking changes, including the removal of identity `FromIn` impl for `Ident` and re-exports of string types from `oxc_span`, along with the introduction of a new `static_ident!` macro.
- gotcha Oxc Minify is currently alpha software and may produce incorrect results or not fully optimize code as expected. It lacks some advanced minification techniques like constant inlining and dead code removal.
- gotcha To maximize performance, `oxc-minify` assumes the input code is semantically correct. It uses a fast parsing mode that skips checks for semantic errors related to symbols and scopes.
- breaking Oxlint's internal `span` and `str` crate re-exports were removed, potentially impacting applications that directly interfaced with these internal Rust crates.
Install
-
npm install oxc-minify -
yarn add oxc-minify -
pnpm add oxc-minify
Imports
- minifySync
const { minifySync } = require('oxc-minify');import { minifySync } from 'oxc-minify'; - minify
import minify from 'oxc-minify'; // Not a default export
import { minify } from 'oxc-minify'; - MinifyOptions
import type { MinifyOptions } from 'oxc-minify';
Quickstart
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);
// })();