mirror of
https://github.com/ArthurDanjou/ui.git
synced 2026-01-19 06:21:46 +01:00
feat(unplugin): expose options for embedded plugins, throw warnings for duplication (#3207)
This commit is contained in:
20
src/plugins/auto-import.ts
Normal file
20
src/plugins/auto-import.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { join } from 'pathe'
|
||||
import type { UnpluginContextMeta, UnpluginOptions } from 'unplugin'
|
||||
import { defu } from 'defu'
|
||||
|
||||
import { runtimeDir } from '../unplugin'
|
||||
import type { NuxtUIOptions } from '../unplugin'
|
||||
import AutoImport from 'unplugin-auto-import'
|
||||
import type { Options as AutoImportOptions } from 'unplugin-auto-import/types'
|
||||
|
||||
/**
|
||||
* This plugin adds all the Nuxt UI composables as auto-imports.
|
||||
*/
|
||||
export default function AutoImportPlugin(options: NuxtUIOptions, meta: UnpluginContextMeta): UnpluginOptions {
|
||||
const pluginOptions = defu(options.autoImport, <AutoImportOptions>{
|
||||
dts: options.dts ?? true,
|
||||
dirs: [join(runtimeDir, 'composables')]
|
||||
})
|
||||
|
||||
return AutoImport.raw(pluginOptions, meta) as UnpluginOptions
|
||||
}
|
||||
@@ -2,20 +2,35 @@ import { join, normalize } from 'pathe'
|
||||
import type { UnpluginContextMeta, UnpluginOptions } from 'unplugin'
|
||||
import { globSync } from 'tinyglobby'
|
||||
import AutoImportComponents from 'unplugin-vue-components'
|
||||
import type { Options as ComponentsOptions } from 'unplugin-vue-components/types'
|
||||
|
||||
import { runtimeDir } from '../unplugin'
|
||||
import type { NuxtUIOptions } from '../unplugin'
|
||||
import { defu } from 'defu'
|
||||
|
||||
/**
|
||||
* This plugin adds all the Nuxt UI components as auto-imports.
|
||||
*/
|
||||
export default function ComponentImportPlugin(framework: UnpluginContextMeta['framework'], options: NuxtUIOptions & { prefix: NonNullable<NuxtUIOptions['prefix']> }) {
|
||||
export default function ComponentImportPlugin(options: NuxtUIOptions & { prefix: NonNullable<NuxtUIOptions['prefix']> }, meta: UnpluginContextMeta) {
|
||||
const components = globSync('**/*.vue', { cwd: join(runtimeDir, 'components') })
|
||||
const componentNames = new Set(components.map(c => `${options.prefix}${c.replace(/\.vue$/, '')}`))
|
||||
|
||||
const overrides = globSync('**/*.vue', { cwd: join(runtimeDir, 'vue/components') })
|
||||
const overrideNames = new Set(overrides.map(c => `${options.prefix}${c.replace(/\.vue$/, '')}`))
|
||||
|
||||
const pluginOptions = defu(options.components, <ComponentsOptions>{
|
||||
dts: options.dts ?? true,
|
||||
exclude: [/[\\/]node_modules[\\/](?!\.pnpm|@nuxt\/ui)/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
|
||||
resolvers: [
|
||||
(componentName) => {
|
||||
if (overrideNames.has(componentName))
|
||||
return { name: 'default', from: join(runtimeDir, 'vue/components', `${componentName.slice(options.prefix.length)}.vue`) }
|
||||
if (componentNames.has(componentName))
|
||||
return { name: 'default', from: join(runtimeDir, 'components', `${componentName.slice(options.prefix.length)}.vue`) }
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
return [
|
||||
/**
|
||||
* This plugin aims to ensure we override certain components with Vue-compatible versions:
|
||||
@@ -41,18 +56,7 @@ export default function ComponentImportPlugin(framework: UnpluginContextMeta['fr
|
||||
}
|
||||
}
|
||||
},
|
||||
AutoImportComponents[framework]({
|
||||
dts: options.dts ?? true,
|
||||
exclude: [/[\\/]node_modules[\\/](?!\.pnpm|@nuxt\/ui)/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/],
|
||||
resolvers: [
|
||||
(componentName) => {
|
||||
if (overrideNames.has(componentName))
|
||||
return { name: 'default', from: join(runtimeDir, 'vue/components', `${componentName.slice(options.prefix.length)}.vue`) }
|
||||
if (componentNames.has(componentName))
|
||||
return { name: 'default', from: join(runtimeDir, 'components', `${componentName.slice(options.prefix.length)}.vue`) }
|
||||
}
|
||||
]
|
||||
})
|
||||
AutoImportComponents.raw(pluginOptions, meta) as UnpluginOptions
|
||||
] satisfies UnpluginOptions[]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user