0.1.0 โ€ข Published 5 months ago

@tsxd/core v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

TSXD (TSX Deserializer)

TSXD is a lightweight library that converts TSX code into a structured JSON format. This can be useful for various applications, such as building native Linux UIs with React, parsing components for static analysis, or generating UI definitions dynamically.

๐Ÿš€ Features

  • Convert TSX to JSON effortlessly.
  • Resolve and include imported components.
  • Handle nested elements, attributes, and dynamic expressions.

๐Ÿ› ๏ธ Installation

Install TSXD via npm:

npm install @tsxd/core

๐Ÿ“– Usage

Import tsxToJson and pass your TSX code as a string:

import { tsxToJson } from "@tsxd/core";

const tsxCode = `
  export default function App() {
    return <div className="my-class">Hello World</div>;
  }
`;

const result = await tsxToJson(tsxCode);
console.log(result);

๐Ÿ” Sample Output

{
  "body": [
    {
      "type": "div",
      "className": "my-class",
      "props": {
        "children": ["Hello World"]
      }
    }
  ],
  "imports": {}
}

โš™๏ธ Advanced Usage

Resolving Imported Components

You can pass an optional resolvePath to parse and include imported components from the file system:

const tsxCode = fs.readFileSync("tests/files/import-component.tsx", "utf-8");
const result = await tsxToJson(tsxCode, { resolvePath: "tests/files" });
console.log(result);

Sample Output with Imports

{
  "body": [
    {
      "type": "div",
      "className": "my-class",
      "props": {
        "children": [
          {
            "type": "Dummy",
            "props": { "children": [] }
          }
        ]
      }
    }
  ],
  "imports": {
    "Dummy": {
      "type": "div",
      "props": { "children": ["Dummy Component"] }
    }
  }
}

๐Ÿงช Testing

TSXD includes unit tests with vitest. Run them with:

npm test

โš ๏ธ Limitations

  • Dynamic expressions beyond simple literals might not be fully supported.
  • Imports must be accessible via the provided resolvePath.

๐Ÿค Contributing

Contributions are welcome! Feel free to open issues or submit pull requests.

๐Ÿ“œ License

MIT License


Happy parsing! ๐Ÿ› ๏ธโšก

0.1.0

5 months ago