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(kit,nuxt): add component priority to allow overriding #19252

Merged
merged 7 commits into from
Mar 6, 2023
14 changes: 12 additions & 2 deletions packages/kit/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,24 @@ export async function addComponent (opts: AddComponentOptions) {
preload: false,
mode: 'all',
shortPath: opts.filePath,
priority: 0,
...opts
}

nuxt.hook('components:extend', (components: Component[]) => {
const existingComponent = components.find(c => (c.pascalName === component.pascalName || c.kebabName === component.kebabName) && c.mode === component.mode)
if (existingComponent) {
const name = existingComponent.pascalName || existingComponent.kebabName
console.warn(`Overriding ${name} component.`)
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
console.warn(`Overriding ${name} component. You can specify a \`priority\` option when calling \`addComponent\` to avoid this warning.`)
}
Object.assign(existingComponent, component)
} else {
components.push(component)
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/src/components/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr
kebabName,
chunkName,
shortPath,
export: 'default'
export: 'default',
// by default, give priority to scanned components
priority: 1
}

if (typeof dir.extendComponent === 'function') {
Expand Down
18 changes: 14 additions & 4 deletions packages/nuxt/test/scan-components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const expectedComponents = [
pascalName: 'Isle',
prefetch: false,
preload: false,
priority: 1,
shortPath: 'components/islands/Isle.vue'
},
{
Expand All @@ -109,6 +110,7 @@ const expectedComponents = [
pascalName: 'Glob',
prefetch: false,
preload: false,
priority: 1,
shortPath: 'components/global/Glob.vue'
},
{
Expand All @@ -121,7 +123,8 @@ const expectedComponents = [
global: undefined,
island: undefined,
prefetch: false,
preload: false
preload: false,
priority: 1
},
{
mode: 'client',
Expand All @@ -133,7 +136,8 @@ const expectedComponents = [
global: undefined,
island: undefined,
prefetch: false,
preload: false
preload: false,
priority: 1
},
{
mode: 'server',
Expand All @@ -145,7 +149,8 @@ const expectedComponents = [
global: undefined,
island: undefined,
prefetch: false,
preload: false
preload: false,
priority: 1
},
{
chunkName: 'components/client-component-with-props',
Expand All @@ -157,6 +162,7 @@ const expectedComponents = [
pascalName: 'ClientComponentWithProps',
prefetch: false,
preload: false,
priority: 1,
shortPath: 'components/client/ComponentWithProps.vue'
},
{
Expand All @@ -169,6 +175,7 @@ const expectedComponents = [
pascalName: 'ClientWithClientOnlySetup',
prefetch: false,
preload: false,
priority: 1,
shortPath: 'components/client/WithClientOnlySetup.vue'
},
{
Expand All @@ -181,7 +188,8 @@ const expectedComponents = [
global: undefined,
island: undefined,
prefetch: false,
preload: false
preload: false,
priority: 1
},
{
chunkName: 'components/some-glob',
Expand All @@ -193,6 +201,7 @@ const expectedComponents = [
pascalName: 'SomeGlob',
prefetch: false,
preload: false,
priority: 1,
shortPath: 'components/some-glob.global.vue'
},
{
Expand All @@ -205,6 +214,7 @@ const expectedComponents = [
pascalName: 'Some',
prefetch: false,
preload: false,
priority: 1,
shortPath: 'components/some.island.vue'
}
]
Expand Down
6 changes: 6 additions & 0 deletions packages/schema/src/types/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ export interface Component {
global?: boolean
island?: boolean
mode?: 'client' | 'server' | 'all'
/**
* This number allows configuring the behaviour of overriding Nuxt components.
danielroe marked this conversation as resolved.
Show resolved Hide resolved
* If multiple components are provided with the same name, then higher priority
* components will be used instead of lower priority components.
*/
priority?: number
}

export interface ScanDir {
Expand Down