npm install @wasm-fmt/mago_fmtnpx jsr add @fmt/mago-fmtimport init, { format } from "@wasm-fmt/mago_fmt";
await init();
const input = `<?php
function hello( \$name ) {
echo "Hello, " . \$name;
}
?>`;
const formatted = format(input);
console.log(formatted);with custom options:
import init, { format } from "@wasm-fmt/mago_fmt";
await init();
const input = `<?php
function hello( \$name ) {
echo "Hello, " . \$name;
}
?>`;
const formatted = format(input, "main.php", {
"use-tabs": false,
"tab-width": 4,
"print-width": 120,
});
console.log(formatted);with specific PHP version:
import init, { format_with_version } from "@wasm-fmt/mago_fmt";
await init();
const input = `<?php
function hello( \$name ) {
echo "Hello, " . \$name;
}
?>`;
const formatted = format_with_version(input, "8.3", "main.php", {
"use-tabs": false,
"tab-width": 4,
"print-width": 120,
});
console.log(formatted);For Vite users:
Add "@wasm-fmt/mago_fmt" to optimizeDeps.exclude in your vite config:
{
"optimizeDeps": {
"exclude": ["@wasm-fmt/mago_fmt"]
}
}If you cannot change the vite config, you can use another import entry
import init, { format } from "@wasm-fmt/mago_fmt/vite";
// ...Mago is an extremely fast PHP linter, formatter, and static analyzer, written in Rust.
This package is a WebAssembly build of the Mago formatter, with a JavaScript wrapper.
Thanks to:
- The Mago project created by Carthage Software