Skip to content

Commit

Permalink
fix(kit): respect priority when registering extra dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Aug 29, 2023
1 parent d75e906 commit 865c288
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/kit/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function addComponentsDir (dir: ComponentsDir) {
const nuxt = useNuxt()
await assertNuxtCompatibility({ nuxt: '>=2.13' }, nuxt)
nuxt.options.components = nuxt.options.components || []
dir.priority ||= 0
nuxt.hook('components:dirs', (dirs) => { dirs.push(dir) })
}

Expand Down
12 changes: 9 additions & 3 deletions packages/nuxt/src/components/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr
shortPath,
export: 'default',
// by default, give priority to scanned components
priority: 1
priority: dir.priority ?? 1
}

if (typeof dir.extendComponent === 'function') {
Expand All @@ -135,9 +135,15 @@ export async function scanComponents (dirs: ComponentsDir[], srcDir: string): Pr
}

const existingComponent = components.find(c => c.pascalName === component.pascalName && ['all', component.mode].includes(c.mode))
// Ignore component if component is already defined (with same mode)
if (existingComponent) {
// Ignore component if component is already defined (with same mode)
warnAboutDuplicateComponent(componentName, filePath, existingComponent.filePath)
const existingPriority = existingComponent.priority ?? 0
const newPriority = component.priority ?? 0

if (newPriority >= existingPriority) {
warnAboutDuplicateComponent(componentName, filePath, existingComponent.filePath)
}

continue
}

Expand Down
8 changes: 8 additions & 0 deletions packages/schema/src/types/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ export interface ComponentsDir extends ScanDir {
* By default ('auto') it will set transpile: true if node_modules/ is in path.
*/
transpile?: 'auto' | boolean
/**
* This number allows configuring the behavior of overriding Nuxt components.
* It will be inherited by any components within the directory.
*
* 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 ComponentsOptions {
Expand Down

0 comments on commit 865c288

Please sign in to comment.