Skip to content

Commit fa4ca99

Browse files
ElMassimoantfu
andauthored
feat: add a components api to allow manual detection in frameworks (#329)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 6a3eb84 commit fa4ca99

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/core/unplugin.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { createUnplugin } from 'unplugin'
22
import { createFilter } from '@rollup/pluginutils'
33
import chokidar from 'chokidar'
44
import type { ResolvedConfig, ViteDevServer } from 'vite'
5-
import type { Options } from '../types'
5+
import type { Options, PublicPluginAPI } from '../types'
66
import { Context } from './context'
7-
import { shouldTransform } from './utils'
7+
import { shouldTransform, stringifyComponentImport } from './utils'
88

99
export default createUnplugin<Options>((options = {}) => {
1010
const filter = createFilter(
@@ -13,10 +13,21 @@ export default createUnplugin<Options>((options = {}) => {
1313
)
1414
const ctx: Context = new Context(options)
1515

16+
const api: PublicPluginAPI = {
17+
async findComponent(name, filename) {
18+
return await ctx.findComponent(name, 'component', filename ? [filename] : [])
19+
},
20+
stringifyImport(info) {
21+
return stringifyComponentImport(info, ctx)
22+
},
23+
}
24+
1625
return {
1726
name: 'unplugin-vue-components',
1827
enforce: 'post',
1928

29+
api,
30+
2031
transformInclude(id) {
2132
return filter(id)
2233
},

src/types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ export type Transformer = (code: string, id: string, path: string, query: Record
3434

3535
export type SupportedTransformer = 'vue3' | 'vue2'
3636

37+
export interface PublicPluginAPI {
38+
/**
39+
* Resolves a component using the configured resolvers.
40+
*/
41+
findComponent: (name: string, filename?: string) => Promise<ComponentInfo | undefined>
42+
/**
43+
* Obtain an import statement for a resolved component.
44+
*/
45+
stringifyImport: (info: ComponentInfo) => string
46+
}
47+
3748
/**
3849
* Plugin options.
3950
*/

src/vite.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { Plugin } from 'vite'
2+
import type { Options, PublicPluginAPI } from './types'
13
import unplugin from '.'
24

3-
export default unplugin.vite
5+
export default unplugin.vite as (options?: Options | undefined) => Plugin & { api: PublicPluginAPI }

0 commit comments

Comments
 (0)