A utility for extracting JSON schema objects from a Stylable stylesheet
npm install @stylable/schema-extract
@stylable/schema-extract is a utility that allows you to transform Stylable stylesheets into JSON-Schema compatible format.
``sh`
yarn add @stylable/schema-extractextractSchemaUsage
Import the utility function from @stylable/schema-extract, and invoke it.extractSchema
The function receives four arguments, css, filePath, rootPath and path.
object containing a minimal set of required utility methods|#### MinimalPath interface
`ts
export interface MinimalPath {
dirname: (p: string) => string;
join: (...paths: string[]) => string;
isAbsolute: (path: string) => boolean;
relative: (from: string, to: string) => string;
}
`Example
Usage example for extractSchema.`ts
import fs from 'fs';
import path from 'path';
import { extractSchema } from '@stylable/schema-extract';const filePath = path.join(__dirname, 'src/entry.st.css');
const css = fs.readFileSync(filePath, 'utf8');
const stylesheetSchema = extractSchema(
css,
filePath,
__dirname,
path
);
`$3
`css
/ ~/myproject/src/entry.st.css /
:import {
-st-from: './imported.st.css';
-st-default: Comp;
-st-named: part;
}
:vars {
myColor: red;
}
.root {
-st-extends: Comp;
}
.otherPart {
-st-extends: part;
}`$3
`JSON
{
"$id": "/entry.st.css",
"$ref": "stylable/module",
"properties": {
"root": {
"$ref": "stylable/class",
"states": {
"userSelected": {
"type": "boolean"
}
},
"extends": {
"$ref": "/imported.st.css#root"
}
},
"Comp": {},
"part": {},
"myColor": {
"$ref": "stylable/var"
},
"otherPart": {
"$ref": "stylable/class",
"states": {
"size": {
"type": "enum",
"enum": [
"s",
"m",
"l"
]
}
},
"extends": {
"$ref": "/imported.st.css#part"
}
}
}
}
``