Repository: nuxt/nuxt
File: packages/nuxt/src/components/module.ts (TypeScript)

Code before:
  },
  async setup (moduleOptions, nuxt) {
    let componentDirs: ComponentsDir[] = []
    const context = {
      components: [] as Component[],
    }

    const getComponents: getComponentsT = (mode) => {
      return (mode && mode !== 'all')
        ? context.components.filter(c => c.mode === mode || c.mode === 'all' || (c.mode === 'server' && !context.components.some(otherComponent => otherComponent.mode !== 'server' && otherComponent.pascalName === c.pascalName)))
        : context.components
    }

    // TODO: remove in Nuxt v5
    if (nuxt.options.experimental.normalizeComponentNames) {
      addBuildPlugin(ComponentNamePlugin({ sourcemap: !!nuxt.options.sourcemap.client, getComponents }), { server: false })
      addBuildPlugin(ComponentNamePlugin({ sourcemap: !!nuxt.options.sourcemap.server, getComponents }), { client: false })
    }

    // Resolve dirs
    nuxt.hook('app:resolve', async () => {
      // components/ dirs from all layers
      const allDirs: ComponentsDir[] = []
      const layerCount = nuxt.options._layers.length
      for (let i = 0; i < layerCount; i++) {
        const layer = nuxt.options._layers[i]!
        // Assign priority based on layer position: lower index = higher priority
        // This ensures correct override order: root > auto-scanned > extends layers
        const priority = layerCount - i
        const layerDirs = normalizeDirs(layer.config.components, layer.config.srcDir, { priority })
        allDirs.push(...layerDirs)
      }

      await nuxt.callHook('components:dirs', allDirs)

      const userComponentDirs: ComponentsDir[] = []
      const libraryComponentDirs: ComponentsDir[] = []

      for (const dir of allDirs) {
        if (!isPureObjectOrString(dir)) {
          continue
        }

        const dirOptions = typeof dir === 'object' ? dir : { path: dir }
        const dirPath = resolveAlias(dirOptions.path)
        const extensions = (dirOptions.extensions || nuxt.options.extensions).map(e => e.replace(STARTER_DOT_RE, ''))
        const _transpile = typeof dirOptions.transpile === 'boolean' ? dirOptions.transpile : 'auto'
        const transpile = _transpile === 'auto' ? dirPath.includes('node_modules') : _transpile
        if (transpile) {
          nuxt.options.build.transpile.push(dirPath)
        }

Review comment (suggestion):
```suggestion
      for (const [i, layer] of nuxt.options._layers.entries()) {
```

Code after:
  },
  async setup (moduleOptions, nuxt) {
    let componentDirs: ComponentsDir[] = []
    const context = {
      components: [] as Component[],
    }

    const getComponents: getComponentsT = (mode) => {
      return (mode && mode !== 'all')
        ? context.components.filter(c => c.mode === mode || c.mode === 'all' || (c.mode === 'server' && !context.components.some(otherComponent => otherComponent.mode !== 'server' && otherComponent.pascalName === c.pascalName)))
        : context.components
    }

    // TODO: remove in Nuxt v5
    if (nuxt.options.experimental.normalizeComponentNames) {
      addBuildPlugin(ComponentNamePlugin({ sourcemap: !!nuxt.options.sourcemap.client, getComponents }), { server: false })
      addBuildPlugin(ComponentNamePlugin({ sourcemap: !!nuxt.options.sourcemap.server, getComponents }), { client: false })
    }

    // Resolve dirs
    nuxt.hook('app:resolve', async () => {
      // components/ dirs from all layers
      const allDirs: ComponentsDir[] = []
      const layerCount = nuxt.options._layers.length
      for (const [i, layer] of nuxt.options._layers.entries()) {
        // Assign priority based on layer position: lower index = higher priority
        // This ensures correct override order: root > auto-scanned > extends layers
        const priority = layerCount - i
        const layerDirs = normalizeDirs(layer.config.components, layer.config.srcDir, { priority })
        allDirs.push(...layerDirs)
      }

      await nuxt.callHook('components:dirs', allDirs)

      const userComponentDirs: ComponentsDir[] = []
      const libraryComponentDirs: ComponentsDir[] = []

      for (const dir of allDirs) {
        if (!isPureObjectOrString(dir)) {
          continue
        }

        const dirOptions = typeof dir === 'object' ? dir : { path: dir }
        const dirPath = resolveAlias(dirOptions.path)
        const extensions = (dirOptions.extensions || nuxt.options.extensions).map(e => e.replace(STARTER_DOT_RE, ''))
        const _transpile = typeof dirOptions.transpile === 'boolean' ? dirOptions.transpile : 'auto'
        const transpile = _transpile === 'auto' ? dirPath.includes('node_modules') : _transpile
        if (transpile) {
          nuxt.options.build.transpile.push(dirPath)
        }