Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an option for pre-built components #291

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/resolvers/antdv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ function getSideEffects(compName: string, options: AntDesignVueResolverOptions):
return `ant-design-vue/${lib}/${styleDir}/style/css`
}
}
const primitiveNames = ['Affix', 'Anchor', 'AnchorLink', 'AutoComplete', 'AutoCompleteOptGroup', 'AutoCompleteOption', 'Alert', 'Avatar', 'AvatarGroup', 'BackTop', 'Badge', 'BadgeRibbon', 'Breadcrumb', 'BreadcrumbItem', 'BreadcrumbSeparator', 'Button', 'ButtonGroup', 'Calendar', 'Card', 'CardGrid', 'CardMeta', 'Collapse', 'CollapsePanel', 'Carousel', 'Cascader', 'Checkbox', 'CheckboxGroup', 'Col', 'Comment', 'ConfigProvider', 'DatePicker', 'MonthPicker', 'WeekPicker', 'RangePicker', 'QuarterPicker', 'Descriptions', 'DescriptionsItem', 'Divider', 'Dropdown', 'DropdownButton', 'Drawer', 'Empty', 'Form', 'FormItem', 'FormItemRest', 'Grid', 'Input', 'InputGroup', 'InputPassword', 'InputSearch', 'Textarea', 'Image', 'ImagePreviewGroup', 'InputNumber', 'Layout', 'LayoutHeader', 'LayoutSider', 'LayoutFooter', 'LayoutContent', 'List', 'ListItem', 'ListItemMeta', 'Menu', 'MenuDivider', 'MenuItem', 'MenuItemGroup', 'SubMenu', 'Mentions', 'MentionsOption', 'Modal', 'Statistic', 'StatisticCountdown', 'PageHeader', 'Pagination', 'Popconfirm', 'Popover', 'Progress', 'Radio', 'RadioButton', 'RadioGroup', 'Rate', 'Result', 'Row', 'Select', 'SelectOptGroup', 'SelectOption', 'Skeleton', 'SkeletonButton', 'SkeletonAvatar', 'SkeletonInput', 'SkeletonImage', 'Slider', 'Space', 'Spin', 'Steps', 'Step', 'Switch', 'Table', 'TableColumn', 'TableColumnGroup', 'TableSummary', 'TableSummaryRow', 'TableSummaryCell', 'Transfer', 'Tree', 'TreeNode', 'DirectoryTree', 'TreeSelect', 'TreeSelectNode', 'Tabs', 'TabPane', 'Tag', 'CheckableTag', 'TimePicker', 'TimeRangePicker', 'Timeline', 'TimelineItem', 'Tooltip', 'Typography', 'TypographyLink', 'TypographyParagraph', 'TypographyText', 'TypographyTitle', 'Upload', 'UploadDragger', 'LocaleProvider']
const primitiveNames = ['Affix', 'Anchor', 'AnchorLink', 'AutoComplete', 'AutoCompleteOptGroup', 'AutoCompleteOption', 'Alert', 'Avatar', 'AvatarGroup', 'BackTop', 'Badge', 'BadgeRibbon', 'Breadcrumb', 'BreadcrumbItem', 'BreadcrumbSeparator', 'Button', 'ButtonGroup', 'Calendar', 'Card', 'CardGrid', 'CardMeta', 'Collapse', 'CollapsePanel', 'Carousel', 'Cascader', 'Checkbox', 'CheckboxGroup', 'Col', 'Comment', 'ConfigProvider', 'DatePicker', 'MonthPicker', 'WeekPicker', 'RangePicker', 'Descriptions', 'DescriptionsItem', 'Divider', 'Dropdown', 'DropdownButton', 'Drawer', 'Empty', 'Form', 'FormItem', 'FormItemRest', 'Grid', 'Input', 'InputGroup', 'InputPassword', 'InputSearch', 'Textarea', 'Image', 'ImagePreviewGroup', 'InputNumber', 'Layout', 'LayoutHeader', 'LayoutSider', 'LayoutFooter', 'LayoutContent', 'List', 'ListItem', 'ListItemMeta', 'Menu', 'MenuDivider', 'MenuItem', 'MenuItemGroup', 'SubMenu', 'Mentions', 'MentionsOption', 'Modal', 'Statistic', 'StatisticCountdown', 'PageHeader', 'Pagination', 'Popconfirm', 'Popover', 'Progress', 'Radio', 'RadioButton', 'RadioGroup', 'Rate', 'Result', 'Row', 'Select', 'SelectOptGroup', 'SelectOption', 'Skeleton', 'SkeletonButton', 'SkeletonAvatar', 'SkeletonInput', 'SkeletonImage', 'Slider', 'Space', 'Spin', 'Steps', 'Step', 'Switch', 'Table', 'TableColumn', 'TableColumnGroup', 'TableSummary', 'TableSummaryRow', 'TableSummaryCell', 'Transfer', 'Tree', 'TreeNode', 'DirectoryTree', 'TreeSelect', 'TreeSelectNode', 'Tabs', 'TabPane', 'Tag', 'CheckableTag', 'TimePicker', 'TimeRangePicker', 'Timeline', 'TimelineItem', 'Tooltip', 'Typography', 'TypographyLink', 'TypographyParagraph', 'TypographyText', 'TypographyTitle', 'Upload', 'UploadDragger', 'LocaleProvider']
const prefix = 'A'

