UNPKG

@vue-vine/compiler

Version:
352 lines (350 loc) 11.9 kB
import MagicString from "magic-string"; import { ArrowFunctionExpression, CallExpression, CommentBlock, ExportNamedDeclaration, File, FunctionDeclaration, FunctionExpression, Identifier, Node, ObjectProperty, ReturnStatement, SourceLocation, TSType, TSTypeLiteral, TaggedTemplateExpression, traverse } from "@babel/types"; import { BindingTypes, CompilerOptions, ComponentNode, ConstantTypes, ElementNode, RootNode, SourceLocation as SourceLocation$1, TemplateChildNode } from "@vue/compiler-dom"; import { Project, TypeChecker } from "ts-morph"; import { ParseResult, ParserOptions } from "@babel/parser"; import { Result } from "postcss"; import { RawSourceMap } from "source-map-js"; //#region src/constants.d.ts /** This is derived from `@vue/compiler-core` */ declare const VineBindingTypes: { /** * declared as a prop */ readonly PROPS: BindingTypes.PROPS; /** * a local alias of a `<script setup>` destructured prop. * the original is stored in __propsAliases of the bindingMetadata object. */ readonly PROPS_ALIASED: BindingTypes.PROPS_ALIASED; /** * a let binding (may or may not be a ref) */ readonly SETUP_LET: BindingTypes.SETUP_LET; /** * a const binding that can never be a ref. * these bindings don't need `unref()` calls when processed in inlined * template expressions. */ readonly SETUP_CONST: BindingTypes.SETUP_CONST; /** * a const binding that does not need `unref()`, but may be mutated. */ readonly SETUP_REACTIVE_CONST: BindingTypes.SETUP_REACTIVE_CONST; /** * a const binding that may be a ref. */ readonly SETUP_MAYBE_REF: BindingTypes.SETUP_MAYBE_REF; /** * bindings that are guaranteed to be refs */ readonly SETUP_REF: BindingTypes.SETUP_REF; /** * a literal constant, e.g. 'foo', 1, true */ readonly LITERAL_CONST: BindingTypes.LITERAL_CONST; /** * a destructured prop */ readonly DESTRUCTURED_PROP: "destructured-prop"; }; //#endregion //#region src/types.d.ts type VineBabelRoot = ParseResult<File>; type MacrosInfoForVolar = { macroType: "vineProp"; macroCall: CallExpression; macroMeta: VinePropMeta; } | { macroType: "vineEmits"; macroCall: CallExpression; } | { macroType: "vineSlots"; macroCall: CallExpression; } | { macroType: "vineExpose"; macroCall: CallExpression; } | { macroType: "vineValidators"; macroCall: CallExpression; } | { macroType: "useTemplateRef"; macroCall: CallExpression; } | { macroType: "vineStyle"; macroCall: CallExpression; }; type VineProcessorLang = "scss" | "sass" | "less" | "stylus"; type VineStyleLang = "css" | "postcss" | VineProcessorLang; type VineTemplateBindings = Record<string, BindingTypes>; type BabelFunctionNodeTypes = FunctionDeclaration | FunctionExpression | ArrowFunctionExpression; type HMRCompFnsName = string | null; interface TsMorphCache { project: Project; typeChecker: TypeChecker; } interface VineCompilerHooks { getCompilerCtx: () => VineCompilerCtx; getTsMorph?: () => TsMorphCache; onError: (err: VineDiagnostic) => void; onWarn: (warn: VineDiagnostic) => void; onBindFileCtx?: (fileId: string, fileCtx: VineFileCtx) => void; onValidateEnd?: () => void; onAnalysisEnd?: () => void; onEnd?: () => void; } interface VineCompilerOptions { envMode?: string; inlineTemplate?: boolean; vueCompilerOptions?: CompilerOptions & { /** @internal */ __enableTransformAssetsURL?: boolean; /** @internal */ __enableTransformBareAttrAsBool?: false | ({ transformNegativeBool: false; } | { transformNegativeBool: true; constType?: ConstantTypes; }); /** @internal */ __shouldAddTemplateSuffix?: boolean; }; preprocessOptions?: Record<string, any>; postcssOptions?: any; postcssPlugins?: any[]; tsMorphOptions?: { disabled?: boolean; tsConfigPath?: string; }; } interface VineStyleMeta { lang: VineStyleLang; source: string; isExternalFilePathSource: boolean; range: [number, number] | undefined; scoped: boolean; fileCtx: VineFileCtx; compCtx: VineCompFnCtx; } interface VinePropMeta { typeAnnotationRaw?: string; isFromMacroDefine: boolean; isMaybeBool: boolean; isRequired: boolean; /** Source code node of given validator function */ validator?: Node; /** Source code node of given default value */ default?: Node; /** Declared identifier AST Node by vineProp */ macroDeclaredIdentifier?: Identifier; /** Whether the prop name needs to be quoted */ nameNeedQuoted?: boolean; jsDocComments?: CommentBlock[]; } interface VineCompilerCtx { isRunningHMR: boolean; fileCtxMap: Map<string, VineFileCtx>; vineCompileErrors: VineDiagnostic[]; vineCompileWarnings: VineDiagnostic[]; options: VineCompilerOptions; } interface VineUserImport { source: string; isType: boolean; isNamespace?: boolean; isDefault?: boolean; isUsedInTemplate?: (vineCompFn: VineCompFnCtx) => boolean; } interface VineFileCtx { readonly fileId: string; readonly root: ParseResult<File>; readonly originCode: string; readonly isCRLF: boolean; /** * Hot update only executes the * markup of the render function */ renderOnly: boolean; /** * The analysis result of the hot update module, * including the function name and scopeId of the component that needs to be updated */ hmrCompFnsName: HMRCompFnsName; fileMagicCode: MagicString; vineCompFns: VineCompFnCtx[]; userImports: Record<string, VineUserImport>; vueImportAliases: Record<string, string>; /** key: `scopeId` => value: `VineStyleMeta` */ styleDefine: Record<string, VineStyleMeta[]>; /** * We assume that all import statments are at top of this file, * record the end line of these imports * * To be noticed that the last import statement may take up multiple lines, * so we store the its location here. */ importsLastLine?: SourceLocation | null; /* Store all ExportNamedDeclaration */ exportNamedDeclarations: ExportNamedDeclaration[]; getAstNodeContent: (node: Node) => string; getLinkedTSTypeLiteralNodeContent: (node: TSTypeLiteral) => string; } interface VineQuery { type: string; scopeId: string; scoped: boolean; lang: string; index: number; vineFileId?: string; } interface VineDestructuredProp { node: ObjectProperty["key"]; isRest: boolean; alias?: string; default?: Node; } declare const VinePropsDefinitionBy: { readonly typeLiteral: 1; readonly typeReference: 2; readonly macro: 3; }; type VinePropsDefinitionSource = typeof VinePropsDefinitionBy[keyof typeof VinePropsDefinitionBy]; interface VineCompFnCtx { fileCtx: VineFileCtx; fnDeclNode: Node; fnItselfNode?: BabelFunctionNodeTypes; templateSource: string; templateReturn?: ReturnStatement; templateStringNode?: TaggedTemplateExpression; templateAst?: RootNode; templateParsedAst?: RootNode; templateComponentNames: Set<string>; templateRefNames: Set<string>; isExportDefault: boolean; isAsync: boolean; /** is web component (customElement) */ isCustomElement: boolean; fnName: string; scopeId: string; bindings: VineTemplateBindings; macrosInfoForVolar: MacrosInfoForVolar[]; propsAlias: string; props: Record<string, VinePropMeta>; propsDestructuredNames: Record<string, VineDestructuredProp>; propsDefinitionBy: VinePropsDefinitionSource; propsFormalParamType?: TSType; emitsAlias: string; emits: string[]; emitsTypeParam?: TSTypeLiteral; emitsDefinitionByNames?: boolean; /** Store the `defineExpose`'s argument in source code */ expose?: { macroCall: CallExpression; paramObj: Node; }; /** Store the `defineOptions`'s argument in source code */ options?: Node; /** Store every slot's props definition */ slots: Record<string, { props: TSTypeLiteral; }>; slotsNamesInTemplate: string[]; /** Store `vineModel` defines */ vineModels: Record<string, { varName: string; typeParameter?: TSType; modelModifiersName: string; modelOptions: Node | null; }>; slotsAlias: string; hoistSetupStmts: Node[]; cssBindings: Record<string, string | null>; externalStyleFilePaths: string[]; getPropsTypeRecordStr: (options?: { joinStr?: string; isNeedLinkedCodeTag?: boolean; isNeedJsDoc?: boolean; }) => string; } interface VineDiagnostic { full: string; msg: string; location: SourceLocation | null | undefined; vineCompFnCtx?: VineCompFnCtx; rawVueTemplateLocation?: SourceLocation$1 | null; } interface VineCompileCtx { compilerHooks: VineCompilerHooks; fileCtxCache?: VineFileCtx; babelParseOptions?: ParserOptions; } //#endregion //#region src/babel-helpers/ast.d.ts declare function findVineCompFnDecls(root: VineBabelRoot): Node[]; //#endregion //#region src/style/compile.d.ts declare function compileVineStyle(compilerCtx: VineCompilerCtx, params: { vineFileId: string; source: string; isScoped: boolean; scopeId: string; preprocessLang?: VineProcessorLang; inputSourceMap?: RawSourceMap; preprocessOptions?: Record<string, any>; postcssOptions?: any; postcssPlugins?: any[]; onErr?: (msg: string) => void; onWarn?: (msg: string) => void; }): Promise<{ code: string; map?: RawSourceMap; errors: Error[]; rawResult?: Result; dependencies: Set<string>; }>; //#endregion //#region src/template/walk.d.ts type WalkAction = (node: TemplateChildNode, parents: TemplateChildNode[], breakWalk: () => void) => void; interface WalkActions { enter?: WalkAction; leave?: WalkAction; } declare function walkVueTemplateAst(root: RootNode | undefined, walkActions: WalkActions): void; declare function isElementNode(node: TemplateChildNode): node is ElementNode; declare function isComponentNode(node: TemplateChildNode): node is ComponentNode; //#endregion //#region src/ts-morph/create.d.ts interface CreateTsMorphOptions { fileId?: string; tsConfigPath?: string; } declare function createTsMorph(options: CreateTsMorphOptions): TsMorphCache; //#endregion //#region src/utils/index.d.ts declare class ExitTraverseError extends Error { constructor(); } declare const exitTraverse: ExitTraverseError; declare const _breakableTraverse: typeof traverse; //#endregion //#region src/utils/topo-sort.d.ts type ComponentRelationsMap = Record<string, Set<string>>; declare function topoSort(relationsMap: Record<string, Set<string>>): string[] | null; //#endregion //#region src/index.d.ts declare function createCompilerCtx(options?: VineCompilerOptions): VineCompilerCtx; declare function createVineFileCtx(code: string, fileId: string, vineCompileCtx: VineCompileCtx): VineFileCtx; declare function doValidateVine(vineCompilerHooks: VineCompilerHooks, vineFileCtx: VineFileCtx, vineCompFnDecls: Node[]): void; declare function doAnalyzeVine(vineCompilerHooks: VineCompilerHooks, vineFileCtx: VineFileCtx, vineCompFnDecls: Node[]): void; declare function compileVineTypeScriptFile(code: string, fileId: string, vineCompileCtx: VineCompileCtx, ssr?: boolean): VineFileCtx; /** * Analyze .vine.ts for language service. * * @param code - The `.vine.ts` source code. * @param fileId - The file id. * @param vineCompileCtx - The vine compile context. * @returns Vue Vine file context. */ declare function analyzeVineTypeScriptFile(code: string, fileId: string, vineCompileCtx: VineCompileCtx): VineFileCtx; //#endregion export { ComponentRelationsMap, HMRCompFnsName, VineBindingTypes, VineCompileCtx, VineCompilerCtx, VineCompilerHooks, VineCompilerOptions, VineDiagnostic, VineFileCtx, VineCompFnCtx as VineFnCompCtx, VineProcessorLang, VinePropMeta, VinePropsDefinitionBy, VineQuery, _breakableTraverse, analyzeVineTypeScriptFile, compileVineStyle, compileVineTypeScriptFile, createCompilerCtx, createTsMorph, createVineFileCtx, doAnalyzeVine, doValidateVine, exitTraverse, findVineCompFnDecls, isComponentNode, isElementNode, topoSort, walkVueTemplateAst };