diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 111e12fff8..d65d972bed 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -1,4 +1,4 @@ -import { normalize, resolve } from 'pathe' +import { join, normalize, resolve } from 'pathe' import { createHooks } from 'hookable' import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema' import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule } from '@nuxt/kit' @@ -62,6 +62,8 @@ async function initNuxt (nuxt: Nuxt) { // Add import protection const config = { rootDir: nuxt.options.rootDir, + // Exclude top-level resolutions by plugins + exclude: [join(nuxt.options.rootDir, 'index.html')], patterns: vueAppPatterns(nuxt) } addVitePlugin(ImportProtectionPlugin.vite(config)) diff --git a/packages/nuxt/src/core/plugins/import-protection.ts b/packages/nuxt/src/core/plugins/import-protection.ts index 493d5b7a83..f0f2492b1e 100644 --- a/packages/nuxt/src/core/plugins/import-protection.ts +++ b/packages/nuxt/src/core/plugins/import-protection.ts @@ -1,7 +1,7 @@ import { createRequire } from 'node:module' import { createUnplugin } from 'unplugin' import { logger } from '@nuxt/kit' -import { isAbsolute, relative, resolve } from 'pathe' +import { isAbsolute, join, relative, resolve } from 'pathe' import type { Nuxt } from '@nuxt/schema' import escapeRE from 'escape-string-regexp' @@ -32,6 +32,12 @@ export const ImportProtectionPlugin = createUnplugin(function (options: ImportPr enforce: 'pre', resolveId (id, importer) { if (!importer) { return } + if (id.startsWith('.')) { + id = join(importer, '..', id) + } + if (isAbsolute(id)) { + id = relative(options.rootDir, id) + } if (importersToExclude.some(p => typeof p === 'string' ? importer === p : p.test(importer))) { return } const invalidImports = options.patterns.filter(([pattern]) => pattern instanceof RegExp ? pattern.test(id) : pattern === id)