let antdvNames: Set<string>
Expand Down Expand Up @@ -278,5 +278,8 @@ export function AntDesignVueResolver(options: AntDesignVueResolverOptions = {
}
}
},
getAllComponentNames: () => {
return [...antdvNames]
},
}
}
100 changes: 100 additions & 0 deletions src/core/resolvers/vant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,103 @@ export interface VantResolverOptions {
importStyle?: boolean | 'css' | 'less'
}

const primitiveNames = [
'ActionBar',
'ActionBarButton',
'ActionBarIcon',
'ActionSheet',
'AddressEdit',
'AddressList',
'Area',
'Badge',
'Button',
'Calendar',
'Card',
'Cascader',
'Cell',
'CellGroup',
'Checkbox',
'CheckboxGroup',
'Circle',
'Col',
'Collapse',
'CollapseItem',
'ConfigProvider',
'ContactCard',
'ContactEdit',
'ContactList',
'CountDown',
'Coupon',
'CouponCell',
'CouponList',
'DatetimePicker',
'Dialog',
'Divider',
'DropdownItem',
'DropdownMenu',
'Empty',
'Field',
'Form',
'Grid',
'GridItem',
'Icon',
'Image',
'ImagePreview',
'IndexAnchor',
'IndexBar',
'List',
'Loading',
'Locale',
'NavBar',
'NoticeBar',
'Notify',
'NumberKeyboard',
'Overlay',
'Pagination',
'PasswordInput',
'Picker',
'Popover',
'Popup',
'Progress',
'PullRefresh',
'Radio',
'RadioGroup',
'Rate',
'Row',
'Search',
'ShareSheet',
'Sidebar',
'SidebarItem',
'Skeleton',
'Slider',
'Step',
'Stepper',
'Steps',
'Sticky',
'SubmitBar',
'Swipe',
'SwipeCell',
'SwipeItem',
'Switch',
'Tab',
'Tabbar',
'TabbarItem',
'Tabs',
'Tag',
'Toast',
'TreeSelect',
'Uploader',
]

const prefix = 'Van'

let vantNames: Set<string>

function genVantNames(primitiveNames: string[]): void {
vantNames = new Set(primitiveNames.map(name => `${prefix}${name}`))
}
genVantNames(primitiveNames)

function getSideEffects(dirName: string, options: VantResolverOptions): SideEffectsInfo | undefined {
const { importStyle = true } = options

Expand Down Expand Up @@ -43,5 +140,8 @@ export function VantResolver(options: VantResolverOptions = {}): ComponentResolv
}
}
},
getAllComponentNames: () => {
return [...vantNames]
},
}
}
31 changes: 29 additions & 2 deletions src/core/unplugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { createUnplugin } from 'unplugin'
import { createFilter } from '@rollup/pluginutils'
import chokidar from 'chokidar'
import type { ResolvedConfig, ViteDevServer } from 'vite'
import type { Options } from '../types'
import Debug from 'debug'
import type { ResolvedConfig, UserConfig, ViteDevServer } from 'vite'
import type { ComponentInfo, Options } from '../types'
import { Context } from './context'
import { shouldTransform } from './utils'

const debug = Debug('unplugin-vue-components:unplugin')

export default createUnplugin<Options>((options = {}) => {
const filter = createFilter(
options.include || [/\.vue$/, /\.vue\?vue/],
Expand Down Expand Up @@ -35,6 +38,30 @@ export default createUnplugin<Options>((options = {}) => {
},

vite: {
config(config: UserConfig) {
if (ctx.options.preBuild) {
ctx.options.resolvers.forEach((r) => {
if (r.getAllComponentNames) {
const allComponentNames = r.getAllComponentNames()
const resolvedNames = allComponentNames
.map((name) => {
const componentInfo = r.resolve(name) as ComponentInfo
debug(componentInfo)

const fullPath = componentInfo.name ? `${componentInfo.path}/${componentInfo.name}` : componentInfo.path

const sideEffects = (componentInfo?.sideEffects as string) || ''

return [fullPath, sideEffects]
})
.flat()
config.optimizeDeps = config.optimizeDeps || {}
config.optimizeDeps.include = Array.from(new Set([...(config.optimizeDeps.include || []), ...resolvedNames]))
config.optimizeDeps.include.sort()
}
})
}
},
configResolved(config: ResolvedConfig) {
ctx.setRoot(config.root)
ctx.sourcemap = true
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type ComponentResolverFunction = (name: string) => ComponentResolveResult
export interface ComponentResolverObject {
type: 'component' | 'directive'
resolve: ComponentResolverFunction
getAllComponentNames?: () => Array<string>
}
export type ComponentResolver = ComponentResolverFunction | ComponentResolverObject
export interface UILibraryOptions {
Expand Down Expand Up @@ -152,6 +153,7 @@ Required<Options>,
globs: string[]
dts: string | false
root: string
preBuild?: boolean
}

export type ComponentsImportMap = Record<string, string[] | undefined>