From bed48bbe4648061dbf02239722357a7e60f78f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 25 May 2026 11:30:41 +0800 Subject: [PATCH] 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). --- packages/proton/src/index.ts | 21 ++++++++++++------- .../proton/src/typecheck/subclass-wrapper.ts | 2 +- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/proton/src/index.ts b/packages/proton/src/index.ts index 0c9023d9..ee950538 100644 --- a/packages/proton/src/index.ts +++ b/packages/proton/src/index.ts @@ -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; @@ -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 && diff --git a/packages/proton/src/typecheck/subclass-wrapper.ts b/packages/proton/src/typecheck/subclass-wrapper.ts index b4f6e127..5def7e19 100644 --- a/packages/proton/src/typecheck/subclass-wrapper.ts +++ b/packages/proton/src/typecheck/subclass-wrapper.ts @@ -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 {