Files
mermaid/.vite/jsonSchemaPlugin.ts
2023-08-11 20:49:39 +05:30

34 lines
937 B
TypeScript

import { PluginOption } from 'vite';
import { getDefaults, getSchema, loadSchema } from '../.build/jsonSchema.js';
/**
* Vite plugin that handles JSON Schemas saved as a `.schema.yaml` file.
*
* Use `my-example.schema.yaml?only-defaults=true` to only load the default values.
*/
export default function jsonSchemaPlugin(): PluginOption {
return {
name: 'json-schema-plugin',
transform(src: string, id: string) {
const idAsUrl = new URL(id, 'file:///');
if (!idAsUrl.pathname.endsWith('schema.yaml')) {
return;
}
const jsonSchema = loadSchema(src, idAsUrl.pathname);
if (idAsUrl.searchParams.get('only-defaults')) {
return {
code: getDefaults(jsonSchema),
map: null, // no source map
};
} else {
return {
code: getSchema(jsonSchema),
map: null, // provide source map if available
};
}
},
};
}