Skip to content

dprint/js-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0d345ff · Jul 1, 2024

History

25 Commits
Jun 30, 2024
Jun 30, 2024
Jun 30, 2024
Jun 30, 2024
May 4, 2024
Jul 1, 2024
Jun 30, 2024
Jun 30, 2024
Jun 30, 2024
Jul 1, 2024
Jun 30, 2024
Jun 30, 2024
Jun 30, 2024
Jun 30, 2024

Repository files navigation

dprint - JS Formatter

CI npm version JSR

JS formatter for dprint Wasm plugins.

Setup

Deno:

deno add @dprint/formatter

Node.js:

npm i @dprint/formatter

Use

import { createStreaming, GlobalConfiguration } from "@dprint/formatter";

const globalConfig: GlobalConfiguration = {
  indentWidth: 2,
  lineWidth: 80,
};
const tsFormatter = await createStreaming(
  // check https://plugins.dprint.dev/ for latest plugin versions
  fetch("https://plugins.dprint.dev/typescript-0.57.0.wasm"),
);

tsFormatter.setConfig(globalConfig, {
  semiColons: "asi",
});

// outputs: "const t = 5\n"
console.log(tsFormatter.formatText({
  filePath: "file.ts",
  fileText: "const   t    = 5;",
}));

Using with plugins on npm (ex. @dprint/json):

import { createFromBuffer } from "@dprint/formatter";
// You may have to use `getBuffer` on plugins that haven't updated yet.
// See the plugins README.md for details.
import { getPath } from "@dprint/json";
import * as fs from "node:fs";

const buffer = fs.readFileSync(getPath());
const formatter = createFromBuffer(buffer);

console.log(formatter.formatText({
  filePath: "test.json",
  fileText: "{test: 5}",
}));

Plugin NPM Packages

Note: In the future I will ensure plugins are published to JSR as well.