7ce4f27812dfca15467db7348da7f7288122222018bbab38431e63cc0bbdb36d552a21547e1f3b0f4719c5443c112b37c68d64decb2dce54ea3210ece78c1b 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import MagicString from 'magic-string';
  2. declare const builtinPresets: {
  3. '@vue/composition-api': InlinePreset;
  4. '@vueuse/core': () => Preset;
  5. '@vueuse/head': InlinePreset;
  6. pinia: InlinePreset;
  7. preact: InlinePreset;
  8. quasar: InlinePreset;
  9. react: InlinePreset;
  10. 'react-router': InlinePreset;
  11. 'react-router-dom': InlinePreset;
  12. svelte: InlinePreset;
  13. 'svelte/animate': InlinePreset;
  14. 'svelte/easing': InlinePreset;
  15. 'svelte/motion': InlinePreset;
  16. 'svelte/store': InlinePreset;
  17. 'svelte/transition': InlinePreset;
  18. 'vee-validate': InlinePreset;
  19. vitepress: InlinePreset;
  20. 'vue-demi': InlinePreset;
  21. 'vue-i18n': InlinePreset;
  22. 'vue-router': InlinePreset;
  23. vue: InlinePreset;
  24. 'vue/macros': InlinePreset;
  25. vuex: InlinePreset;
  26. vitest: InlinePreset;
  27. 'uni-app': InlinePreset;
  28. 'solid-js': InlinePreset;
  29. 'solid-app-router': InlinePreset;
  30. };
  31. type BuiltinPresetName = keyof typeof builtinPresets;
  32. type ModuleId = string;
  33. type ImportName = string;
  34. interface ImportCommon {
  35. /** Module specifier to import from */
  36. from: ModuleId;
  37. /**
  38. * Priority of the import, if multiple imports have the same name, the one with the highest priority will be used
  39. * @default 1
  40. */
  41. priority?: number;
  42. /** If this import is disabled */
  43. disabled?: boolean;
  44. /**
  45. * Metadata of the import
  46. */
  47. meta?: {
  48. /** Short description of the import */
  49. description?: string;
  50. /** URL to the documentation */
  51. docsUrl?: string;
  52. /** Additional metadata */
  53. [key: string]: any;
  54. };
  55. }
  56. interface Import extends ImportCommon {
  57. /** Import name to be detected */
  58. name: ImportName;
  59. /** Import as this name */
  60. as?: ImportName;
  61. }
  62. type PresetImport = ImportName | [name: ImportName, as?: ImportName, from?: ModuleId] | Exclude<Import, 'from'>;
  63. interface InlinePreset extends ImportCommon {
  64. imports: (PresetImport | InlinePreset)[];
  65. }
  66. /**
  67. * Auto extract exports from a package for auto import
  68. */
  69. interface PackagePreset {
  70. /**
  71. * Name of the package
  72. */
  73. package: string;
  74. /**
  75. * Path of the importer
  76. * @default process.cwd()
  77. */
  78. url?: string;
  79. /**
  80. * RegExp, string, or custom function to exclude names of the extracted imports
  81. */
  82. ignore?: (string | RegExp | ((name: string) => boolean))[];
  83. /**
  84. * Use local cache if exits
  85. * @default true
  86. */
  87. cache?: boolean;
  88. }
  89. type Preset = InlinePreset | PackagePreset;
  90. interface UnimportContext {
  91. staticImports: Import[];
  92. dynamicImports: Import[];
  93. getImports(): Promise<Import[]>;
  94. getImportMap(): Promise<Map<string, Import>>;
  95. addons: Addon[];
  96. invalidate(): void;
  97. resolveId(id: string, parentId?: string): Thenable<string | null | undefined | void>;
  98. options: Partial<UnimportOptions>;
  99. getMetadata(): UnimportMeta | undefined;
  100. }
  101. interface InjectionUsageRecord {
  102. import: Import;
  103. count: number;
  104. moduleIds: string[];
  105. }
  106. interface UnimportMeta {
  107. injectionUsage: Record<string, InjectionUsageRecord>;
  108. }
  109. interface AddonsOptions {
  110. /**
  111. * Enable auto import inside for Vue's <template>
  112. *
  113. * @default false
  114. */
  115. vueTemplate?: boolean;
  116. }
  117. interface UnimportOptions {
  118. /**
  119. * Auto import items
  120. */
  121. imports: Import[];
  122. /**
  123. * Auto import preset
  124. */
  125. presets: (Preset | BuiltinPresetName)[];
  126. /**
  127. * Custom warning function
  128. * @default console.warn
  129. */
  130. warn: (msg: string) => void;
  131. /**
  132. * Custom debug log function
  133. * @default console.log
  134. */
  135. debugLog: (msg: string) => void;
  136. /**
  137. * Unimport Addons
  138. * To use built-in addons, use `addons: { vueTemplate: true }`
  139. *
  140. * Built-in addons:
  141. * - vueTemplate: enable auto import inside for Vue's <template>
  142. *
  143. * @default {}
  144. */
  145. addons: AddonsOptions | Addon[];
  146. /**
  147. * Name of virtual modules that exposed all the registed auto-imports
  148. * @default []
  149. */
  150. virtualImports: string[];
  151. /**
  152. * Custom resolver to auto import id
  153. */
  154. resolveId?: (id: string, importee?: string) => Thenable<string | void>;
  155. /**
  156. * Custom magic comments to be opt-out for auto import, per file/module
  157. *
  158. * @default ['@unimport-disable', '@imports-disable']
  159. */
  160. commentsDisable?: string[];
  161. /**
  162. * Custom magic comments to debug auto import, printed to console
  163. *
  164. * @default ['@unimport-debug', '@imports-debug']
  165. */
  166. commentsDebug?: string[];
  167. /**
  168. * Collect meta data for each auto import. Accessible via `ctx.meta`
  169. */
  170. collectMeta?: boolean;
  171. }
  172. type PathFromResolver = (_import: Import) => string | undefined;
  173. interface ScanDirExportsOptions {
  174. /**
  175. * Glob patterns for matching files
  176. *
  177. * @default ['*.{ts,js,mjs,cjs,mts,cts}']
  178. */
  179. filePatterns?: string[];
  180. /**
  181. * Custom function to filter scanned files
  182. */
  183. fileFilter?: (file: string) => boolean;
  184. /**
  185. * Current working directory
  186. *
  187. * @default process.cwd()
  188. */
  189. cwd?: string;
  190. }
  191. interface TypeDeclarationOptions {
  192. /**
  193. * Custom resolver for path of the import
  194. */
  195. resolvePath?: PathFromResolver;
  196. /**
  197. * Append `export {}` to the end of the file
  198. *
  199. * @default true
  200. */
  201. exportHelper?: boolean;
  202. }
  203. interface InjectImportsOptions {
  204. /**
  205. * Merge the existing imports
  206. *
  207. * @default false
  208. */
  209. mergeExisting?: boolean;
  210. /**
  211. * If the module should be auto imported
  212. *
  213. * @default true
  214. */
  215. autoImport?: boolean;
  216. /**
  217. * If the module should be transformed for virtual modules.
  218. * Only available when `virtualImports` is set.
  219. *
  220. * @default true
  221. */
  222. transformVirtualImports?: boolean;
  223. /** @deprecated use `virtualImports` instead */
  224. transformVirtualImoports?: boolean;
  225. }
  226. type Thenable<T> = Promise<T> | T;
  227. interface Addon {
  228. transform?: (this: UnimportContext, code: MagicString, id: string | undefined) => Thenable<MagicString>;
  229. declaration?: (this: UnimportContext, dts: string, options: TypeDeclarationOptions) => Thenable<string>;
  230. matchImports?: (this: UnimportContext, identifiers: Set<string>, matched: Import[]) => Thenable<Import[] | void>;
  231. }
  232. interface InstallGlobalOptions {
  233. /**
  234. * @default globalThis
  235. */
  236. globalObject?: any;
  237. /**
  238. * Overrides the existing property
  239. * @default false
  240. */
  241. overrides?: boolean;
  242. }
  243. interface MagicStringResult {
  244. s: MagicString;
  245. code: string;
  246. }
  247. interface ImportInjectionResult extends MagicStringResult {
  248. imports: Import[];
  249. }
  250. export { AddonsOptions as A, BuiltinPresetName as B, Import as I, MagicStringResult as M, Preset as P, ScanDirExportsOptions as S, TypeDeclarationOptions as T, UnimportOptions as U, InlinePreset as a, Thenable as b, InjectImportsOptions as c, ImportInjectionResult as d, UnimportMeta as e, InstallGlobalOptions as f, builtinPresets as g, ModuleId as h, ImportName as i, ImportCommon as j, PresetImport as k, PackagePreset as l, UnimportContext as m, InjectionUsageRecord as n, PathFromResolver as o, Addon as p };