fakeReact

tcmts.core.test.hash.fakeReact · #aoyfothr · term · runs on the server

uses node:path

A minimal React for JSX: the automatic runtime's types, pinned at a version.

import fs from "node:fs";
import path from "node:path";

export function fakeReact(root: string) {
  const dir = path.join(root, "node_modules", "react");
  fs.mkdirSync(dir, { recursive: true });
  fs.writeFileSync(
    path.join(dir, "package.json"),
    JSON.stringify({ name: "react", version: "19.0.0", exports: { ".": { types: "./index.d.ts" }, "./jsx-runtime": { types: "./jsx-runtime.d.ts" } } }),
  );
  fs.writeFileSync(path.join(dir, "index.d.ts"), "export declare function useState<T>(v: T): [T, (v: T) => void];\n");
  fs.writeFileSync(
    path.join(dir, "jsx-runtime.d.ts"),
    "export namespace JSX { interface Element { el: true } interface IntrinsicElements { [tag: string]: any } interface ElementChildrenAttribute { children: {} } }\n" +
      "export declare function jsx(type: any, props: any): JSX.Element;\nexport declare function jsxs(type: any, props: any): JSX.Element;\nexport declare const Fragment: unique symbol;\n",
  );
}

used by