perf(nuxt): iterate rather than using Object.fromEntries (#24953)

This commit is contained in:
Michael Brevard 2024-01-09 11:56:40 +02:00 committed by GitHub
parent 4168157214
commit 3b94883414
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 26 deletions

View File

@ -260,16 +260,17 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
const _routeRules = nitro.options.routeRules const _routeRules = nitro.options.routeRules
for (const key in _routeRules) { for (const key in _routeRules) {
if (key === '/__nuxt_error') { continue } if (key === '/__nuxt_error') { continue }
const filteredRules = Object.entries(_routeRules[key]) let hasRules = false
.filter(([key, value]) => ['prerender', 'redirect'].includes(key) && value) const filteredRules = {} as Record<string, any>
.map(([key, value]: any) => { for (const routeKey in _routeRules[key]) {
if (key === 'redirect') { const value = (_routeRules as any)[key][routeKey]
return [key, typeof value === 'string' ? value : value.to] if (['prerender', 'redirect'].includes(routeKey) && value) {
} filteredRules[routeKey] = routeKey === 'redirect' ? typeof value === 'string' ? value : value.to : value
return [key, value] hasRules = true
}) }
if (filteredRules.length > 0) { }
routeRules[key] = Object.fromEntries(filteredRules) if (hasRules) {
routeRules[key] = filteredRules
} }
} }

View File

@ -17,22 +17,23 @@ const ALIAS_RE = /(?<=['"])[~@]{1,2}(?=\/)/g
const ALIAS_RE_SINGLE = /(?<=['"])[~@]{1,2}(?=\/)/ const ALIAS_RE_SINGLE = /(?<=['"])[~@]{1,2}(?=\/)/
export const LayerAliasingPlugin = createUnplugin((options: LayerAliasingOptions) => { export const LayerAliasingPlugin = createUnplugin((options: LayerAliasingOptions) => {
const aliases = Object.fromEntries(options.layers.map((l) => { const aliases: Record<string, { aliases: Record<string, string>, prefix: string, publicDir: false | string }> = {}
const srcDir = l.config.srcDir || l.cwd for (const layer of options.layers) {
const rootDir = l.config.rootDir || l.cwd const srcDir = layer.config.srcDir || layer.cwd
const publicDir = join(srcDir, l.config?.dir?.public || 'public') const rootDir = layer.config.rootDir || layer.cwd
const publicDir = join(srcDir, layer.config?.dir?.public || 'public')
return [srcDir, { aliases[srcDir] = {
aliases: { aliases: {
'~': l.config?.alias?.['~'] || srcDir, '~': layer.config?.alias?.['~'] || srcDir,
'@': l.config?.alias?.['@'] || srcDir, '@': layer.config?.alias?.['@'] || srcDir,
'~~': l.config?.alias?.['~~'] || rootDir, '~~': layer.config?.alias?.['~~'] || rootDir,
'@@': l.config?.alias?.['@@'] || rootDir '@@': layer.config?.alias?.['@@'] || rootDir
}, },
prefix: relative(options.root, publicDir), prefix: relative(options.root, publicDir),
publicDir: !options.dev && existsSync(publicDir) && publicDir publicDir: !options.dev && existsSync(publicDir) && publicDir
}] }
})) }
const layers = Object.keys(aliases).sort((a, b) => b.length - a.length) const layers = Object.keys(aliases).sort((a, b) => b.length - a.length)
return { return {

View File

@ -155,7 +155,12 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir) const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
const getImportName = (name: string) => (name[0] === '.' ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '') const getImportName = (name: string) => (name[0] === '.' ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '')
const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)]) const modules = moduleInfo.map(meta => [genString(meta.configKey), getImportName(meta.importName)])
const privateRuntimeConfig = Object.create(null)
for (const key in nuxt.options.runtimeConfig) {
if (key !== 'public') {
privateRuntimeConfig[key] = nuxt.options.runtimeConfig[key]
}
}
return [ return [
"import { NuxtModule, RuntimeConfig } from 'nuxt/schema'", "import { NuxtModule, RuntimeConfig } from 'nuxt/schema'",
"declare module 'nuxt/schema' {", "declare module 'nuxt/schema' {",
@ -165,7 +170,7 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
), ),
modules.length > 0 ? ` modules?: (undefined | null | false | NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName]) => `[${genString(importName)}, Exclude<NuxtConfig[${configKey}], boolean>]`).join(' | ')})[],` : '', modules.length > 0 ? ` modules?: (undefined | null | false | NuxtModule | string | [NuxtModule | string, Record<string, any>] | ${modules.map(([configKey, importName]) => `[${genString(importName)}, Exclude<NuxtConfig[${configKey}], boolean>]`).join(' | ')})[],` : '',
' }', ' }',
generateTypes(await resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public')) as Record<string, JSValue>), generateTypes(await resolveSchema(privateRuntimeConfig as Record<string, JSValue>),
{ {
interfaceName: 'RuntimeConfig', interfaceName: 'RuntimeConfig',
addExport: false, addExport: false,

View File

@ -12,8 +12,16 @@ import type {
Target Target
} from './types' } from './types'
const removeUndefinedProps = (props: Props) => const removeUndefinedProps = (props: Props) => {
Object.fromEntries(Object.entries(props).filter(([, value]) => value !== undefined)) const filteredProps = Object.create(null)
for (const key in props) {
const value = props[key]
if (value !== undefined) {
filteredProps[key] = value;
}
}
return filteredProps
}
const setupForUseMeta = (metaFactory: (props: Props, ctx: SetupContext) => Record<string, any>, renderChild?: boolean) => (props: Props, ctx: SetupContext) => { const setupForUseMeta = (metaFactory: (props: Props, ctx: SetupContext) => Record<string, any>, renderChild?: boolean) => (props: Props, ctx: SetupContext) => {
useHead(() => metaFactory({ ...removeUndefinedProps(props), ...ctx.attrs }, ctx)) useHead(() => metaFactory({ ...removeUndefinedProps(props), ...ctx.attrs }, ctx))

View File

@ -22,7 +22,10 @@ const NUXT_LIB_RE = /node_modules\/(nuxt|nuxt3|nuxt-nightly)\//
const SUPPORTED_EXT_RE = /\.(m?[jt]sx?|vue)/ const SUPPORTED_EXT_RE = /\.(m?[jt]sx?|vue)/
export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions) => { export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions) => {
const composableMeta = Object.fromEntries(options.composables.map(({ name, ...meta }) => [name, meta])) const composableMeta: Record<string, any> = {}
for (const { name, ...meta } of options.composables) {
composableMeta[name] = meta
}
const maxLength = Math.max(...options.composables.map(({ argumentLength }) => argumentLength)) const maxLength = Math.max(...options.composables.map(({ argumentLength }) => argumentLength))
const keyedFunctions = new Set(options.composables.map(({ name }) => name)) const keyedFunctions = new Set(options.composables.map(({ name }) => name))