821 B29 lines
Blame
1import type { PluginOption } from 'vite';
2import { getDefaults, getSchema, loadSchema } from '../.build/jsonSchema.js';
3
4/**
5 * Vite plugin that handles JSON Schemas saved as a `.schema.yaml` file.
6 *
7 * Use `my-example.schema.yaml?only-defaults=true` to only load the default values.
8 */
9export default function jsonSchemaPlugin(): PluginOption {
10 return {
11 name: 'json-schema-plugin',
12 transform(src: string, id: string) {
13 const idAsUrl = new URL(id, 'file:///');
14
15 if (!idAsUrl.pathname.endsWith('schema.yaml')) {
16 return;
17 }
18
19 const jsonSchema = loadSchema(src, idAsUrl.pathname);
20 return {
21 code: idAsUrl.searchParams.get('only-defaults')
22 ? getDefaults(jsonSchema)
23 : getSchema(jsonSchema),
24 map: null, // no source map
25 };
26 },
27 };
28}
29