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

Code before:
      return [
        '/types/imports.d.ts',
        '/imports.d.ts',
        '/imports.mjs',
      ].some(i => template.filename.endsWith(i))
    }

    const regenerateImports = async () => {
      await ctx.modifyDynamicImports(async (imports) => {
        // Clear old imports
        imports.length = 0

        // Scan for `composables/` and `utils/` directories
        if (options.scan) {
          const scannedImports = await scanDirExports(composablesDirs, {
            fileFilter: file => !isIgnored(file),
          })

          const presetMap = new Map<string, string>()

          for (const preset of presets) {
            preset.imports = preset.imports ?? []
            for (const i of preset.imports) {
              presetMap.set(i, preset.from)
            }
            presetMap.set(preset.as, preset.from)
          }

          for (const i of scannedImports) {
            const name = i.as ?? i.name
            const preset = presetMap.get(name)

            if (preset) {
              console.warn(`[imports] "${name}" is already defined and auto imported from "${preset ?? 'unknown preset'}" within nuxt itself. Please consider renaming "${name}" at ${i.from}.`)
            }

            i.priority = i.priority || priorities.find(([dir]) => i.from.startsWith(dir))?.[1]
          }
          imports.push(...scannedImports)
        }

        // Modules extending
        await nuxt.callHook('imports:extend', imports)
        return imports
      })

      await updateTemplates({
        filter: isImportsTemplate,
      })
    }


Review comment (suggestion):
the previous implementation has type issues. types are weired on the presets. I tried to solve it like this, but seems kinda hacky
```suggestion
            for (const i of preset.imports) {
              if(typeof i === "string") {
                presetMap.set(i, preset.from)
                continue
              }
              let name, as, from
              if(Array.isArray(i)) {
                [name, as, from] = i
              }
              else {
                name = ('name' in i) ? i.name : undefined
                as = ('as' in i) ? i.as : undefined
                from = preset.from
              }
              if(!name || !from) {
                continue
              }
              presetMap.set(as ?? name, from)
            }
```


Code after:
          logger.info(`Directory \`${relativePath}/\` ${event === 'addDir' ? 'created' : 'removed'}`)
          return nuxt.callHook('restart')
        }
      })
    }

    // Support for importing from '#imports'
    addTemplate({
      filename: 'imports.mjs',
      getContents: async () => toExports(await ctx.getImports()) + '\nif (import.meta.dev) { console.warn("[nuxt] `#imports` should be transformed with real imports. There seems to be something wrong with the imports plugin.") }',
    })
    nuxt.options.alias['#imports'] = join(nuxt.options.buildDir, 'imports')

    // Transform to inject imports in production mode
    addBuildPlugin(TransformPlugin({ ctx, options, sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client }))

    const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length)

    const IMPORTS_TEMPLATE_RE = /\/imports\.(?:d\.ts|mjs)$/
    function isImportsTemplate (template: ResolvedNuxtTemplate) {
      return IMPORTS_TEMPLATE_RE.test(template.filename)
    }

    const isIgnored = createIsIgnored(nuxt)
    const defaultImportSources = new Set(defaultPresets.flatMap(i => i.from))
    const defaultImports = new Set(presets.flatMap(p => defaultImportSources.has(p.from) ? p.imports : []))
    const regenerateImports = async () => {
      await ctx.modifyDynamicImports(async (imports) => {
        // Clear old imports
        imports.length = 0

        // Scan for `composables/` and `utils/` directories
        if (options.scan) {
          const scannedImports = await scanDirExports(composablesDirs, {
            fileFilter: file => !isIgnored(file),
          })
          for (const i of scannedImports) {
            i.priority ||= priorities.find(([dir]) => i.from.startsWith(dir))?.[1]
          }
          imports.push(...scannedImports)
        }

        // Modules extending
        await nuxt.callHook('imports:extend', imports)
        for (const i of imports) {
          if (!defaultImportSources.has(i.from)) {
            const value = i.as || i.name
            if (defaultImports.has(value) && (!i.priority || i.priority >= 0 /* default priority */)) {
              const relativePath = isAbsolute(i.from) ? `~/${relative(nuxt.options.srcDir, i.from)}` : i.from
              logger.error(`\`${value}\` is an auto-imported function that is in use by Nuxt. Overriding it will likely cause issues. Please consider renaming \`${value}\` in \`${relativePath}\`.`)
            }