An esbuild plugin that parses the Swagger specs (YAML and JSON).
import esbuild from 'esbuild'
import { SwaggerSpecParser } from 'esbuild-plugin-swagger-spec-parser'
await esbuild.build({
/* all of your config */,
plugins: [
SwaggerSpecParser,
]
})
The entrypoint files for swagger specification must be named with swagger.(ya?ml|json)
as suffix. e.g. openapi.swagger.yaml
, or openapi.swagger.json
.
This allows the plugin to identify which files should be parsed.
const specs = require("./openapi.swagger.json");
const specsFromYaml = require("./openapi.swagger.yaml");
console.log(specs); // Parsed to a JSON format
console.log(specsFromYaml); // Parsed to a JSON format
When working with typescript, you might want to specify the type declarations of the swagger spec file you're importing.
You can currently do that be specifying the reference to the node_module ambient declarations of the plugin.
/// <reference path="../node_modules/esbuild-plugin-swagger-spec-parser/dist/custom.d.ts" />
const specs = require("./openapi.swagger.yaml");
console.log(specs); // Parsed to a JSON format
MIT License link.