2021-11-21 16:14:46 +00:00
|
|
|
import { pascalCase, kebabCase } from 'scule'
|
|
|
|
import type { ComponentsDir, Component } from '@nuxt/schema'
|
2022-02-07 13:45:47 +00:00
|
|
|
import { genDynamicImport } from 'knitwork'
|
2021-11-21 16:14:46 +00:00
|
|
|
import { useNuxt } from './context'
|
2021-12-21 13:57:26 +00:00
|
|
|
import { assertNuxtCompatibility } from './compatibility'
|
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+
|
|
|
|
*/
|
2021-12-21 13:57:26 +00:00
|
|
|
export async function addComponentsDir (dir: ComponentsDir) {
|
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 || []
|
|
|
|
nuxt.hook('components:dirs', (dirs) => { dirs.push(dir) })
|
|
|
|
}
|
|
|
|
|
|
|
|
export type AddComponentOptions = { name: string, filePath: string } & Partial<Exclude<Component,
|
|
|
|
'shortPath' | 'async' | 'level' | 'import' | 'asyncImport'
|
|
|
|
>>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a directory to be scanned for components and imported only when used.
|
|
|
|
*
|
|
|
|
* 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 || []
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Nuxt 2 support
|
|
|
|
shortPath: opts.filePath,
|
|
|
|
async: false,
|
|
|
|
level: 0,
|
2022-02-07 13:45:47 +00:00
|
|
|
asyncImport: `${genDynamicImport(opts.filePath)}.then(r => r['${opts.export || 'default'}'])`,
|
|
|
|
import: `require(${JSON.stringify(opts.filePath)})['${opts.export || 'default'}']`,
|
2021-11-21 16:14:46 +00:00
|
|
|
|
|
|
|
...opts
|
|
|
|
}
|
|
|
|
|
|
|
|
nuxt.hook('components:extend', (components: Component[]) => {
|
2022-07-01 10:02:34 +00:00
|
|
|
const existingComponent = components.find(c => (c.pascalName === component.pascalName || c.kebabName === component.kebabName) && c.mode === component.mode)
|
2021-11-21 16:14:46 +00:00
|
|
|
if (existingComponent) {
|
|
|
|
const name = existingComponent.pascalName || existingComponent.kebabName
|
|
|
|
console.warn(`Overriding ${name} component.`)
|
|
|
|
Object.assign(existingComponent, component)
|
|
|
|
} else {
|
|
|
|
components.push(component)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|