Skip to content
Kit

Parsers

Define a Kubb parser with defineExtension and kind parser. It turns the AST of a generated file into the source written to disk.

A parser turns a FileNode into the source text Kubb writes. Define one with defineExtension and kind: 'parser'. build gets the user's options and returns the parser methods. meta and the factory statics work as for every extension.

For TypeScript and JavaScript output, use @kubb/parser-ts. When the config sets no parsers, defineConfig adds parserTs(), parserTsx(), and parserMd(). Write your own only for another language, such as Python, Kotlin, or Rust.
parserText.ts
import { defineExtension } from 'kubb/kit'

export const parserText = defineExtension({
  kind: 'parser',
  meta: { name: 'text', version: '1.0.0' },
  build() {
    return {
      extNames: ['.txt'],
      parse(file) {
        return file.sources
          .flatMap((source) => source.nodes ?? [])
          .map((node) => (node.kind === 'Text' ? node.value : ''))
          .join('\n')
      },
      print(...nodes) {
        return nodes.map(String).join('\n')
      },
    }
  },
})
A non-empty parsers array replaces the defaults. An empty one keeps them. To keep TypeScript and Markdown output, list the defaults again.
kubb.config.ts
import { defineConfig } from 'kubb/config'
import { parserMd } from '@kubb/parser-md'
import { parserTs, parserTsx } from '@kubb/parser-ts'
import { parserText } from './parserText.ts'

export default defineConfig({
  input: './petStore.yaml',
  output: { path: './src/gen' },
  parsers: [parserTs(), parserTsx(), parserMd(), parserText()],
})

build returns these members:

MemberTypeRequiredCalledPurpose
extNamesArray<FileNode['extname']> | undefinedYesThe file extensions this parser handles. With undefined, parse never runs.
parse(file: FileNode) => stringYesAfter every plugin, once per filePrints the file's sources. Must be synchronous.
print(...nodes: TNode[]) => stringYesBy plugins, while they generatePrints compiler AST nodes, such as ts.Node for parserTs.
inlineInlineLanguageNoBefore barrels, for inline sourcesLets files of this parser absorb inline sources. parserTs has it. See Inline sources.
copy(file: FileNode, source: string) => UserFileNodeNoFor each copy fileDescribes a copied template as nodes, such as its imports as ImportNodes, for parse to print. Without it, Kubb copies the file as is.

Parser selection and Parser fallback cover duplicate extensions and files no parser handles.

Parser naming

SurfacePatternExample
npm package@<scope>/parser-<name> or kubb-parser-<name>@kubb/parser-ts
meta.nameThe output language or format, in lowercase'typescript', 'markdown'
Factory exportparser<Name>parserTs, parserMd

Less common cases

Parser selection

When two parsers in parsers register the same file extension, the last one in the array wins.

Put the parser you want for that extension last.

Parser fallback

When no parser handles a file's extension, Kubb joins its sources as they are. To control the output, add a parser for that extension.