Reference › APIs

On this page

clarusjs/compiler

The compiler, for tools: an editor, a linter, a docs generator, a different bundler. Apps don't need it; the Vite plugin calls it.

ts
import { compile, parse, ClarusError } from 'clarusjs/compiler'

compile() #

ts
compile(source: string, options: CompileOptions): CompileResult

Compiles a .tscl component. For the server target, the result is a module whose render(props) returns the component's HTML. For the client target, it's an island's attach(root, props), or export {} when the component has no script.

Parameters #

Returns #

A CompileResult:

Caveats #

Usage #

ts
const { code, css, stateful, warnings } = compile(source, { file: 'src/components/card.tscl', target: 'server', clientUrl: '/card.js' })
for (const w of warnings) console.warn(w.message)

parse() #

ts
parse(source: string, file: string): ParsedFile

Parses a .tscl file into its syntax tree, without compiling it.

Returns #

A ParsedFile:

Every node has a start offset into the source.

Caveats #

Usage #

ts
const { inputs, imports } = parse(source, 'src/components/card.tscl')
console.log(inputs.map((i) => `${i.name}: ${i.type}`))

ClarusError #

ts
class ClarusError extends Error { file: string; line: number; column: number }

A compile error pointing at a place in a .tscl file. Its message reads file:line:column text (1-based), and the place is also in file, line and column. compile() and parse() throw it, and compile()'s warnings are ClarusErrors too.

ts
try {
  compile(source, { file, target: 'server' })
} catch (err) {
  if (err instanceof ClarusError) report(err.file, err.line, err.column, err.message)
  else throw err
}