Skip to content

wasm-fmt/mago_fmt

Repository files navigation

Test

Install

npm

npm install @wasm-fmt/mago_fmt

jsr.io

npx jsr add @fmt/mago-fmt

Usage

import 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";

// ...

How does it work?

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.

Credits

Thanks to:

Packages

 
 
 

Contributors 2

  •  
  •  

Languages