2024-06-29 16:09:50 +05:30
|
|
|
import type { PluginOption } from 'vite';
|
2023-08-13 18:33:41 +05:30
|
|
|
import { getDefaults, getSchema, loadSchema } from '../.build/jsonSchema.js';
|
2022-12-22 01:19:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:33:41 +05:30
|
|
|
const jsonSchema = loadSchema(src, idAsUrl.pathname);
|
|
|
|
return {
|
|
|
|
code: idAsUrl.searchParams.get('only-defaults')
|
|
|
|
? getDefaults(jsonSchema)
|
|
|
|
: getSchema(jsonSchema),
|
|
|
|
map: null, // no source map
|
|
|
|
};
|
2022-12-22 01:19:02 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|