From b62f1fe60d37dd188c84cfd11cdb417b3dc61702 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 12 Jul 2022 10:10:22 +0100 Subject: [PATCH 1/3] refactor(nuxt): add import protection to nitro config --- packages/nuxt/src/core/nitro.ts | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 1d67b635246..809a8dee8b4 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -106,6 +106,16 @@ export async function initNitro (nuxt: Nuxt) { nitroConfig.virtual['#build/dist/server/server.mjs'] = 'export default () => {}' } + // Register nuxt protection patterns + const plugin = ImportProtectionPlugin.rollup({ + rootDir: nuxt.options.rootDir, + patterns: [ + ...['#app', /^#build(\/|$)/] + .map(p => [p, 'Vue app aliases are not allowed in server routes.']) as [RegExp | string, string][] + ] + }) + nitroConfig.rollupConfig.plugins.push(plugin) + // Extend nitro config with hook await nuxt.callHook('nitro:config', nitroConfig) @@ -121,18 +131,6 @@ export async function initNitro (nuxt: Nuxt) { // Connect hooks nuxt.hook('close', () => nitro.hooks.callHook('close')) - // Register nuxt protection patterns - nitro.hooks.hook('rollup:before', (nitro) => { - const plugin = ImportProtectionPlugin.rollup({ - rootDir: nuxt.options.rootDir, - patterns: [ - ...['#app', /^#build(\/|$)/] - .map(p => [p, 'Vue app aliases are not allowed in server routes.']) as [RegExp | string, string][] - ] - }) - nitro.options.rollupConfig.plugins.push(plugin) - }) - // Setup handlers const devMidlewareHandler = dynamicEventHandler() nitro.options.devHandlers.unshift({ handler: devMidlewareHandler }) From 484714a83240000d78c15d47c33ccd2a8e3532da Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 12 Jul 2022 10:12:24 +0100 Subject: [PATCH 2/3] style: remove separate variable for plugin --- packages/nuxt/src/core/nitro.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 809a8dee8b4..4fe4b46e371 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -107,14 +107,13 @@ export async function initNitro (nuxt: Nuxt) { } // Register nuxt protection patterns - const plugin = ImportProtectionPlugin.rollup({ + nitroConfig.rollupConfig.plugins.push(ImportProtectionPlugin.rollup({ rootDir: nuxt.options.rootDir, patterns: [ ...['#app', /^#build(\/|$)/] .map(p => [p, 'Vue app aliases are not allowed in server routes.']) as [RegExp | string, string][] ] - }) - nitroConfig.rollupConfig.plugins.push(plugin) + })) // Extend nitro config with hook await nuxt.callHook('nitro:config', nitroConfig) From a0bda70fd62b33b0e38c297b60c9399ca8cb1a75 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 12 Jul 2022 10:37:24 +0100 Subject: [PATCH 3/3] fix: exclude nitro renderer from import protection --- packages/nuxt/src/core/nitro.ts | 3 ++- packages/nuxt/src/core/plugins/import-protection.ts | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/nuxt/src/core/nitro.ts b/packages/nuxt/src/core/nitro.ts index 4fe4b46e371..f1e383a54b3 100644 --- a/packages/nuxt/src/core/nitro.ts +++ b/packages/nuxt/src/core/nitro.ts @@ -112,7 +112,8 @@ export async function initNitro (nuxt: Nuxt) { patterns: [ ...['#app', /^#build(\/|$)/] .map(p => [p, 'Vue app aliases are not allowed in server routes.']) as [RegExp | string, string][] - ] + ], + exclude: [/core[\\/]runtime[\\/]nitro[\\/]renderer/] })) // Extend nitro config with hook diff --git a/packages/nuxt/src/core/plugins/import-protection.ts b/packages/nuxt/src/core/plugins/import-protection.ts index 6237a579c6e..3826ff6b6bf 100644 --- a/packages/nuxt/src/core/plugins/import-protection.ts +++ b/packages/nuxt/src/core/plugins/import-protection.ts @@ -10,6 +10,7 @@ const _require = createRequire(import.meta.url) interface ImportProtectionOptions { rootDir: string patterns: [importPattern: string | RegExp, warning?: string][] + exclude?: Array } export const vueAppPatterns = (nuxt: Nuxt) => [ @@ -25,10 +26,13 @@ export const vueAppPatterns = (nuxt: Nuxt) => [ export const ImportProtectionPlugin = createUnplugin(function (options: ImportProtectionOptions) { const cache: Record> = {} + const importersToExclude = options?.exclude || [] return { name: 'nuxt:import-protection', enforce: 'pre', resolveId (id, importer) { + 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) let matched: boolean for (const match of invalidImports) {