Skip to content
文档
使用
@swc/wasm

@swc/wasm-web

This modules allows you to synchronously transform code inside the browser using WebAssembly.

Usage

You must first initialize the module before you can use it.

App.jsx
import { useEffect, useState } from "react";
import initSwc, { transformSync } from "@swc/wasm-web";
 
export default function App() {
  const [initialized, setInitialized] = useState(false);
 
  useEffect(() => {
    async function importAndRunSwcOnMount() {
      await initSwc();
      setInitialized(true);
    }
    importAndRunSwcOnMount();
  }, []);
 
  function compile() {
    if (!initialized) {
      return;
    }
    const result = transformSync(`console.log('hello')`, {});
    console.log(result);
  }
 
  return (
    <div className="App">
      <button onClick={compile}>Compile</button>
    </div>
  );
}