@maverick-js/compiler
Version:
Maverick toolchain including the analyzer and compiler.
129 lines (125 loc) • 4.52 kB
JavaScript
import { resolveConfigPaths, escapeQuotes, TS_NODE } from './chunks/chunk-FDW5IKT6.js';
export { TS_NODE } from './chunks/chunk-FDW5IKT6.js';
import { isUndefined } from './chunks/chunk-3M33KLBM.js';
import { dirname } from 'pathe';
var DEFAULT_CONFIG = {
cwd: process.cwd(),
outFile: "./elements.json"
};
var replacer = (key, value) => {
return key !== TS_NODE ? value : void 0;
};
var createJSONPlugin = (config = {}) => ({
name: "maverick/json",
async transform(components, fs) {
var _a;
const normalizedConfig = await normalizeJSONPluginConfig(config);
const output = {
components: []
};
components.forEach((component) => {
output.components.push(component);
});
const stringify = config.stringifyJson ?? JSON.stringify;
const finalOutput = ((_a = normalizedConfig.transformJson) == null ? void 0 : _a.call(normalizedConfig, output)) ?? output;
const dir = dirname(normalizedConfig.outFile);
const { existsSync, mkdirSync, writeFileSync } = await import('node:fs');
if (!existsSync(dir))
mkdirSync(dir, { recursive: true });
writeFileSync(
normalizedConfig.outFile,
stringify(finalOutput, replacer, 2)
);
}
});
async function normalizeJSONPluginConfig(config) {
return resolveConfigPaths(config.cwd ?? DEFAULT_CONFIG.cwd, {
...DEFAULT_CONFIG,
...config
});
}
var DEFAULT_CONFIG2 = {
cwd: process.cwd(),
outFile: "./vscode.html-data.json"
};
var primitiveTypeRE = /undefined|null|string|boolean|number|any|unknown|never/;
var createVSCodePlugin = (config = {}) => ({
name: "maverick/vscode-html-data",
async transform(components) {
const normalizedConfig = await normalizeVSCodePluginConfig(config);
const output = {
version: 1.1,
tags: []
};
components.filter((component) => !isUndefined(component.tag)).forEach((component) => {
var _a, _b;
const tagData = {
name: component.tag.name,
description: component.docs,
attributes: (component.props ?? []).filter((prop) => !!prop.attribute && !prop.readonly && !prop.internal).map((prop) => {
var _a2, _b2;
const data = {
name: prop.attribute,
description: prop.docs,
values: prop.type.includes("|") ? (_a2 = prop.type.split(/\s+\|\s+/)) == null ? void 0 : _a2.filter((value) => !primitiveTypeRE.test(value)).map((type) => ({ name: escapeQuotes(type) })) : void 0
};
return ((_b2 = config.transformAttributeData) == null ? void 0 : _b2.call(config, prop, data)) ?? data;
})
};
(_b = output.tags) == null ? void 0 : _b.push(((_a = config.transformTagData) == null ? void 0 : _a.call(config, component, tagData)) ?? tagData);
});
const dir = dirname(normalizedConfig.outFile);
const { existsSync, mkdirSync, writeFileSync } = await import('node:fs');
if (!existsSync(dir))
mkdirSync(dir, { recursive: true });
writeFileSync(normalizedConfig.outFile, JSON.stringify(output, void 0, 2));
}
});
async function normalizeVSCodePluginConfig(config) {
return resolveConfigPaths(config.cwd ?? DEFAULT_CONFIG2.cwd, {
...DEFAULT_CONFIG2,
...config
});
}
// src/analyze/meta/walk.ts
var propKeys = /* @__PURE__ */ new Set(["props", "events", "cssvars", "cssparts", "slots"]);
function walkComponentDocs(component, callback) {
var _a, _b;
if (component.docs) {
const newDocs = callback(component.docs);
if (newDocs)
component.docs = newDocs;
}
const keys = Object.keys(component);
for (const key of keys) {
if (key === "members") {
if ((_a = component.members) == null ? void 0 : _a.props) {
for (const prop of component.members.props) {
if (prop.docs) {
const newDocs = callback(prop.docs);
if (newDocs)
prop.docs = newDocs;
}
}
}
if ((_b = component.members) == null ? void 0 : _b.methods) {
for (const method of component.members.methods) {
if (method.docs) {
const newDocs = callback(method.docs);
if (newDocs)
method.docs = newDocs;
}
}
}
} else if (propKeys.has(key)) {
for (const prop of component[key]) {
if (typeof prop.docs === "string") {
const newDocs = callback(prop.docs);
if (newDocs)
prop.docs = newDocs;
}
}
}
}
}
export { createJSONPlugin, createVSCodePlugin, walkComponentDocs };