Skip to content
开发文档
Usage
@swc/wasm

@swc/wasm-web

该模块允许你使用 WebAssembly 在浏览器内同步转换代码。

¥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>
  );
}
Last updated on November 24, 2023