⛋ @terrazzo/parser
The JS API is a lower-level API than the CLI that can be used to build Design Token systems. The JS API runs anywhere JavaScript does, including in Node.js, the browser, and serverless functions.
Basic Usage
npm i -D @terrazzo/parser
And here’s a basic example showing config
, parse
, and build
steps:
import { defineConfig, parse, build } from "@terrazzo/parser";
const config = defineConfig(
{
},
{ cwd: new URL(import.meta.url) }
);
const rawTokens = "(any JSON or YAML)";
const { tokens, sources } = await parse(
[{ filename: new URL("file:///tokens.json"), src: rawTokens }],
{ config }
);
const buildResult = await build(tokens, { sources, config });
It’s worth noting the JS API is a little more manual work than the CLI:
parse()
and build()
are distinct steps that each do some of the work.
defineConfig()
needs a cwd so it can resolve files (this can even be a remote URL, so long as it’s a URL())
- The AST generated from
parse()
must get passed into build()
so the error messages can point to the right lines in the source file.
- The
build()
step only returns a final array of outputFiles
in memory but doesn’t write them to disk. It’s up to you to write them to disk, upload them somewhere, etc.
Full documentation