Skip subclass-wrapper pipeline when no subclasses

Avoid the expensive TypeChecker/program startup for files that don't declare extending classes by performing a cheap top-level scan first. index.ts now imports fileHasExtendingClass and a SubclassWrapperPipelineResult type and conditionally calls runSubclassWrapperPipeline only when a candidate is found, otherwise returning an empty result. Also updated comments to explain the optimization. subclass-wrapper.ts now imports isStaticMethod from ast-helpers.js (small import cleanup/usage update).
This commit is contained in:
手瓜一十雪 committed 2026-05-25 11:30:41 +08:00
1 parent 34cc6be8df
commit bed48bbe46
2 files changed
+15 -8

No files matched your search

+14 -7
View File
@@ -1,9 +1,14 @@
import type { Plugin } from 'vite';
import { analyze, analyzeSource, selectUsedRegistry, typeNodeToMangledName } from './ast/analyzer.js';
import { fileHasExtendingClass } from './ast/ast-helpers.js';
import { resolveImports, type ParsedFileEntry } from './ast/import-resolver.js';
import { generateCode } from './codegen/generator.js';
import { applyReplacements, replaceCallSites } from './transform/replacer.js';
import { applyOverrideInsertions, runSubclassWrapperPipeline } from './typecheck/file-pipeline.js';
import {
applyOverrideInsertions,
runSubclassWrapperPipeline,
type SubclassWrapperPipelineResult,
} from './typecheck/file-pipeline.js';
import { invalidateProgramCache } from './typecheck/program-cache.js';
export type ProtobufVitePluginOptions = Record<string, never>;
@@ -21,12 +26,14 @@ export default function protobufVitePlugin(_options: ProtobufVitePluginOptions =
const imported = resolveImports(code, cleanId, fileCache);
const { registry, callSites, sourceFile } = analyze(code, cleanId, imported);
// Subclass-wrapper pipeline lives outside the AST analyzer because it
// needs a TypeChecker. It returns extra root type names that must
// participate in `selectUsedRegistry` so the codecs they reference
// actually get generated, plus the per-subclass override text we
// append after the user code.
const subclassWrappers = runSubclassWrapperPipeline(cleanId);
// The subclass-wrapper pipeline needs a TypeChecker, which carries a
// 1–2 second startup cost the first time we touch a tsconfig.
// Almost every file in a typical project has zero extending classes,
// so guard the heavy work behind a cheap top-level scan and only
// build the Program when there's actually a candidate to resolve.
const subclassWrappers: SubclassWrapperPipelineResult = fileHasExtendingClass(sourceFile)
? runSubclassWrapperPipeline(cleanId)
: { resolvedWrappers: [], extraRootTypeNames: [], insertions: [] };
if (
registry.size === 0 &&
@@ -1,5 +1,5 @@
import ts from 'typescript';
import { getPropertyNameText, isBareTypeRef } from '../ast/ast-helpers.js';
import { getPropertyNameText, isBareTypeRef, isStaticMethod } from '../ast/ast-helpers.js';
import type { ClassWrapperInfo, WrapperCodecCall, WrapperMethodInfo } from '../ast/static-wrapper.js';
import { detectClassWrappers } from '../ast/static-wrapper.js';
import {