Closed1

Biome の Format を Web Worker で動かす

uttkuttk

TD;LR

./worker.js
import initBiome, { Workspace } from "https://esm.sh/@biomejs/wasm-web@1.9.4";
import * as Comlink from "https://esm.sh/comlink@4.4.2";

const apis = {
  format: async (code) => {
    await initBiome({
      module_or_path: "https://esm.sh/@biomejs/wasm-web@1.9.4/biome_wasm_bg.wasm",
    });
    const workspace = new Workspace();
    const file = {
      path: {
        path: "index.js",
        kind: ["Handleable"],
        was_written: false,
      },
      version: 0,
      content: code,
    };
    workspace.openFile(file);
    const result = workspace.formatFile({ path: file.path });
    return result.code;
  },
};

Comlink.expose(apis);
./index.js
import * as Comlink from "comlink";

const worker = new Worker("./worker.js", { type: "module" });
const apis = Comlink.wrap(worker);
const result = await apis.format(`function

Hoge() {
  console.log("hoge");
}`);

console.log({ result });
// output:
// function Hoge() {
//   console.log("hoge");
// }
このスクラップは2025/03/02にクローズされました