options.d.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { BindingMetadata, CompilerError, RootNode } from '@vue/compiler-core';
  2. import type { RawSourceMap } from 'source-map-js';
  3. import { DirectiveTransform, NodeTransform } from './transform';
  4. interface SharedTransformCodegenOptions {
  5. /**
  6. * @default 'default'
  7. */
  8. mode?: 'default' | 'module';
  9. rootDir?: string;
  10. targetLanguage?: 'kotlin' | 'swift';
  11. /**
  12. * Transform expressions like {{ foo }} to `_ctx.foo`.
  13. * @default false
  14. */
  15. prefixIdentifiers?: boolean;
  16. /**
  17. * Optional binding metadata analyzed from script - used to optimize
  18. * binding access when `prefixIdentifiers` is enabled.
  19. */
  20. bindingMetadata?: BindingMetadata;
  21. /**
  22. * Compile the function for inlining inside setup().
  23. * This allows the function to directly access setup() local bindings.
  24. */
  25. inline?: boolean;
  26. /**
  27. * Filename for source map generation.
  28. * Also used for self-recursive reference in templates
  29. * @default ''
  30. */
  31. filename?: string;
  32. /**
  33. * 编译的模板类名
  34. */
  35. className?: string;
  36. }
  37. export interface CodegenOptions extends SharedTransformCodegenOptions {
  38. inMap?: RawSourceMap;
  39. /**
  40. * Generate source map?
  41. * @default false
  42. */
  43. sourceMap?: boolean;
  44. /**
  45. * 匹配 easycom 组件
  46. * @param tag
  47. */
  48. matchEasyCom?: (tag: string, uts: boolean) => string | false | undefined | void;
  49. /**
  50. * 解析 uts component 组件
  51. * @param name
  52. * @param type
  53. */
  54. parseUTSComponent?: (name: string, type: 'kotlin' | 'swift') => {
  55. className: string;
  56. namespace: string;
  57. source: string;
  58. } | undefined | void;
  59. /**
  60. * template的offset
  61. */
  62. originalLineOffset?: number;
  63. /**
  64. * script的offset
  65. */
  66. generatedLineOffset?: number;
  67. }
  68. export interface ErrorHandlingOptions {
  69. onWarn?: (warning: CompilerError) => void;
  70. onError?: (error: CompilerError) => void;
  71. }
  72. export interface TransformOptions extends SharedTransformCodegenOptions, ErrorHandlingOptions {
  73. rootDir?: string;
  74. /**
  75. * Cache v-on handlers to avoid creating new inline functions on each render,
  76. * also avoids the need for dynamically patching the handlers by wrapping it.
  77. * e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
  78. * option it's compiled to:
  79. * ```js
  80. * { onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }
  81. * ```
  82. * - Requires "prefixIdentifiers" to be enabled because it relies on scope
  83. * analysis to determine if a handler is safe to cache.
  84. * @default false
  85. */
  86. cacheHandlers?: boolean;
  87. /**
  88. * An array of node transforms to be applied to every AST node.
  89. */
  90. nodeTransforms?: NodeTransform[];
  91. /**
  92. * An object of { name: transform } to be applied to every directive attribute
  93. * node found on element nodes.
  94. */
  95. directiveTransforms?: Record<string, DirectiveTransform | undefined>;
  96. /**
  97. * If the pairing runtime provides additional built-in elements, use this to
  98. * mark them as built-in so the compiler will generate component vnodes
  99. * for them.
  100. */
  101. isBuiltInComponent?: (tag: string) => symbol | void;
  102. /**
  103. * Used by some transforms that expects only native elements
  104. */
  105. isCustomElement?: (tag: string) => boolean | void;
  106. /**
  107. * SFC scoped styles ID
  108. */
  109. scopeId?: string | null;
  110. /**
  111. * Indicates this SFC template has used :slotted in its styles
  112. * Defaults to `true` for backwards compatibility - SFC tooling should set it
  113. * to `false` if no `:slotted` usage is detected in `<style>`
  114. */
  115. slotted?: boolean;
  116. }
  117. export type TemplateCompilerOptions = {
  118. /**
  119. * e.g. platform native elements, e.g. `<div>` for browsers
  120. */
  121. isNativeTag?: (tag: string) => boolean;
  122. } & TransformOptions & CodegenOptions;
  123. export interface CodegenResult {
  124. ast?: RootNode;
  125. code: string;
  126. preamble?: string;
  127. easyComponentAutoImports: Record<string, [string, string]>;
  128. importEasyComponents: string[];
  129. importUTSComponents: string[];
  130. imports: string[];
  131. elements: string[];
  132. map?: RawSourceMap;
  133. }
  134. export {};