Skip to content

generate-types + TSX (JSX) issue #26

@knightedcodemonkey

Description

@knightedcodemonkey

Running knighted-css-generate-types --root . fails when it hits JSX. The CLI feeds each source file directly into es-module-lexer.parse(), which only understands standard JavaScript module syntax, so raw TSX causes a parse error before ?knighted-css&types imports are discovered.

Potential fixes

  1. Inline transpile before parsing – Run files through a lightweight transformer (esbuild, sucrase, @swc/core) to strip TypeScript/JSX down to JS, then hand the result to parse().
  2. Use typescript.transpileModule() – Invoke the TS compiler with { module: ESNext, jsx: ReactJSX, target: ES2022 } to emit JS (React.createElement) while preserving import specifiers.
  3. Expose a lexer override hook – Promote the internal setLexerOverrides() helper so consumers can provide a JSX-capable parser (e.g., acorn + acorn-jsx), falling back to es-module-lexer for plain JS.
  4. Heuristic retry – Wrap parse() in try/catch; if a .tsx/.jsx file fails, run a minimal JSX-stripping pass and retry to avoid new dependencies for vanilla projects.

The goal is to pick one (or combine several) so @knighted/css can parse JSX-heavy codebases without extra setup.

For now a workaround is to use @knighted/jsx to write JSX syntax within tagged template literals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions