### Basic Vite Plugin Uni Components Setup Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Demonstrates the basic setup for @uni-helper/vite-plugin-uni-components in a Vite configuration file. Ensure this plugin is registered before the Uni() plugin. ```typescript import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' export default defineConfig({ plugins: [ Components(), // Must be before Uni() Uni(), ], }) ``` -------------------------------- ### Install @uni-helper/vite-plugin-uni-components Source: https://github.com/uni-helper/vite-plugin-uni-components/blob/main/packages/core/README.md Installs the @uni-helper/vite-plugin-uni-components package as a development dependency using pnpm. This is the initial step before configuring the plugin in your Vite project. ```bash pnpm i -D @uni-helper/vite-plugin-uni-components ``` -------------------------------- ### Implement Custom Component and Directive Resolvers Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Provides an example of creating custom resolvers for third-party component and directive libraries. This allows for flexible integration with any component library by defining custom resolution logic. ```typescript import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' import type { ComponentResolver } from '@uni-helper/vite-plugin-uni-components' const MyLibraryResolver: ComponentResolver = { type: 'component', resolve: (name: string) => { // Match components starting with "My" if (name.match(/^My[A-Z]/)) { return { name, from: `my-component-library/lib/${name.toLowerCase()}`, } } }, } const MyDirectiveResolver: ComponentResolver = { type: 'directive', resolve: (name: string) => { if (name === 'loading') { return { name: 'vLoading', from: 'my-directives/loading', } } }, } export default defineConfig({ plugins: [ Components({ resolvers: [MyLibraryResolver, MyDirectiveResolver], directives: true, // Enable directive auto-import }), Uni(), ], }) ``` -------------------------------- ### Utility Functions for Component Name Transformations Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Provides utility functions for converting component names between different casing conventions (PascalCase, camelCase, kebab-case). These functions are useful for creating custom resolvers or other tooling that requires consistent naming. It also shows an example of using `kebabCase` within a custom `ComponentResolver`. Dependencies include `@uni-helper/vite-plugin-uni-components`. ```typescript import { pascalCase, camelCase, kebabCase } from '@uni-helper/vite-plugin-uni-components' // Convert naming conventions const component1 = pascalCase('my-component') // 'MyComponent' const component2 = camelCase('my-component') // 'myComponent' const component3 = kebabCase('MyComponent') // 'my-component' // Use in custom resolver import type { ComponentResolver } from '@uni-helper/vite-plugin-uni-components' const CustomResolver: ComponentResolver = { type: 'component', resolve: (name: string) => { const kebabName = kebabCase(name) return { name, from: `my-library/components/${kebabName}/${kebabName}.vue`, } }, } ``` -------------------------------- ### Configure Custom Directories and Extensions Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Shows how to configure custom directories to scan for components, specify file extensions to match, and enable subdirectory searching. It also configures TypeScript declaration file generation. ```typescript import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' export default defineConfig({ plugins: [ Components({ dirs: ['src/components', 'src/ui-components'], extensions: ['vue', 'tsx'], deep: true, // Search subdirectories dts: 'components.d.ts', // TypeScript declaration file }), Uni(), ], }) ``` -------------------------------- ### Programmatic Usage of Vite Plugin Uni Components Context Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Demonstrates how to use the plugin's Context API programmatically for advanced scenarios like custom build tools or framework integration. It covers creating a context, setting the project root, transforming code, accessing the component registry, and generating type declarations. Dependencies include `@uni-helper/vite-plugin-uni-components`. ```typescript import { Context } from '@uni-helper/vite-plugin-uni-components' import type { ComponentResolver } from '@uni-helper/vite-plugin-uni-components' // Create context const ctx = new Context({ dirs: ['src/components'], extensions: ['vue'], resolvers: [ { type: 'component', resolve: (name) => { if (name === 'CustomButton') { return { from: 'my-lib/button', name: 'Button', as: 'CustomButton' } } }, }, ], transformer: 'vue3', }) // Set project root ctx.setRoot('/path/to/project') // Transform code const code = ` const render = (_ctx, _cache) => { const _component_my_button = _resolveComponent("my-button") return _createBlock(_component_my_button) } ` const result = await ctx.transform(code, 'App.vue') console.log(result.code) // Contains auto-imported components // Access component registry const componentMap = ctx.componentNameMap console.log(Object.keys(componentMap)) // ['MyButton', 'UserCard', ...] // Generate TypeScript declarations ctx.generateDeclaration() ``` -------------------------------- ### Integrate UI Library Resolvers Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Demonstrates how to use built-in resolvers for popular UniApp UI frameworks like uni-ui, wot-design-uni, uv-ui, and uview-pro. Multiple resolvers can be combined. ```typescript import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' import { UniUIResolver, WotResolver, UvResolver, uViewProResolver } from '@uni-helper/vite-plugin-uni-components/resolvers' export default defineConfig({ plugins: [ Components({ resolvers: [ UniUIResolver({ exclude: 'UniTest', // Exclude specific components }), WotResolver(), UvResolver(), uViewProResolver(), ], }), Uni(), ], }) ``` -------------------------------- ### Advanced Component Matching with Glob Patterns Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Provides precise control over which files are recognized as components using glob patterns. This method bypasses the `dirs` and `extensions` options, offering maximum flexibility in component selection and exclusion. ```typescript import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' export default defineConfig({ plugins: [ Components({ globs: [ 'src/components/**/*.vue', 'src/ui/**/*.{vue,tsx}', '!src/components/internal/**', ], }), Uni(), ], }) ``` -------------------------------- ### Configure Vite for UniApp Components Source: https://github.com/uni-helper/vite-plugin-uni-components/blob/main/packages/core/README.md Configures Vite to use the @uni-helper/vite-plugin-uni-components plugin. This involves importing the plugin and adding it to the Vite configuration, ensuring it's placed before the Uni plugin. ```typescript // vite.config.ts import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' // https://vitejs.dev/config/ export default defineConfig({ plugins: [ // make sure put it before `Uni()` Components(), Uni(), ], }) ``` -------------------------------- ### Directory Namespacing for Component Naming Source: https://context7.com/uni-helper/vite-plugin-uni-components/llms.txt Enables hierarchical component naming based on folder structure to prevent name collisions and organize components. Redundant prefixes can be collapsed. This feature helps in creating unique and organized component names. ```typescript import { defineConfig } from 'vite' import Uni from '@dcloudio/vite-plugin-uni' import Components from '@uni-helper/vite-plugin-uni-components' export default defineConfig({ plugins: [ Components({ dirs: 'src/components', directoryAsNamespace: true, collapseSamePrefixes: true, globalNamespaces: ['global'], }), Uni(), ], }) // Example: // src/components/user/profile/Card.vue -> // src/components/global/Button.vue ->