From 31de18100cab56827f3a3cf5b711c42c3c603a1a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 13 Sep 2022 11:57:14 +0100 Subject: [PATCH] perf(nuxt): only inject preload helper when webpack is used (#7460) --- packages/nuxt/src/core/nuxt.ts | 5 +++++ packages/nuxt/src/core/templates.ts | 4 ++-- packages/schema/src/config/build.ts | 2 +- packages/schema/src/types/config.ts | 4 +++- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index de2f18acc4..74cc1fb793 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -176,6 +176,11 @@ async function initNuxt (nuxt: Nuxt) { // Add prerender payload support addPlugin(resolve(nuxt.options.appDir, 'plugins/payload.client')) + // Track components used to render for webpack + if (nuxt.options.builder === '@nuxt/webpack-builder') { + addPlugin(resolve(nuxt.options.appDir, 'plugins/preload.server')) + } + for (const m of modulesToInstall) { if (Array.isArray(m)) { await installModule(m[0], m[1]) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index 2223c6a2cd..3dbf745dee 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -68,8 +68,8 @@ export const serverPluginTemplate: NuxtTemplate = { filename: 'plugins/server.mjs', getContents (ctx) { const serverPlugins = ctx.app.plugins.filter(p => !p.mode || p.mode !== 'client') - const exports: string[] = ['preload'] - const imports: string[] = ["import preload from '#app/plugins/preload.server'"] + const exports: string[] = [] + const imports: string[] = [] for (const plugin of serverPlugins) { const path = relative(ctx.nuxt.options.rootDir, plugin.src) const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path) diff --git a/packages/schema/src/config/build.ts b/packages/schema/src/config/build.ts index 42e7bc9bba..0cd808958a 100644 --- a/packages/schema/src/config/build.ts +++ b/packages/schema/src/config/build.ts @@ -20,7 +20,7 @@ export default defineUntypedSchema({ vite: '@nuxt/vite-builder', webpack: '@nuxt/webpack-builder', } - return map[val] || (await get('vite') === false ? map.webpack : map.vite) + return map[val] || val || (await get('vite') === false ? map.webpack : map.vite) } }, /** diff --git a/packages/schema/src/types/config.ts b/packages/schema/src/types/config.ts index 0f8d67fb21..51047eb9af 100644 --- a/packages/schema/src/types/config.ts +++ b/packages/schema/src/types/config.ts @@ -3,6 +3,7 @@ import { ConfigSchema } from '../../schema/config' import type { UserConfig as ViteUserConfig } from 'vite' import type { Options as VuePluginOptions } from '@vitejs/plugin-vue' import type { MetaObject } from './meta' +import type { Nuxt } from './nuxt' type DeepPartial = T extends Function ? T : T extends Record ? { [P in keyof T]?: DeepPartial } : T @@ -25,8 +26,9 @@ export type NuxtConfigLayer = ConfigLayer /** Normalized Nuxt options available as `nuxt.options.*` */ -export interface NuxtOptions extends ConfigSchema { +export interface NuxtOptions extends Omit { sourcemap: Required> + builder: '@nuxt/vite-builder' | '@nuxt/webpack-builder' | { bundle: (nuxt: Nuxt) => Promise } _layers: NuxtConfigLayer[] }