2023-04-07 16:02:47 +00:00
|
|
|
import { kebabCase, pascalCase } from 'scule'
|
|
|
|
import type { Component, ComponentsDir } from '@nuxt/schema'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { useNuxt } from './context'
|
2021-12-21 13:57:26 +00:00
|
|
|
import { assertNuxtCompatibility } from './compatibility'
|
2023-09-19 21:26:15 +00:00
|
|
|
import { logger } from './logger'
|
2021-11-21 16:14:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a directory to be scanned for components and imported only when used.
|
|
|
|
*
|
|
|
|
* Requires Nuxt 2.13+
|
|
|
|
*/
|
2023-11-23 21:13:03 +00:00
|
|
|
export async function addComponentsDir (dir: ComponentsDir, opts: { prepend?: boolean } = {}) {
|
2021-11-21 16:14:46 +00:00
|
|
|
const nuxt = useNuxt()
|
2021-12-21 13:57:26 +00:00
|
|
|
await assertNuxtCompatibility({ nuxt: '>=2.13' }, nuxt)
|
2021-11-21 16:14:46 +00:00
|
|
|
nuxt.options.components = nuxt.options.components || []
|
2023-08-29 22:06:41 +00:00
|
|
|
dir.priority ||= 0
|
2023-11-23 21:13:03 +00:00
|
|
|
nuxt.hook('components:dirs', (dirs) => { dirs[opts.prepend ? 'unshift' : 'push'](dir) })
|
2021-11-21 16:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type AddComponentOptions = { name: string, filePath: string } & Partial<Exclude<Component,
|
|
|
|
'shortPath' | 'async' | 'level' | 'import' | 'asyncImport'
|
|
|
|
>>
|
|
|
|
|
|
|
|
/**
|
2023-03-07 11:12:54 +00:00
|
|
|
* Register a component by its name and filePath.
|
2021-11-21 16:14:46 +00:00
|
|
|
*
|
|
|
|
* Requires Nuxt 2.13+
|
|
|
|
*/
|
2021-12-21 13:57:26 +00:00
|
|
|
export async function addComponent (opts: AddComponentOptions) {
|
2021-11-21 16:14:46 +00:00
|
|
|
const nuxt = useNuxt()
|
2021-12-21 13:57:26 +00:00
|
|
|
await assertNuxtCompatibility({ nuxt: '>=2.13' }, nuxt)
|
2021-11-21 16:14:46 +00:00
|
|
|
nuxt.options.components = nuxt.options.components || []
|
|
|
|
|
2023-12-20 10:52:14 +00:00
|
|
|
if (!opts.mode) {
|
|
|
|
const [, mode = 'all'] = opts.filePath.match(/\.(server|client)(\.\w+)*$/) || []
|
|
|
|
opts.mode = mode as 'all' | 'client' | 'server'
|
|
|
|
}
|
|
|
|
|
2021-11-21 16:14:46 +00:00
|
|
|
// Apply defaults
|
|
|
|
const component: Component = {
|
|
|
|
export: opts.export || 'default',
|
|
|
|
chunkName: 'components/' + kebabCase(opts.name),
|
|
|
|
global: opts.global ?? false,
|
|
|
|
kebabName: kebabCase(opts.name || ''),
|
|
|
|
pascalName: pascalCase(opts.name || ''),
|
|
|
|
prefetch: false,
|
|
|
|
preload: false,
|
2022-04-19 19:13:55 +00:00
|
|
|
mode: 'all',
|
2021-11-21 16:14:46 +00:00
|
|
|
shortPath: opts.filePath,
|
2023-03-06 11:33:40 +00:00
|
|
|
priority: 0,
|
2021-11-21 16:14:46 +00:00
|
|
|
...opts
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt.hook('components:extend', (components: Component[]) => {
|
2024-02-14 11:48:25 +00:00
|
|
|
const existingComponentIndex = components.findIndex(c => (c.pascalName === component.pascalName || c.kebabName === component.kebabName) && c.mode === component.mode)
|
|
|
|
if (existingComponentIndex !== -1) {
|
|
|
|
const existingComponent = components[existingComponentIndex]
|
2023-03-06 11:33:40 +00:00
|
|
|
const existingPriority = existingComponent.priority ?? 0
|
|
|
|
const newPriority = component.priority ?? 0
|
|
|
|
|
|
|
|
if (newPriority < existingPriority) { return }
|
|
|
|
|
|
|
|
// We override where new component priority is equal or higher
|
|
|
|
// but we warn if they are equal.
|
|
|
|
if (newPriority === existingPriority) {
|
|
|
|
const name = existingComponent.pascalName || existingComponent.kebabName
|
2023-09-19 21:26:15 +00:00
|
|
|
logger.warn(`Overriding ${name} component. You can specify a \`priority\` option when calling \`addComponent\` to avoid this warning.`)
|
2023-03-06 11:33:40 +00:00
|
|
|
}
|
2024-02-14 11:48:25 +00:00
|
|
|
components.splice(existingComponentIndex, 1, component)
|
2021-11-21 16:14:46 +00:00
|
|
|
} else {
|
|
|
|
components.push(component)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|