feat(proton): add @snowluma/proton — compile-time protobuf DSL
Vendors SnowLuma/protobuf-dsl into the monorepo as @snowluma/proton, with
edge-case fixes applied on top of the upstream source. Reference copy of
the unmodified upstream is kept under dev/protobuf-dsl/.
Edge cases fixed (each with a regression test):
- Aliased pb / pb_repeated marker imports (`import { pb as P }`)
— collector now resolves the marker name through the import resolver
- Concrete interface with a generic-instantiated message field
(`wrapped: pb<5, Wrapper<uint_32>>`) — field typeName now resolves
to the mangled form, and the inner instantiation is queued for
monomorphization
- Generic template with a generic-instantiated field
(`interface Outer<U> { x: pb<5, Wrapper<U>> }`) — the inner
instantiation is now re-substituted and re-monomorphized at outer
instantiation time
- Wrapper-binding synthetic SourceFile was empty-text — getText()
returned garbage, breaking mangled name computation downstream
- Cross-file forwarded wrappers (chain → imported base) were not
detected — parseFileForDefinitions now resolves imports recursively
(value imports only, with cycle protection)
- matchForwardedKnownWrapper did not propagate typePattern — chain
treated <X> as the encoded type instead of Wrapper<X>
- Synthetic-SF WeakMap was private to analyzer.ts — replacer could
not see it. Moved to utils.ts so typeNodeToMangledName picks it up
automatically.
Defensive guard: any field whose typeName resolves to neither a primitive
nor a known message now throws at analyze() time, instead of silently
leaking the WireType.Varint placeholder into codegen.
95/95 tests pass; tsc --noEmit clean.
This commit is contained in:
62 files changed
+4714
No files matched your search
@@ -0,0 +1,113 @@
|
||||
import type { Plugin } from 'vite';
|
||||
import { analyze, analyzeSource, selectUsedRegistry, typeNodeToMangledName } from './ast/analyzer.js';
|
||||
import { generateCode } from './codegen/generator.js';
|
||||
import { applyReplacements, replaceCallSites } from './transform/replacer.js';
|
||||
import type { MessageRegistry } from './ast/types.js';
|
||||
import { resolveImports, type ParsedFileEntry } from './ast/import-resolver.js';
|
||||
import { createRuntimeMap, type RuntimeMapCallSite } from './runtime-map.js';
|
||||
|
||||
export interface ProtobufRuntimeMapPluginOptions {
|
||||
enabled?: boolean;
|
||||
fileName?: string;
|
||||
}
|
||||
|
||||
export interface ProtobufVitePluginOptions {
|
||||
runtimeMap?: ProtobufRuntimeMapPluginOptions;
|
||||
}
|
||||
|
||||
interface RuntimeMapFileEntry {
|
||||
messages: MessageRegistry;
|
||||
callSites: RuntimeMapCallSite[];
|
||||
}
|
||||
|
||||
export default function protobufVitePlugin(options: ProtobufVitePluginOptions = {}): Plugin {
|
||||
const fileCache = new Map<string, ParsedFileEntry>();
|
||||
const runtimeMapByFile = new Map<string, RuntimeMapFileEntry>();
|
||||
const runtimeMapEnabled = options.runtimeMap?.enabled === true;
|
||||
const runtimeMapFileName = options.runtimeMap?.fileName ?? 'snowluma-proton.runtime-map.json';
|
||||
|
||||
return {
|
||||
name: 'vite-plugin-protobuf',
|
||||
enforce: 'pre',
|
||||
|
||||
transform(code, id) {
|
||||
const cleanId = id.split('?')[0];
|
||||
if (!cleanId.endsWith('.ts') || cleanId.endsWith('.d.ts')) return null;
|
||||
|
||||
const imported = resolveImports(code, cleanId, fileCache);
|
||||
const { registry, callSites, sourceFile } = analyze(code, cleanId, imported);
|
||||
if (registry.size === 0 && callSites.length === 0) return null;
|
||||
|
||||
const used = selectUsedRegistry(registry, callSites, sourceFile);
|
||||
if (used.registry.size === 0) {
|
||||
runtimeMapByFile.delete(cleanId);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (runtimeMapEnabled) {
|
||||
runtimeMapByFile.set(cleanId, {
|
||||
messages: used.registry,
|
||||
callSites: used.callSites.map(cs => ({
|
||||
file: cleanId,
|
||||
line: cs.line,
|
||||
column: cs.column,
|
||||
fnName: cs.fnName as RuntimeMapCallSite['fnName'],
|
||||
typeName: cs.typeName,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
const generatedCode = generateCode(used.registry);
|
||||
const { transformedCode, hasReplacements } = applyReplacements(code, sourceFile, callSites, used.registry);
|
||||
if (!hasReplacements && generatedCode === '') return null;
|
||||
|
||||
return { code: generatedCode + '\n' + transformedCode, map: null };
|
||||
},
|
||||
|
||||
handleHotUpdate({ file }) {
|
||||
if (file.endsWith('.ts')) {
|
||||
fileCache.delete(file);
|
||||
runtimeMapByFile.delete(file);
|
||||
}
|
||||
},
|
||||
|
||||
generateBundle() {
|
||||
if (!runtimeMapEnabled) return;
|
||||
|
||||
const mergedMessages: MessageRegistry = new Map();
|
||||
const mergedCallSites: RuntimeMapCallSite[] = [];
|
||||
|
||||
for (const entry of runtimeMapByFile.values()) {
|
||||
for (const [name, msg] of entry.messages) mergedMessages.set(name, msg);
|
||||
mergedCallSites.push(...entry.callSites);
|
||||
}
|
||||
|
||||
if (mergedMessages.size === 0 || mergedCallSites.length === 0) return;
|
||||
|
||||
const runtimeMap = createRuntimeMap({
|
||||
messages: mergedMessages,
|
||||
callSites: mergedCallSites,
|
||||
});
|
||||
|
||||
this.emitFile({
|
||||
type: 'asset',
|
||||
fileName: runtimeMapFileName,
|
||||
source: JSON.stringify(runtimeMap, null, 2),
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export {
|
||||
analyze,
|
||||
analyzeSource,
|
||||
generateCode,
|
||||
replaceCallSites,
|
||||
applyReplacements,
|
||||
typeNodeToMangledName,
|
||||
resolveImports,
|
||||
selectUsedRegistry,
|
||||
createRuntimeMap,
|
||||
};
|
||||
|
||||
export type { ProtobufRuntimeMap, RuntimeMapCallSite } from './runtime-map.js';
|
||||
Reference in new issue
Block a user