1.2 KB36 lines
Blame
1import type { JSONSchemaType } from 'ajv/dist/2019.js';
2import type { MermaidConfig } from '../packages/mermaid/src/config.type.js';
3import { readFile } from 'node:fs/promises';
4import { getDefaults, getSchema, loadSchema } from '../.build/jsonSchema.js';
5
6/**
7 * ESBuild plugin that handles JSON Schemas saved as a `.schema.yaml` file.
8 *
9 * Use `my-example.schema.yaml?only-defaults=true` to only load the default values.
10 */
11
12export const jsonSchemaPlugin = {
13 name: 'json-schema-plugin',
14 setup(build) {
15 let schema: JSONSchemaType<MermaidConfig> | undefined = undefined;
16 let content = '';
17
18 build.onLoad({ filter: /config\.schema\.yaml$/ }, async (args) => {
19 // Load the file from the file system
20 const source = await readFile(args.path, 'utf8');
21 const resolvedSchema: JSONSchemaType<MermaidConfig> =
22 content === source && schema ? schema : loadSchema(source, args.path);
23 if (content !== source) {
24 content = source;
25 schema = resolvedSchema;
26 }
27 const contents = args.suffix.includes('only-defaults')
28 ? getDefaults(resolvedSchema)
29 : getSchema(resolvedSchema);
30 return { contents, warnings: [] };
31 });
32 },
33};
34
35export default jsonSchemaPlugin;
36