Spaces:
Running
Running
File size: 4,922 Bytes
518343a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | import { readFileSync } from 'node:fs';
import path from 'node:path';
import type { Plugin } from 'vite';
const VIRTUAL_ID = 'virtual:shared-ui-manifest';
const RESOLVED_ID = `\0${VIRTUAL_ID}`;
export interface SharedUiExport {
name: string;
isComponent: boolean;
}
const ALL_CAPS_RE = /^[A-Z][A-Z0-9_]+$/;
const NON_COMPONENT_SUFFIXES = [
'Props',
'Config',
'Options',
'Type',
'Types',
'Colors',
'Color',
'Styles',
'Style',
'Labels',
'Label',
'Status',
'Statuses',
'Mode',
'Event',
'Events',
'Context',
'Schema',
'Entry',
'Item',
'Items',
'Args',
'Params',
'Payload',
'Result',
'Response',
'Request',
'Error',
'Action',
'Actions',
'Spec',
'Def',
'Info',
'Meta',
'Map',
'Signal',
'Level',
'Severity',
'Category',
];
export function classifyName(name: string): boolean {
if (!name || !/^[A-Z]/.test(name)) return false;
if (ALL_CAPS_RE.test(name)) return false;
for (const suffix of NON_COMPONENT_SUFFIXES) {
if (name.endsWith(suffix)) return false;
}
return true;
}
function processExportItem(raw: string, seen: Set<string>, out: SharedUiExport[]): void {
const trimmed = raw.trim();
if (!trimmed) return;
if (trimmed.startsWith('type ') || trimmed === 'type') return;
const aliasMatch = trimmed.match(/^(\w+)\s+as\s+(\w+)$/);
const finalName = aliasMatch ? aliasMatch[2] : trimmed.replace(/\s+as\s+\w+$/, '').trim();
if (!finalName || finalName === 'default' || seen.has(finalName)) return;
seen.add(finalName);
out.push({ name: finalName, isComponent: classifyName(finalName) });
}
export function parseExportsFromIndex(indexPath: string): SharedUiExport[] {
let content: string;
try {
content = readFileSync(indexPath, 'utf-8');
} catch {
return [];
}
const exports: SharedUiExport[] = [];
const seen = new Set<string>();
const lines = content.split('\n');
let blockBuffer = '';
let inBlock = false;
let isTypeBlock = false;
for (const rawLine of lines) {
const line = rawLine.trim();
if (!inBlock) {
if (/^export\s+type\s+\{/.test(line)) {
isTypeBlock = true;
inBlock = true;
if (line.includes('}')) {
inBlock = false;
isTypeBlock = false;
}
continue;
}
if (/^export\s+\{/.test(line)) {
if (line.includes('}')) {
const m = line.match(/^export\s+\{([^}]*)\}/);
const inner = m?.[1];
if (inner !== undefined) {
for (const item of inner.split(',')) {
processExportItem(item, seen, exports);
}
}
} else {
inBlock = true;
isTypeBlock = false;
blockBuffer = line.replace(/^export\s+\{/, '').trim();
}
continue;
}
const declMatch = line.match(
/^export\s+(?:default\s+)?(?:function|class|const|let|var|abstract\s+class)\s+(\w+)/,
);
if (declMatch && declMatch[1] !== undefined) {
processExportItem(declMatch[1], seen, exports);
}
} else {
if (line.includes('}')) {
inBlock = false;
if (!isTypeBlock) {
const inner = `${blockBuffer},${line.replace(/\}.*/, '')}`;
for (const item of inner.split(',')) {
processExportItem(item, seen, exports);
}
}
blockBuffer = '';
isTypeBlock = false;
} else {
if (!isTypeBlock) {
blockBuffer += `,${line}`;
}
}
}
}
return exports.sort((a, b) => a.name.localeCompare(b.name));
}
export function sharedUiManifestPlugin(options?: { indexPath?: string }): Plugin {
let resolvedIndexPath = '';
let manifestSource = '';
function buildManifest(indexPath: string): string {
const allExports = parseExportsFromIndex(indexPath);
const json = JSON.stringify(allExports, null, 2);
return [
'// Auto-generated by sharedUiManifestPlugin — do not edit.',
`export const sharedUiExports = ${json};`,
].join('\n');
}
return {
name: 'shared-ui-manifest',
enforce: 'pre',
configResolved(config) {
resolvedIndexPath =
options?.indexPath ??
path.resolve(config.root, '../../lib/shared-ui/src/index.ts');
manifestSource = buildManifest(resolvedIndexPath);
},
resolveId(id): string | undefined {
if (id === VIRTUAL_ID) return RESOLVED_ID;
return undefined;
},
load(id): string | undefined {
if (id === RESOLVED_ID) return manifestSource;
return undefined;
},
handleHotUpdate({ file, server }) {
if (file === resolvedIndexPath) {
manifestSource = buildManifest(resolvedIndexPath);
const mod = server.moduleGraph.getModuleById(RESOLVED_ID);
if (mod) {
server.moduleGraph.invalidateModule(mod);
server.ws.send({ type: 'full-reload' });
}
}
},
};
}
|