Guide › Tooling

On this page

Formatting

@clarusjs/prettier-plugin formats .tscl components with Prettier: the template is indented like any HTML, <script> is formatted as JavaScript and <style> as CSS, all with your project's Prettier options.

Set up #

Install Prettier and the plugin:

pnpm add -D prettier @clarusjs/prettier-plugin

Add the plugin to your Prettier config:

json
// .prettierrc
{
  "plugins": ["@clarusjs/prettier-plugin"]
}

Then format as usual:

sh
npx prettier --write "src/**/*.tscl"
npx prettier --check "src/**/*.tscl"   # in CI: fails if a file isn't formatted

What it changes #

For example, this:

tscl
@input title: string
<script>
let open=state(false)
</script>
<section>@if (open) {<p>{{ title }}</p>} @else {<p>Closed</p>}</section>

becomes:

tscl
@input title: string

<script>
  let open = state(false);
</script>
<section>
  @if (open) {
    <p>{{ title }}</p>
  } @else {
    <p>Closed</p>
  }
</section>

The indentation reaches your HTML, since whitespace inside elements is kept: on this site, formatting made pages 11% bigger, but only 2% gzipped.

Whitespace is kept where it shows on the page: <b>a</b> <i>b</i> keeps its space, and <b>a</b><i>b</i> stays without one.

In your editor #

With the language server, Format Document (and format on save) formats .tscl files with your project's Prettier and the plugin, using the same config as the command line. It needs both installed in the project, and a trusted workspace, since formatting runs your project's code. Only a Prettier config inside the workspace folder is used (one in a parent folder could load plugins from outside it). If a file can't be formatted (a block isn't closed, say), nothing changes, and the reason is in the clarusjs output.

oxfmt #

oxfmt can't load third-party Prettier plugins yet (plugin support is on its roadmap). Until it can, format .tscl files with Prettier, and the rest of your project with oxfmt if you like:

sh
npx oxfmt
npx prettier --write "src/**/*.tscl"