Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compiler-sfc.d.ts 17KB

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