aboutsummaryrefslogtreecommitdiff
path: root/src/api/demo/pages/scan.tsx
blob: 656629dda1653c980e1f5dc58fa703f98312e16c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import Head from "next/head";
import { readFile } from "fs/promises";
import styles from "../styles/Home.module.css";

import React from "react";

if (typeof window !== "undefined") {
  globalThis.Scan = await import("../lib/scan");
  await import("../lib/api");
}

export async function getStaticProps(ctx) {
  return {
    props: {
      // not tested
      code: readFile("/Users/jarred/Build/es-module-lexer/test/samples/magic-string.js", { encoding: "utf-8" }),
      defaultFile: "magic-string.js",
    },
  };
}

var textDecoder = new TextDecoder();
export default function Home({ code, defaultFile }) {
  const fileNameRef = React.useRef<HTMLInputElement>(null);
  const [lexer, setLexer] = React.useState("");
  const [bunResult, setBunResult] = React.useState("");
  const [file, setFile] = React.useState(defaultFile);
  React.useEffect(() => {
    globalThis.Scan.start();
  }, []);

  const runBuild = React.useCallback(
    event => {
      globalThis.Scan.transform(event.target.value, fileNameRef?.current?.value).then(result => {
        setLexer(JSON.stringify(result.lexer, null, 2));
        setBunResult(JSON.stringify(result.bun, null, 2));
      }, console.error);
    },
    [fileNameRef, setBunResult, setLexer],
  );
  return (
    <div className={styles.container}>
      <Head>
        <title>Next.js</title>
        <meta name="description" content="Generated by create next app" />
        <link rel="icon" href="/favicon.ico" />
      </Head>

      <main className={styles.main}>
        <div>
          <input
            autoComplete="filename"
            type="text"
            placeholder="filename"
            value={file}
            onChange={event => setFile(event.target.value)}
            ref={fileNameRef}
          />
          <textarea onChange={runBuild} defaultValue={code}></textarea>

          <textarea readOnly value={bunResult}></textarea>
          <textarea readOnly value={lexer}></textarea>
        </div>
      </main>
    </div>
  );
}