unimport.e1e7fe3e.d.mts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. import MagicString from 'magic-string';
  2. import { ESMExport } from 'mlly';
  3. declare const builtinPresets: {
  4. '@vue/composition-api': InlinePreset;
  5. '@vueuse/core': () => Preset;
  6. '@vueuse/head': InlinePreset;
  7. pinia: InlinePreset;
  8. preact: InlinePreset;
  9. quasar: InlinePreset;
  10. react: InlinePreset;
  11. 'react-router': InlinePreset;
  12. 'react-router-dom': InlinePreset;
  13. svelte: InlinePreset;
  14. 'svelte/animate': InlinePreset;
  15. 'svelte/easing': InlinePreset;
  16. 'svelte/motion': InlinePreset;
  17. 'svelte/store': InlinePreset;
  18. 'svelte/transition': InlinePreset;
  19. 'vee-validate': InlinePreset;
  20. vitepress: InlinePreset;
  21. 'vue-demi': InlinePreset;
  22. 'vue-i18n': InlinePreset;
  23. 'vue-router': InlinePreset;
  24. 'vue-router-composables': InlinePreset;
  25. vue: InlinePreset;
  26. 'vue/macros': InlinePreset;
  27. vuex: InlinePreset;
  28. vitest: InlinePreset;
  29. 'uni-app': InlinePreset;
  30. 'solid-js': InlinePreset;
  31. 'solid-app-router': InlinePreset;
  32. rxjs: InlinePreset;
  33. 'date-fns': InlinePreset;
  34. };
  35. type BuiltinPresetName = keyof typeof builtinPresets;
  36. type ModuleId = string;
  37. type ImportName = string;
  38. interface ImportCommon {
  39. /** Module specifier to import from */
  40. from: ModuleId;
  41. /**
  42. * Priority of the import, if multiple imports have the same name, the one with the highest priority will be used
  43. * @default 1
  44. */
  45. priority?: number;
  46. /** If this import is disabled */
  47. disabled?: boolean;
  48. /** Won't output import in declaration file if true */
  49. dtsDisabled?: boolean;
  50. /** Import declaration type like const / var / enum */
  51. declarationType?: ESMExport['declarationType'];
  52. /**
  53. * Metadata of the import
  54. */
  55. meta?: {
  56. /** Short description of the import */
  57. description?: string;
  58. /** URL to the documentation */
  59. docsUrl?: string;
  60. /** Additional metadata */
  61. [key: string]: any;
  62. };
  63. /**
  64. * If this import is a pure type import
  65. */
  66. type?: boolean;
  67. /**
  68. * Using this as the from when generating type declarations
  69. */
  70. typeFrom?: ModuleId;
  71. }
  72. interface Import extends ImportCommon {
  73. /** Import name to be detected */
  74. name: ImportName;
  75. /** Import as this name */
  76. as?: ImportName;
  77. /**
  78. * With properties
  79. *
  80. * Ignored for CJS imports.
  81. */
  82. with?: Record<string, string>;
  83. }
  84. type PresetImport = Omit<Import, 'from'> | ImportName | [name: ImportName, as?: ImportName, from?: ModuleId];
  85. interface InlinePreset extends ImportCommon {
  86. imports: (PresetImport | InlinePreset)[];
  87. }
  88. /**
  89. * Auto extract exports from a package for auto import
  90. */
  91. interface PackagePreset {
  92. /**
  93. * Name of the package
  94. */
  95. package: string;
  96. /**
  97. * Path of the importer
  98. * @default process.cwd()
  99. */
  100. url?: string;
  101. /**
  102. * RegExp, string, or custom function to exclude names of the extracted imports
  103. */
  104. ignore?: (string | RegExp | ((name: string) => boolean))[];
  105. /**
  106. * Use local cache if exits
  107. * @default true
  108. */
  109. cache?: boolean;
  110. }
  111. type Preset = InlinePreset | PackagePreset;
  112. interface UnimportContext {
  113. readonly version: string;
  114. options: Partial<UnimportOptions>;
  115. staticImports: Import[];
  116. dynamicImports: Import[];
  117. addons: Addon[];
  118. getImports: () => Promise<Import[]>;
  119. getImportMap: () => Promise<Map<string, Import>>;
  120. getMetadata: () => UnimportMeta | undefined;
  121. modifyDynamicImports: (fn: (imports: Import[]) => Thenable<void | Import[]>) => Promise<void>;
  122. clearDynamicImports: () => void;
  123. replaceImports: (imports: UnimportOptions['imports']) => Promise<Import[]>;
  124. invalidate: () => void;
  125. resolveId: (id: string, parentId?: string) => Thenable<string | null | undefined | void>;
  126. }
  127. interface DetectImportResult {
  128. s: MagicString;
  129. strippedCode: string;
  130. isCJSContext: boolean;
  131. matchedImports: Import[];
  132. firstOccurrence: number;
  133. }
  134. interface Unimport {
  135. readonly version: string;
  136. init: () => Promise<void>;
  137. clearDynamicImports: UnimportContext['clearDynamicImports'];
  138. getImportMap: UnimportContext['getImportMap'];
  139. getImports: UnimportContext['getImports'];
  140. getInternalContext: () => UnimportContext;
  141. getMetadata: UnimportContext['getMetadata'];
  142. modifyDynamicImports: UnimportContext['modifyDynamicImports'];
  143. generateTypeDeclarations: (options?: TypeDeclarationOptions) => Promise<string>;
  144. /**
  145. * Get un-imported usages from code
  146. */
  147. detectImports: (code: string | MagicString) => Promise<DetectImportResult>;
  148. /**
  149. * Insert missing imports statements to code
  150. */
  151. injectImports: (code: string | MagicString, id?: string, options?: InjectImportsOptions) => Promise<ImportInjectionResult>;
  152. scanImportsFromDir: (dir?: string[], options?: ScanDirExportsOptions) => Promise<Import[]>;
  153. scanImportsFromFile: (file: string, includeTypes?: boolean) => Promise<Import[]>;
  154. /**
  155. * @deprecated
  156. */
  157. toExports: (filepath?: string, includeTypes?: boolean) => Promise<string>;
  158. }
  159. interface InjectionUsageRecord {
  160. import: Import;
  161. count: number;
  162. moduleIds: string[];
  163. }
  164. interface UnimportMeta {
  165. injectionUsage: Record<string, InjectionUsageRecord>;
  166. }
  167. interface AddonsOptions {
  168. /**
  169. * Enable auto import inside for Vue's <template>
  170. *
  171. * @default false
  172. */
  173. vueTemplate?: boolean;
  174. }
  175. interface UnimportOptions extends Pick<InjectImportsOptions, 'injectAtEnd' | 'mergeExisting' | 'parser'> {
  176. /**
  177. * Auto import items
  178. */
  179. imports: Import[];
  180. /**
  181. * Auto import preset
  182. */
  183. presets: (Preset | BuiltinPresetName)[];
  184. /**
  185. * Custom warning function
  186. * @default console.warn
  187. */
  188. warn: (msg: string) => void;
  189. /**
  190. * Custom debug log function
  191. * @default console.log
  192. */
  193. debugLog: (msg: string) => void;
  194. /**
  195. * Unimport Addons
  196. * To use built-in addons, use `addons: { vueTemplate: true }`
  197. *
  198. * Built-in addons:
  199. * - vueTemplate: enable auto import inside for Vue's <template>
  200. *
  201. * @default {}
  202. */
  203. addons: AddonsOptions | Addon[];
  204. /**
  205. * Name of virtual modules that exposed all the registed auto-imports
  206. * @default []
  207. */
  208. virtualImports: string[];
  209. /**
  210. * Directories to scan for auto import
  211. * @default []
  212. */
  213. dirs?: string[];
  214. /**
  215. * Options for scanning directories for auto import
  216. */
  217. dirsScanOptions?: ScanDirExportsOptions;
  218. /**
  219. * Custom resolver to auto import id
  220. */
  221. resolveId?: (id: string, importee?: string) => Thenable<string | void>;
  222. /**
  223. * Custom magic comments to be opt-out for auto import, per file/module
  224. *
  225. * @default ['@unimport-disable', '@imports-disable']
  226. */
  227. commentsDisable?: string[];
  228. /**
  229. * Custom magic comments to debug auto import, printed to console
  230. *
  231. * @default ['@unimport-debug', '@imports-debug']
  232. */
  233. commentsDebug?: string[];
  234. /**
  235. * Collect meta data for each auto import. Accessible via `ctx.meta`
  236. */
  237. collectMeta?: boolean;
  238. }
  239. type PathFromResolver = (_import: Import) => string | undefined;
  240. interface ScanDirExportsOptions {
  241. /**
  242. * Glob patterns for matching files
  243. *
  244. * @default ['*.{ts,js,mjs,cjs,mts,cts}']
  245. */
  246. filePatterns?: string[];
  247. /**
  248. * Custom function to filter scanned files
  249. */
  250. fileFilter?: (file: string) => boolean;
  251. /**
  252. * Register type exports
  253. *
  254. * @default true
  255. */
  256. types?: boolean;
  257. /**
  258. * Current working directory
  259. *
  260. * @default process.cwd()
  261. */
  262. cwd?: string;
  263. }
  264. interface TypeDeclarationOptions {
  265. /**
  266. * Custom resolver for path of the import
  267. */
  268. resolvePath?: PathFromResolver;
  269. /**
  270. * Append `export {}` to the end of the file
  271. *
  272. * @default true
  273. */
  274. exportHelper?: boolean;
  275. /**
  276. * Auto-import for type exports
  277. *
  278. * @default true
  279. */
  280. typeReExports?: boolean;
  281. }
  282. interface InjectImportsOptions {
  283. /**
  284. * Merge the existing imports
  285. *
  286. * @default false
  287. */
  288. mergeExisting?: boolean;
  289. /**
  290. * If the module should be auto imported
  291. *
  292. * @default true
  293. */
  294. autoImport?: boolean;
  295. /**
  296. * If the module should be transformed for virtual modules.
  297. * Only available when `virtualImports` is set.
  298. *
  299. * @default true
  300. */
  301. transformVirtualImports?: boolean;
  302. /**
  303. * Parser to use for parsing the code
  304. *
  305. * Note that `acorn` only takes valid JS Code, should usually only be used after transformationa and transpilation
  306. *
  307. * @default 'regex'
  308. */
  309. parser?: 'acorn' | 'regex';
  310. /**
  311. * Inject the imports at the end of other imports
  312. *
  313. * @default false
  314. */
  315. injectAtEnd?: boolean;
  316. }
  317. type Thenable<T> = Promise<T> | T;
  318. interface Addon {
  319. transform?: (this: UnimportContext, code: MagicString, id: string | undefined) => Thenable<MagicString>;
  320. declaration?: (this: UnimportContext, dts: string, options: TypeDeclarationOptions) => Thenable<string>;
  321. matchImports?: (this: UnimportContext, identifiers: Set<string>, matched: Import[]) => Thenable<Import[] | void>;
  322. /**
  323. * Extend or modify the imports list before injecting
  324. */
  325. extendImports?: (this: UnimportContext, imports: Import[]) => Import[] | void;
  326. /**
  327. * Resolve imports before injecting
  328. */
  329. injectImportsResolved?: (this: UnimportContext, imports: Import[], code: MagicString, id?: string) => Import[] | void;
  330. /**
  331. * Modify the injection code before injecting
  332. */
  333. injectImportsStringified?: (this: UnimportContext, injection: string, imports: Import[], code: MagicString, id?: string) => string | void;
  334. }
  335. interface InstallGlobalOptions {
  336. /**
  337. * @default globalThis
  338. */
  339. globalObject?: any;
  340. /**
  341. * Overrides the existing property
  342. * @default false
  343. */
  344. overrides?: boolean;
  345. }
  346. interface MagicStringResult {
  347. s: MagicString;
  348. code: string;
  349. }
  350. interface ImportInjectionResult extends MagicStringResult {
  351. imports: Import[];
  352. }
  353. export { type AddonsOptions as A, type BuiltinPresetName as B, type DetectImportResult as D, type Import as I, type MagicStringResult as M, type Preset as P, type ScanDirExportsOptions as S, type TypeDeclarationOptions as T, type UnimportOptions as U, type InlinePreset as a, type Unimport as b, type InstallGlobalOptions as c, builtinPresets as d, type ModuleId as e, type ImportName as f, type ImportCommon as g, type PresetImport as h, type PackagePreset as i, type UnimportContext as j, type InjectionUsageRecord as k, type UnimportMeta as l, type PathFromResolver as m, type InjectImportsOptions as n, type Thenable as o, type Addon as p, type ImportInjectionResult as q };