Skip to content

Commit ef3dc13

Browse files
committed
feat: added function to register only some components
Treeshaking doesn't work if registering all components globally with the plugin. This allows more selective importing.
1 parent aa341db commit ef3dc13

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

src/main.lib.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,44 @@ import { capitalize } from "@alanscodelog/utils"
1212

1313
import * as components from "./components/index.js"
1414

15+
16+
/**
17+
* Register only specific components. Will register components as both snake case and pascal case.
18+
*
19+
* ```ts
20+
* import {SomeComponent} from "@alanscodelog/vue-components"
21+
* const components = { SomeComponent }
22+
* createApp(App)
23+
* .use({
24+
* install(app: typeof App) {
25+
* // will register optional-prefix-some-component and optionalPrefixSomeComponent
26+
* // note it doesn't matter what you call the component in the Object
27+
* // its name is set internally already
28+
* registerComponents(app, components, "optional-prefix-")
29+
* },
30+
* })
31+
* ```
32+
*/
33+
34+
// eslint-disable-next-line @typescript-eslint/no-shadow
35+
export const registerComponents = (app: App, components: Record<string, any>, prefix = ""): void => {
36+
for (const key of Object.keys(components)) {
37+
const component = (components as any)[key]
38+
const name = prefix + component.name
39+
const capitalizedname = name.split("-").map((_: string) => capitalize(_)).join("")
40+
app.component(name, component)
41+
app.component(capitalizedname, component)
42+
}
43+
}
44+
1545
// eslint-disable-next-line @typescript-eslint/naming-convention
1646
export const VueComponentsPlugin: Plugin = {
1747
install(app: App) {
18-
for (const componentName of Object.keys(components)) {
19-
const component = (components as any)[componentName]
20-
const capitalizedName = component.name.split("-").map((_: string) => capitalize(_)).join("")
21-
app.component(component.name, component)
22-
app.component(capitalizedName, component)
23-
}
48+
registerComponents(app, components)
2449
},
2550
}
2651

52+
2753
type Components = typeof components
2854

2955
/**

0 commit comments

Comments
 (0)