Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.

Commit c4dc367

Browse files
committed
feat: reusing logical of import from nuxt\src\utils\parseActiveImports.ts
1 parent 8dd8184 commit c4dc367

File tree

10 files changed

+60
-37
lines changed

10 files changed

+60
-37
lines changed

packages/bootstrap-vue-next/src/BootstrapVue.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import './styles/styles.scss'
66

77
import * as Components from './components'
88
import * as Directives from './directives/exports'
9+
import type {ComponentType, DirectiveType} from './types/BootstrapVueOptions'
10+
import parseActiveImports from './utils/parseActiveImports'
911

1012
// Inject all components into the global @vue/runtime-core
1113
// This allows intellisense in templates w/out direct importing
@@ -118,21 +120,27 @@ declare module '@vue/runtime-core' {
118120
// Main app plugin
119121
const plugin: Plugin = {
120122
install(app: App, options: BootstrapVueOptions = {components: true, directives: true}) {
121-
const allComponents = typeof options?.components === 'boolean' && options?.components
122123
const selectedComponents =
123-
typeof options?.components === 'object' ? options?.components : undefined
124-
const allDirectives = typeof options?.directives === 'boolean' && options?.directives
125-
const selectedDirectives =
126-
typeof options?.directives === 'object' ? options?.directives : undefined
127-
Object.entries(Components).forEach(([name, component]) => {
128-
if (allComponents || selectedComponents?.[name as keyof typeof Components])
129-
app.component(name, component)
124+
typeof options.components === 'boolean' || typeof options.components === 'undefined'
125+
? {all: true}
126+
: options.components
127+
128+
const componentKeys = Object.keys(Components) as unknown as ComponentType[]
129+
parseActiveImports(selectedComponents, componentKeys).forEach((name) => {
130+
const component = Components[name]
131+
app.component(name, component)
130132
})
131133

132-
Object.entries(Directives).forEach(([name, component]) => {
134+
const selectedDirectives =
135+
typeof options?.directives === 'boolean' || typeof options.directives === 'undefined'
136+
? {all: true}
137+
: options?.directives
138+
139+
const directiveKeys = Object.keys(Directives) as unknown as DirectiveType[]
140+
parseActiveImports(selectedDirectives, directiveKeys).forEach((name) => {
133141
const parsedName = name.toLowerCase().startsWith('v') ? name.slice(1) : name
134-
if (allDirectives || selectedDirectives?.[parsedName as keyof typeof Directives])
135-
app.directive(parsedName, component)
142+
const directive = Directives[name]
143+
app.directive(parsedName, directive)
136144
})
137145

138146
if (options?.BToast) app.use(BToastPlugin, options)

packages/bootstrap-vue-next/src/types/BootstrapVueOptions.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ interface BToastPluginOptions {
55
injectkey: symbol
66
}
77

8+
export type ComponentType = keyof typeof Components
9+
export type DirectiveType = keyof typeof Directives
10+
export type ConfigurationOption<T extends string> = Partial<Record<T, boolean>> & {all: boolean}
11+
812
export interface BootstrapVueOptions {
9-
components?: boolean | Record<keyof typeof Components, boolean>
10-
directives?: boolean | Record<keyof typeof Directives, boolean>
13+
components?: boolean | ConfigurationOption<ComponentType>
14+
directives?: boolean | ConfigurationOption<DirectiveType>
1115
BToast?: boolean | BToastPluginOptions
1216
}

packages/bootstrap-vue-next/src/types/exports/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type {
1111
ButtonType,
1212
ButtonVariant,
1313
ColorVariant,
14+
ConfigurationOption,
1415
ContainerPosition,
1516
InputType,
1617
LinkTarget,

packages/bootstrap-vue-next/src/types/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export type {BTableProvider} from './BTableProvider'
1212
export type {BTableProviderContext} from './BTableProviderContext'
1313
export type {BTableSortCompare} from './BTableSortCompare'
1414
export type {Booleanish} from './Booleanish'
15-
export type {BootstrapVueOptions} from './BootstrapVueOptions'
15+
export type {BootstrapVueOptions, ConfigurationOption} from './BootstrapVueOptions'
1616
export type {BreadcrumbItem} from './BreadcrumbItem'
1717
export type {BreadcrumbItemObject} from './BreadcrumbItemObject'
1818
export type {Breakpoint} from './Breakpoint'
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export {BvEvent, BvTriggerableEvent, BvCarouselEvent} from '..'
1+
export {BvEvent, BvTriggerableEvent, BvCarouselEvent, parseActiveImports} from '..'

packages/bootstrap-vue-next/src/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ export * from './stringUtils'
1717
export * from './keys'
1818
export {default as getSlotElements} from './getSlotElements'
1919
export * from './floatingUi'
20+
export {default as parseActiveImports} from './parseActiveImports'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type {ConfigurationOption} from '../types'
2+
3+
export default <Type extends string, Base extends ConfigurationOption<Type>>(
4+
options: Base,
5+
values: Type[]
6+
): Type[] => {
7+
const {all, ...others} = options
8+
const valuesCopy: Partial<Record<keyof Base, boolean>> = {}
9+
if (all) {
10+
values.forEach((el) => {
11+
valuesCopy[el] = all
12+
})
13+
}
14+
const merge: Record<string, boolean> = {...valuesCopy, ...others}
15+
return (
16+
Object.entries(merge)
17+
// filtering possible invalid keys
18+
.filter(([name, value]) => !!value && values.includes(name as Type))
19+
.map(([name]) => name as Type)
20+
)
21+
}

packages/nuxt/src/module.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import {defineNuxtModule, addPlugin, createResolver, addImports} from '@nuxt/kit'
1+
import {defineNuxtModule, createResolver, addImports} from '@nuxt/kit'
2+
import {parseActiveImports} from 'bootstrap-vue-next'
23
import type {Import} from 'unimport'
34
import useComponents from './composables/useComponents'
45
import type {ModuleOptions} from './types/ModuleOptions'
5-
import parseActiveImports from './utils/parseActiveImports'
66

77
export default defineNuxtModule<ModuleOptions>({
88
meta: {
@@ -31,10 +31,10 @@ export default defineNuxtModule<ModuleOptions>({
3131

3232
const arr: Import[] = []
3333
if (Object.values(normalizedComposables).some((el) => el === true)) {
34-
const imports = parseActiveImports(normalizedComposables, {
35-
useBreadcrumb: false,
36-
useColorMode: false,
37-
}).map(
34+
const imports = parseActiveImports(normalizedComposables, [
35+
'useBreadcrumb',
36+
'useColorMode',
37+
]).map(
3838
(el) =>
3939
({
4040
from: resolver.resolve('./runtime/composables'),
+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import type {Composables} from 'bootstrap-vue-next'
1+
import type {ConfigurationOption, Composables} from 'bootstrap-vue-next'
2+
3+
export type ComposableType = keyof typeof Composables
24

35
export interface ModuleOptions {
4-
composables: (Partial<Record<keyof typeof Composables, boolean>> & {all: boolean}) | boolean
6+
composables: ConfigurationOption<ComposableType> | boolean
57
}

packages/nuxt/src/utils/parseActiveImports.ts

-14
This file was deleted.

0 commit comments

Comments
 (0)