UNPKG

16.1 kBTypeScriptView Raw
1import * as _babel_types from '@babel/types';
2import { Statement, Expression, TSType, Program, Node, ObjectPattern, TSModuleDeclaration, TSPropertySignature, TSMethodSignature, TSCallSignatureDeclaration, TSFunctionType } from '@babel/types';
3import { CompilerOptions, CodegenResult, ParserOptions, RootNode, CompilerError, SourceLocation, ElementNode, BindingMetadata as BindingMetadata$1 } from '@vue/compiler-core';
4export { BindingMetadata, CompilerError, CompilerOptions, extractIdentifiers, generateCodeFrame, isInDestructureAssignment, isStaticProperty, walkIdentifiers } from '@vue/compiler-core';
5import { RawSourceMap } from 'source-map-js';
6import { ParserPlugin } from '@babel/parser';
7export { parse as babelParse } from '@babel/parser';
8import { Result, LazyResult } from 'postcss';
9import MagicString from 'magic-string';
10export { default as MagicString } from 'magic-string';
11export { shouldTransform as shouldTransformRef, transform as transformRef, transformAST as transformRefAST } from '@vue/reactivity-transform';
12
13export interface AssetURLTagConfig {
14 [name: string]: string[];
15}
16export interface AssetURLOptions {
17 /**
18 * If base is provided, instead of transforming relative asset urls into
19 * imports, they will be directly rewritten to absolute urls.
20 */
21 base?: string | null;
22 /**
23 * If true, also processes absolute urls.
24 */
25 includeAbsolute?: boolean;
26 tags?: AssetURLTagConfig;
27}
28
29export interface TemplateCompiler {
30 compile(template: string, options: CompilerOptions): CodegenResult;
31 parse(template: string, options: ParserOptions): RootNode;
32}
33export interface SFCTemplateCompileResults {
34 code: string;
35 ast?: RootNode;
36 preamble?: string;
37 source: string;
38 tips: string[];
39 errors: (string | CompilerError)[];
40 map?: RawSourceMap;
41}
42export interface SFCTemplateCompileOptions {
43 source: string;
44 filename: string;
45 id: string;
46 scoped?: boolean;
47 slotted?: boolean;
48 isProd?: boolean;
49 ssr?: boolean;
50 ssrCssVars?: string[];
51 inMap?: RawSourceMap;
52 compiler?: TemplateCompiler;
53 compilerOptions?: CompilerOptions;
54 preprocessLang?: string;
55 preprocessOptions?: any;
56 /**
57 * In some cases, compiler-sfc may not be inside the project root (e.g. when
58 * linked or globally installed). In such cases a custom `require` can be
59 * passed to correctly resolve the preprocessors.
60 */
61 preprocessCustomRequire?: (id: string) => any;
62 /**
63 * Configure what tags/attributes to transform into asset url imports,
64 * or disable the transform altogether with `false`.
65 */
66 transformAssetUrls?: AssetURLOptions | AssetURLTagConfig | boolean;
67}
68export declare function compileTemplate(options: SFCTemplateCompileOptions): SFCTemplateCompileResults;
69
70export interface SFCScriptCompileOptions {
71 /**
72 * Scope ID for prefixing injected CSS variables.
73 * This must be consistent with the `id` passed to `compileStyle`.
74 */
75 id: string;
76 /**
77 * Production mode. Used to determine whether to generate hashed CSS variables
78 */
79 isProd?: boolean;
80 /**
81 * Enable/disable source map. Defaults to true.
82 */
83 sourceMap?: boolean;
84 /**
85 * https://babeljs.io/docs/en/babel-parser#plugins
86 */
87 babelParserPlugins?: ParserPlugin[];
88 /**
89 * A list of files to parse for global types to be made available for type
90 * resolving in SFC macros. The list must be fully resolved file system paths.
91 */
92 globalTypeFiles?: string[];
93 /**
94 * Compile the template and inline the resulting render function
95 * directly inside setup().
96 * - Only affects `<script setup>`
97 * - This should only be used in production because it prevents the template
98 * from being hot-reloaded separately from component state.
99 */
100 inlineTemplate?: boolean;
101 /**
102 * Generate the final component as a variable instead of default export.
103 * This is useful in e.g. @vitejs/plugin-vue where the script needs to be
104 * placed inside the main module.
105 */
106 genDefaultAs?: string;
107 /**
108 * Options for template compilation when inlining. Note these are options that
109 * would normally be passed to `compiler-sfc`'s own `compileTemplate()`, not
110 * options passed to `compiler-dom`.
111 */
112 templateOptions?: Partial<SFCTemplateCompileOptions>;
113 /**
114 * Hoist <script setup> static constants.
115 * - Only enables when one `<script setup>` exists.
116 * @default true
117 */
118 hoistStatic?: boolean;
119 /**
120 * (**Experimental**) Enable macro `defineModel`
121 * @default false
122 */
123 defineModel?: boolean;
124 /**
125 * (**Experimental**) Enable reactive destructure for `defineProps`
126 * @default false
127 */
128 propsDestructure?: boolean;
129 /**
130 * File system access methods to be used when resolving types
131 * imported in SFC macros. Defaults to ts.sys in Node.js, can be overwritten
132 * to use a virtual file system for use in browsers (e.g. in REPLs)
133 */
134 fs?: {
135 fileExists(file: string): boolean;
136 readFile(file: string): string | undefined;
137 };
138 /**
139 * (Experimental) Enable syntax transform for using refs without `.value` and
140 * using destructured props with reactivity
141 * @deprecated the Reactivity Transform proposal has been dropped. This
142 * feature will be removed from Vue core in 3.4. If you intend to continue
143 * using it, disable this and switch to the [Vue Macros implementation](https://vue-macros.sxzz.moe/features/reactivity-transform.html).
144 */
145 reactivityTransform?: boolean;
146}
147interface ImportBinding {
148 isType: boolean;
149 imported: string;
150 local: string;
151 source: string;
152 isFromSetup: boolean;
153 isUsedInTemplate: boolean;
154}
155/**
156 * Compile `<script setup>`
157 * It requires the whole SFC descriptor because we need to handle and merge
158 * normal `<script>` + `<script setup>` if both are present.
159 */
160export declare function compileScript(sfc: SFCDescriptor, options: SFCScriptCompileOptions): SFCScriptBlock;
161
162export interface SFCParseOptions {
163 filename?: string;
164 sourceMap?: boolean;
165 sourceRoot?: string;
166 pad?: boolean | 'line' | 'space';
167 ignoreEmpty?: boolean;
168 compiler?: TemplateCompiler;
169}
170export interface SFCBlock {
171 type: string;
172 content: string;
173 attrs: Record<string, string | true>;
174 loc: SourceLocation;
175 map?: RawSourceMap;
176 lang?: string;
177 src?: string;
178}
179export interface SFCTemplateBlock extends SFCBlock {
180 type: 'template';
181 ast: ElementNode;
182}
183export interface SFCScriptBlock extends SFCBlock {
184 type: 'script';
185 setup?: string | boolean;
186 bindings?: BindingMetadata$1;
187 imports?: Record<string, ImportBinding>;
188 scriptAst?: _babel_types.Statement[];
189 scriptSetupAst?: _babel_types.Statement[];
190 warnings?: string[];
191 /**
192 * Fully resolved dependency file paths (unix slashes) with imported types
193 * used in macros, used for HMR cache busting in @vitejs/plugin-vue and
194 * vue-loader.
195 */
196 deps?: string[];
197}
198export interface SFCStyleBlock extends SFCBlock {
199 type: 'style';
200 scoped?: boolean;
201 module?: string | boolean;
202}
203export interface SFCDescriptor {
204 filename: string;
205 source: string;
206 template: SFCTemplateBlock | null;
207 script: SFCScriptBlock | null;
208 scriptSetup: SFCScriptBlock | null;
209 styles: SFCStyleBlock[];
210 customBlocks: SFCBlock[];
211 cssVars: string[];
212 /**
213 * whether the SFC uses :slotted() modifier.
214 * this is used as a compiler optimization hint.
215 */
216 slotted: boolean;
217 /**
218 * compare with an existing descriptor to determine whether HMR should perform
219 * a reload vs. re-render.
220 *
221 * Note: this comparison assumes the prev/next script are already identical,
222 * and only checks the special case where <script setup lang="ts"> unused import
223 * pruning result changes due to template changes.
224 */
225 shouldForceReload: (prevImports: Record<string, ImportBinding>) => boolean;
226}
227export interface SFCParseResult {
228 descriptor: SFCDescriptor;
229 errors: (CompilerError | SyntaxError)[];
230}
231export declare const parseCache: Map<string, SFCParseResult> & {
232 max?: number | undefined;
233};
234export declare function parse(source: string, { sourceMap, filename, sourceRoot, pad, ignoreEmpty, compiler }?: SFCParseOptions): SFCParseResult;
235
236type PreprocessLang = 'less' | 'sass' | 'scss' | 'styl' | 'stylus';
237
238export interface SFCStyleCompileOptions {
239 source: string;
240 filename: string;
241 id: string;
242 scoped?: boolean;
243 trim?: boolean;
244 isProd?: boolean;
245 inMap?: RawSourceMap;
246 preprocessLang?: PreprocessLang;
247 preprocessOptions?: any;
248 preprocessCustomRequire?: (id: string) => any;
249 postcssOptions?: any;
250 postcssPlugins?: any[];
251 /**
252 * @deprecated use `inMap` instead.
253 */
254 map?: RawSourceMap;
255}
256/**
257 * Aligns with postcss-modules
258 * https://github.com/css-modules/postcss-modules
259 */
260interface CSSModulesOptions {
261 scopeBehaviour?: 'global' | 'local';
262 generateScopedName?: string | ((name: string, filename: string, css: string) => string);
263 hashPrefix?: string;
264 localsConvention?: 'camelCase' | 'camelCaseOnly' | 'dashes' | 'dashesOnly';
265 exportGlobals?: boolean;
266 globalModulePaths?: RegExp[];
267}
268export interface SFCAsyncStyleCompileOptions extends SFCStyleCompileOptions {
269 isAsync?: boolean;
270 modules?: boolean;
271 modulesOptions?: CSSModulesOptions;
272}
273export interface SFCStyleCompileResults {
274 code: string;
275 map: RawSourceMap | undefined;
276 rawResult: Result | LazyResult | undefined;
277 errors: Error[];
278 modules?: Record<string, string>;
279 dependencies: Set<string>;
280}
281export declare function compileStyle(options: SFCStyleCompileOptions): SFCStyleCompileResults;
282export declare function compileStyleAsync(options: SFCAsyncStyleCompileOptions): Promise<SFCStyleCompileResults>;
283
284export declare function rewriteDefault(input: string, as: string, parserPlugins?: ParserPlugin[]): string;
285/**
286 * Utility for rewriting `export default` in a script block into a variable
287 * declaration so that we can inject things into it
288 */
289export declare function rewriteDefaultAST(ast: Statement[], s: MagicString, as: string): void;
290
291type PropsDestructureBindings = Record<string, // public prop key
292{
293 local: string;
294 default?: Expression;
295}>;
296
297interface ModelDecl {
298 type: TSType | undefined;
299 options: string | undefined;
300 identifier: string | undefined;
301}
302
303declare const enum BindingTypes {
304 /**
305 * returned from data()
306 */
307 DATA = "data",
308 /**
309 * declared as a prop
310 */
311 PROPS = "props",
312 /**
313 * a local alias of a `<script setup>` destructured prop.
314 * the original is stored in __propsAliases of the bindingMetadata object.
315 */
316 PROPS_ALIASED = "props-aliased",
317 /**
318 * a let binding (may or may not be a ref)
319 */
320 SETUP_LET = "setup-let",
321 /**
322 * a const binding that can never be a ref.
323 * these bindings don't need `unref()` calls when processed in inlined
324 * template expressions.
325 */
326 SETUP_CONST = "setup-const",
327 /**
328 * a const binding that does not need `unref()`, but may be mutated.
329 */
330 SETUP_REACTIVE_CONST = "setup-reactive-const",
331 /**
332 * a const binding that may be a ref.
333 */
334 SETUP_MAYBE_REF = "setup-maybe-ref",
335 /**
336 * bindings that are guaranteed to be refs
337 */
338 SETUP_REF = "setup-ref",
339 /**
340 * declared by other options, e.g. computed, inject
341 */
342 OPTIONS = "options",
343 /**
344 * a literal constant, e.g. 'foo', 1, true
345 */
346 LITERAL_CONST = "literal-const"
347}
348type BindingMetadata = {
349 [key: string]: BindingTypes | undefined;
350} & {
351 __isScriptSetup?: boolean;
352 __propsAliases?: Record<string, string>;
353};
354
355export declare class ScriptCompileContext {
356 descriptor: SFCDescriptor;
357 options: Partial<SFCScriptCompileOptions>;
358 isJS: boolean;
359 isTS: boolean;
360 scriptAst: Program | null;
361 scriptSetupAst: Program | null;
362 source: string;
363 filename: string;
364 s: MagicString;
365 startOffset: number | undefined;
366 endOffset: number | undefined;
367 scope?: TypeScope;
368 globalScopes?: TypeScope[];
369 userImports: Record<string, ImportBinding>;
370 hasDefinePropsCall: boolean;
371 hasDefineEmitCall: boolean;
372 hasDefineExposeCall: boolean;
373 hasDefaultExportName: boolean;
374 hasDefaultExportRender: boolean;
375 hasDefineOptionsCall: boolean;
376 hasDefineSlotsCall: boolean;
377 hasDefineModelCall: boolean;
378 propsIdentifier: string | undefined;
379 propsRuntimeDecl: Node | undefined;
380 propsTypeDecl: Node | undefined;
381 propsDestructureDecl: ObjectPattern | undefined;
382 propsDestructuredBindings: PropsDestructureBindings;
383 propsDestructureRestId: string | undefined;
384 propsRuntimeDefaults: Node | undefined;
385 emitsRuntimeDecl: Node | undefined;
386 emitsTypeDecl: Node | undefined;
387 emitIdentifier: string | undefined;
388 modelDecls: Record<string, ModelDecl>;
389 optionsRuntimeDecl: Node | undefined;
390 bindingMetadata: BindingMetadata;
391 helperImports: Set<string>;
392 helper(key: string): string;
393 /**
394 * to be exposed on compiled script block for HMR cache busting
395 */
396 deps?: Set<string>;
397 /**
398 * cache for resolved fs
399 */
400 fs?: NonNullable<SFCScriptCompileOptions['fs']>;
401 constructor(descriptor: SFCDescriptor, options: Partial<SFCScriptCompileOptions>);
402 getString(node: Node, scriptSetup?: boolean): string;
403 error(msg: string, node: Node, scope?: TypeScope): never;
404}
405
406/**
407 * TypeResolveContext is compatible with ScriptCompileContext
408 * but also allows a simpler version of it with minimal required properties
409 * when resolveType needs to be used in a non-SFC context, e.g. in a babel
410 * plugin. The simplest context can be just:
411 * ```ts
412 * const ctx: SimpleTypeResolveContext = {
413 * filename: '...',
414 * source: '...',
415 * options: {},
416 * error() {},
417 * ast: []
418 * }
419 * ```
420 */
421export type SimpleTypeResolveContext = Pick<ScriptCompileContext, 'source' | 'filename' | 'error' | 'options'> & Partial<Pick<ScriptCompileContext, 'scope' | 'globalScopes' | 'deps' | 'fs'>> & {
422 ast: Statement[];
423};
424export type TypeResolveContext = ScriptCompileContext | SimpleTypeResolveContext;
425type Import = Pick<ImportBinding, 'source' | 'imported'>;
426interface WithScope {
427 _ownerScope: TypeScope;
428}
429type ScopeTypeNode = Node & WithScope & {
430 _ns?: TSModuleDeclaration & WithScope;
431};
432declare class TypeScope {
433 filename: string;
434 source: string;
435 offset: number;
436 imports: Record<string, Import>;
437 types: Record<string, ScopeTypeNode>;
438 declares: Record<string, ScopeTypeNode>;
439 constructor(filename: string, source: string, offset?: number, imports?: Record<string, Import>, types?: Record<string, ScopeTypeNode>, declares?: Record<string, ScopeTypeNode>);
440 resolvedImportSources: Record<string, string>;
441 exportedTypes: Record<string, ScopeTypeNode>;
442 exportedDeclares: Record<string, ScopeTypeNode>;
443}
444interface MaybeWithScope {
445 _ownerScope?: TypeScope;
446}
447interface ResolvedElements {
448 props: Record<string, (TSPropertySignature | TSMethodSignature) & {
449 _ownerScope: TypeScope;
450 }>;
451 calls?: (TSCallSignatureDeclaration | TSFunctionType)[];
452}
453/**
454 * Resolve arbitrary type node to a list of type elements that can be then
455 * mapped to runtime props or emits.
456 */
457export declare function resolveTypeElements(ctx: TypeResolveContext, node: Node & MaybeWithScope & {
458 _resolvedElements?: ResolvedElements;
459}, scope?: TypeScope): ResolvedElements;
460/**
461 * @private
462 */
463export declare function registerTS(_ts: any): void;
464/**
465 * @private
466 */
467export declare function invalidateTypeCache(filename: string): void;
468export declare function inferRuntimeType(ctx: TypeResolveContext, node: Node & MaybeWithScope, scope?: TypeScope): string[];
469
470export declare const version: string;
471
472export declare const walk: any;
473
474
\No newline at end of file