perf(nuxt): only inject preload helper when webpack is used (#7460)

This commit is contained in:
Daniel Roe 2022-09-13 11:57:14 +01:00 committed by GitHub
parent 1be5e4170b
commit 31de18100c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 11 additions and 4 deletions

View File

@ -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])

View File

@ -68,8 +68,8 @@ export const serverPluginTemplate: NuxtTemplate<TemplateContext> = {
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)

View File

@ -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)
}
},
/**

View File

@ -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> = T extends Function ? T : T extends Record<string, any> ? { [P in keyof T]?: DeepPartial<T[P]> } : T
@ -25,8 +26,9 @@ export type NuxtConfigLayer = ConfigLayer<NuxtConfig & {
}>
/** Normalized Nuxt options available as `nuxt.options.*` */
export interface NuxtOptions extends ConfigSchema {
export interface NuxtOptions extends Omit<ConfigSchema, 'builder'> {
sourcemap: Required<Exclude<ConfigSchema['sourcemap'], boolean>>
builder: '@nuxt/vite-builder' | '@nuxt/webpack-builder' | { bundle: (nuxt: Nuxt) => Promise<void> }
_layers: NuxtConfigLayer[]
}