JS formatter for dprint Wasm plugins.
Setup
Deno:
deno add npm:@dprint/formatter
Node.js:
npm i @dprint/formatter
Use
The context API allows you to manage multiple plugins with shared configuration and automatic plugin selection based on file type:
import { createContext } from "@dprint/formatter"; import * as json from "@dprint/json"; import * as markdown from "@dprint/markdown"; import * as typescript from "@dprint/typescript"; import fs from "node:fs"; const context = createContext({ // global config indentWidth: 2, lineWidth: 80, }); // note: some plugins might have a getBuffer() export instead context.addPlugin(fs.readFileSync(typescript.getPath()), { semiColons: "asi", }); context.addPlugin(fs.readFileSync(json.getPath())); context.addPlugin(fs.readFileSync(markdown.getPath())); console.log(context.formatText({ filePath: "config.json", fileText: "{\"a\":1}", })); console.log(context.formatText({ filePath: "app.ts", fileText: "const x=1", }));
The context also handles host formatting automatically, so embedded code blocks (like JSON in Markdown) will be formatted by the appropriate plugin.
Plugin NPM Packages
Note: In the future I will ensure plugins are published to JSR as well.