Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions packages/nuxt/src/imports/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ import { TransformPlugin } from './transform'
import { appCompatPresets, defaultPresets } from './presets'
import type { ImportPresetWithDeprecation, ImportsOptions, ResolvedNuxtTemplate } from 'nuxt/schema'

import { pagesImportPresets, routeRulesPresets } from '../pages/module'

const allNuxtPresets = [
...pagesImportPresets,
...routeRulesPresets,
...defaultPresets,
]

export default defineNuxtModule<Partial<ImportsOptions>>({
meta: {
name: 'nuxt:imports',
Expand Down Expand Up @@ -122,8 +130,8 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
}

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 nuxtImportSources = new Set(allNuxtPresets.flatMap(i => i.from))
const nuxtImports = new Set(presets.flatMap(p => nuxtImportSources.has(p.from) ? p.imports : []))
const regenerateImports = async () => {
Comment on lines 132 to 135
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Normalise names and include per-import from to avoid false negatives

nuxtImports currently stores raw p.imports (strings, tuples, or objects). Later you .has(value: string), which won’t match tuples/objects. Also, some presets specify from per import; those sources should be included in the “Nuxt sources” set.

Apply the following diff and helper:

-    const nuxtImportSources = new Set(allNuxtPresets.flatMap(i => i.from))
-    const nuxtImports = new Set(presets.flatMap(p => nuxtImportSources.has(p.from) ? p.imports : []))
+    const nuxtImportSources = new Set(
+      allNuxtPresets
+        .flatMap(p => [
+          p.from,
+          ...p.imports.map(i => (typeof i === 'object' && !Array.isArray(i) && i?.from) ? i.from : undefined),
+        ])
+        .filter(Boolean) as string[],
+    )
+    const nuxtImportNames = new Set<string>(
+      presets.flatMap(p =>
+        p.imports
+          .map(i => {
+            const itemFrom = (typeof i === 'object' && !Array.isArray(i) && i?.from) ? i.from : p.from
+            return itemFrom && nuxtImportSources.has(itemFrom) ? normaliseImportName(i) : undefined
+          })
+          .filter(Boolean) as string[],
+      ),
+    )

Place this helper near the top of the file:

function normaliseImportName(i: unknown): string | undefined {
  if (typeof i === 'string') { return i }
  if (Array.isArray(i)) { return (i[1] as string | undefined) || (i[0] as string | undefined) }
  if (i && typeof i === 'object' && 'name' in (i as any)) {
    const o = i as { name?: string; as?: string }
    return o.as || o.name
  }
  return undefined
}
🤖 Prompt for AI Agents
In packages/nuxt/src/imports/module.ts around lines 132-135, nuxtImports is
built from raw p.imports which may be strings, tuples, or objects causing .has
checks to miss matches and also ignores per-import from overrides; add the
provided normaliseImportName helper near the top of the file, expand
nuxtImportSources to include any per-import `from` values found on individual
imports, and rebuild nuxtImports by normalising each import entry (using
normaliseImportName) to a string before adding to the Set so tuple/object
imports match string checks later.

await ctx.modifyDynamicImports(async (imports) => {
// Clear old imports
Expand All @@ -143,9 +151,9 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
// Modules extending
await nuxt.callHook('imports:extend', imports)
for (const i of imports) {
if (!defaultImportSources.has(i.from)) {
if (!nuxtImportSources.has(i.from)) {
const value = i.as || i.name
if (defaultImports.has(value) && (!i.priority || i.priority >= 0 /* default priority */)) {
if (nuxtImports.has(value) && (!i.priority || i.priority >= 0 /* default priority */)) {
const relativePath = isAbsolute(i.from) ? `${resolveToAlias(i.from, nuxt)}` : 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}\`.`)
}
Expand Down
19 changes: 13 additions & 6 deletions packages/nuxt/src/pages/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@ import { globRouteRulesFromPages, removePagesRules } from './route-rules'
import { PageMetaPlugin } from './plugins/page-meta'
import { RouteInjectionPlugin } from './plugins/route-injection'
import type { Nuxt, NuxtPage } from 'nuxt/schema'
import type { InlinePreset } from 'unimport'

const OPTIONAL_PARAM_RE = /^\/?:.*(?:\?|\(\.\*\)\*)$/

const runtimeDir = resolve(distDir, 'pages/runtime')

export const pagesImportPresets: InlinePreset[] = [
{ imports: ['definePageMeta'], from: resolve(runtimeDir, 'composables') },
{ imports: ['useLink'], from: 'vue-router' },
]

export const routeRulesPresets: InlinePreset[] = [
{ imports: ['defineRouteRules'], from: resolve(runtimeDir, 'composables') },
]

async function resolveRouterOptions (nuxt: Nuxt, builtInRouterOptions: string) {
const context = {
files: [] as Array<{ path: string, optional?: boolean }>,
Expand Down Expand Up @@ -418,13 +428,10 @@ export default defineNuxtModule({
nitro.options.prerender.routes = Array.from(prerenderRoutes)
})

nuxt.hook('imports:extend', (imports) => {
imports.push(
{ name: 'definePageMeta', as: 'definePageMeta', from: resolve(runtimeDir, 'composables') },
{ name: 'useLink', as: 'useLink', from: 'vue-router' },
)
nuxt.hook('imports:sources', (sources) => {
sources.push(...pagesImportPresets)
if (nuxt.options.experimental.inlineRouteRules) {
imports.push({ name: 'defineRouteRules', as: 'defineRouteRules', from: resolve(runtimeDir, 'composables') })
sources.push(...routeRulesPresets)
}
})

Expand Down
Loading