unimport.CaVRR9SH.d.mts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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 | ScanDir)[], 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. addons?: Addon[];
  169. /**
  170. * Enable auto import inside for Vue's <template>
  171. *
  172. * @default false
  173. */
  174. vueTemplate?: boolean;
  175. /**
  176. * Enable auto import directives for Vue's SFC.
  177. *
  178. * Library authors should include `meta.vueDirective: true` in the import metadata.
  179. *
  180. * When using a local directives folder, provide the `isDirective`
  181. * callback to check if the import is a Vue directive.
  182. */
  183. vueDirectives?: true | AddonVueDirectivesOptions;
  184. }
  185. interface AddonVueDirectivesOptions {
  186. /**
  187. * Checks if the import is a Vue directive.
  188. *
  189. * **NOTES**:
  190. * - imports from a library should include `meta.vueDirective: true`.
  191. * - this callback is only invoked for local directives (only when meta.vueDirective is not set).
  192. *
  193. * @param from The path of the import normalized.
  194. * @param importEntry The import entry.
  195. */
  196. isDirective?: (from: string, importEntry: Import) => boolean;
  197. }
  198. interface UnimportOptions extends Pick<InjectImportsOptions, 'injectAtEnd' | 'mergeExisting' | 'parser'> {
  199. /**
  200. * Auto import items
  201. */
  202. imports: Import[];
  203. /**
  204. * Auto import preset
  205. */
  206. presets: (Preset | BuiltinPresetName)[];
  207. /**
  208. * Custom warning function
  209. * @default console.warn
  210. */
  211. warn: (msg: string) => void;
  212. /**
  213. * Custom debug log function
  214. * @default console.log
  215. */
  216. debugLog: (msg: string) => void;
  217. /**
  218. * Unimport Addons.
  219. * To use built-in addons, use:
  220. * ```js
  221. * addons: {
  222. * addons: [<custom-addons-here>] // if you want to use also custom addons
  223. * vueTemplate: true,
  224. * vueDirectives: [<the-directives-here>]
  225. * }
  226. * ```
  227. *
  228. * Built-in addons:
  229. * - vueDirectives: enable auto import directives for Vue's SFC
  230. * - vueTemplate: enable auto import inside for Vue's <template>
  231. *
  232. * @default {}
  233. */
  234. addons: AddonsOptions | Addon[];
  235. /**
  236. * Name of virtual modules that exposed all the registed auto-imports
  237. * @default []
  238. */
  239. virtualImports: string[];
  240. /**
  241. * Directories to scan for auto import
  242. * @default []
  243. */
  244. dirs?: (string | ScanDir)[];
  245. /**
  246. * Options for scanning directories for auto import
  247. */
  248. dirsScanOptions?: ScanDirExportsOptions;
  249. /**
  250. * Custom resolver to auto import id
  251. */
  252. resolveId?: (id: string, importee?: string) => Thenable<string | void>;
  253. /**
  254. * Custom magic comments to be opt-out for auto import, per file/module
  255. *
  256. * @default ['@unimport-disable', '@imports-disable']
  257. */
  258. commentsDisable?: string[];
  259. /**
  260. * Custom magic comments to debug auto import, printed to console
  261. *
  262. * @default ['@unimport-debug', '@imports-debug']
  263. */
  264. commentsDebug?: string[];
  265. /**
  266. * Collect meta data for each auto import. Accessible via `ctx.meta`
  267. */
  268. collectMeta?: boolean;
  269. }
  270. type PathFromResolver = (_import: Import) => string | undefined;
  271. interface ScanDirExportsOptions {
  272. /**
  273. * Glob patterns for matching files
  274. *
  275. * @default ['*.{ts,js,mjs,cjs,mts,cts}']
  276. */
  277. filePatterns?: string[];
  278. /**
  279. * Custom function to filter scanned files
  280. */
  281. fileFilter?: (file: string) => boolean;
  282. /**
  283. * Register type exports
  284. *
  285. * @default true
  286. */
  287. types?: boolean;
  288. /**
  289. * Current working directory
  290. *
  291. * @default process.cwd()
  292. */
  293. cwd?: string;
  294. }
  295. interface ScanDir {
  296. /**
  297. * Path pattern of the directory
  298. */
  299. glob: string;
  300. /**
  301. * Register type exports
  302. *
  303. * @default true
  304. */
  305. types?: boolean;
  306. }
  307. interface TypeDeclarationOptions {
  308. /**
  309. * Custom resolver for path of the import
  310. */
  311. resolvePath?: PathFromResolver;
  312. /**
  313. * Append `export {}` to the end of the file
  314. *
  315. * @default true
  316. */
  317. exportHelper?: boolean;
  318. /**
  319. * Auto-import for type exports
  320. *
  321. * @default true
  322. */
  323. typeReExports?: boolean;
  324. }
  325. interface InjectImportsOptions {
  326. /**
  327. * Merge the existing imports
  328. *
  329. * @default false
  330. */
  331. mergeExisting?: boolean;
  332. /**
  333. * If the module should be auto imported
  334. *
  335. * @default true
  336. */
  337. autoImport?: boolean;
  338. /**
  339. * If the module should be transformed for virtual modules.
  340. * Only available when `virtualImports` is set.
  341. *
  342. * @default true
  343. */
  344. transformVirtualImports?: boolean;
  345. /**
  346. * Parser to use for parsing the code
  347. *
  348. * Note that `acorn` only takes valid JS Code, should usually only be used after transformationa and transpilation
  349. *
  350. * @default 'regex'
  351. */
  352. parser?: 'acorn' | 'regex';
  353. /**
  354. * Inject the imports at the end of other imports
  355. *
  356. * @default false
  357. */
  358. injectAtEnd?: boolean;
  359. }
  360. type Thenable<T> = Promise<T> | T;
  361. interface Addon {
  362. name?: string;
  363. transform?: (this: UnimportContext, code: MagicString, id: string | undefined) => Thenable<MagicString>;
  364. declaration?: (this: UnimportContext, dts: string, options: TypeDeclarationOptions) => Thenable<string>;
  365. matchImports?: (this: UnimportContext, identifiers: Set<string>, matched: Import[]) => Thenable<Import[] | void>;
  366. /**
  367. * Extend or modify the imports list before injecting
  368. */
  369. extendImports?: (this: UnimportContext, imports: Import[]) => Import[] | void;
  370. /**
  371. * Resolve imports before injecting
  372. */
  373. injectImportsResolved?: (this: UnimportContext, imports: Import[], code: MagicString, id?: string) => Import[] | void;
  374. /**
  375. * Modify the injection code before injecting
  376. */
  377. injectImportsStringified?: (this: UnimportContext, injection: string, imports: Import[], code: MagicString, id?: string) => string | void;
  378. }
  379. interface InstallGlobalOptions {
  380. /**
  381. * @default globalThis
  382. */
  383. globalObject?: any;
  384. /**
  385. * Overrides the existing property
  386. * @default false
  387. */
  388. overrides?: boolean;
  389. }
  390. interface MagicStringResult {
  391. s: MagicString;
  392. code: string;
  393. }
  394. interface ImportInjectionResult extends MagicStringResult {
  395. imports: Import[];
  396. }
  397. 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 ScanDir as S, type TypeDeclarationOptions as T, type UnimportOptions as U, type Unimport as a, type InstallGlobalOptions as b, type ScanDirExportsOptions as c, type InlinePreset as d, builtinPresets as e, type ModuleId as f, type ImportName as g, type ImportCommon as h, type PresetImport as i, type PackagePreset as j, type UnimportContext as k, type InjectionUsageRecord as l, type UnimportMeta as m, type AddonVueDirectivesOptions as n, type PathFromResolver as o, type InjectImportsOptions as p, type Thenable as q, type Addon as r, type ImportInjectionResult as s };