fix(nuxt): do not apply import protection to top-level resolution (#7344)

This commit is contained in:
Daniel Roe 2022-09-10 12:51:17 +01:00 committed by GitHub
parent 83949c3734
commit 9abc7a2122
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { normalize, resolve } from 'pathe' import { join, normalize, resolve } from 'pathe'
import { createHooks } from 'hookable' import { createHooks } from 'hookable'
import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema' import type { Nuxt, NuxtOptions, NuxtConfig, ModuleContainer, NuxtHooks } from '@nuxt/schema'
import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule } from '@nuxt/kit' import { loadNuxtConfig, LoadNuxtOptions, nuxtCtx, installModule, addComponent, addVitePlugin, addWebpackPlugin, tryResolveModule } from '@nuxt/kit'
@ -62,6 +62,8 @@ async function initNuxt (nuxt: Nuxt) {
// Add import protection // Add import protection
const config = { const config = {
rootDir: nuxt.options.rootDir, rootDir: nuxt.options.rootDir,
// Exclude top-level resolutions by plugins
exclude: [join(nuxt.options.rootDir, 'index.html')],
patterns: vueAppPatterns(nuxt) patterns: vueAppPatterns(nuxt)
} }
addVitePlugin(ImportProtectionPlugin.vite(config)) addVitePlugin(ImportProtectionPlugin.vite(config))

View File

@ -1,7 +1,7 @@
import { createRequire } from 'node:module' import { createRequire } from 'node:module'
import { createUnplugin } from 'unplugin' import { createUnplugin } from 'unplugin'
import { logger } from '@nuxt/kit' 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 type { Nuxt } from '@nuxt/schema'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
@ -32,6 +32,12 @@ export const ImportProtectionPlugin = createUnplugin(function (options: ImportPr
enforce: 'pre', enforce: 'pre',
resolveId (id, importer) { resolveId (id, importer) {
if (!importer) { return } 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 } 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) const invalidImports = options.patterns.filter(([pattern]) => pattern instanceof RegExp ? pattern.test(id) : pattern === id)