Skip to content

Commit

Permalink
perf(nuxt): iterate rather than using Object.fromEntries (#24953)
Browse files Browse the repository at this point in the history
  • Loading branch information
GalacticHypernova committed Jan 9, 2024
1 parent 4168157 commit 3b94883
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 26 deletions.
21 changes: 11 additions & 10 deletions packages/nuxt/src/core/nitro.ts
Expand Up @@ -260,16 +260,17 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
const _routeRules = nitro.options.routeRules
for (const key in _routeRules) {
if (key === '/__nuxt_error') { continue }
const filteredRules = Object.entries(_routeRules[key])
.filter(([key, value]) => ['prerender', 'redirect'].includes(key) && value)
.map(([key, value]: any) => {
if (key === 'redirect') {
return [key, typeof value === 'string' ? value : value.to]
}
return [key, value]
})
if (filteredRules.length > 0) {
routeRules[key] = Object.fromEntries(filteredRules)
let hasRules = false
const filteredRules = {} as Record<string, any>
for (const routeKey in _routeRules[key]) {
const value = (_routeRules as any)[key][routeKey]
if (['prerender', 'redirect'].includes(routeKey) && value) {
filteredRules[routeKey] = routeKey === 'redirect' ? typeof value === 'string' ? value : value.to : value
hasRules = true
}
}
if (hasRules) {
routeRules[key] = filteredRules
}
}

Expand Down
23 changes: 12 additions & 11 deletions packages/nuxt/src/core/plugins/layer-aliasing.ts
Expand Up @@ -17,22 +17,23 @@ const ALIAS_RE = /(?<=['"])[~@]{1,2}(?=\/)/g
const ALIAS_RE_SINGLE = /(?<=['"])[~@]{1,2}(?=\/)/

export const LayerAliasingPlugin = createUnplugin((options: LayerAliasingOptions) => {
const aliases = Object.fromEntries(options.layers.map((l) => {
const srcDir = l.config.srcDir || l.cwd
const rootDir = l.config.rootDir || l.cwd
const publicDir = join(srcDir, l.config?.dir?.public || 'public')
const aliases: Record<string, { aliases: Record<string, string>, prefix: string, publicDir: false | string }> = {}
for (const layer of options.layers) {
const srcDir = layer.config.srcDir || layer.cwd
const rootDir = layer.config.rootDir || layer.cwd
const publicDir = join(srcDir, layer.config?.dir?.public || 'public')

return [srcDir, {
aliases[srcDir] = {
aliases: {
'~': l.config?.alias?.['~'] || srcDir,
'@': l.config?.alias?.['@'] || srcDir,
'~~': l.config?.alias?.['~~'] || rootDir,
'@@': l.config?.alias?.['@@'] || rootDir
'~': layer.config?.alias?.['~'] || srcDir,
'@': layer.config?.alias?.['@'] || srcDir,
'~~': layer.config?.alias?.['~~'] || rootDir,
'@@': layer.config?.alias?.['@@'] || rootDir
},
prefix: relative(options.root, publicDir),
publicDir: !options.dev && existsSync(publicDir) && publicDir
}]
}))
}
}
const layers = Object.keys(aliases).sort((a, b) => b.length - a.length)

return {
Expand Down
9 changes: 7 additions & 2 deletions packages/nuxt/src/core/templates.ts
Expand Up @@ -155,7 +155,12 @@ export const schemaTemplate: NuxtTemplate<TemplateContext> = {
const relativeRoot = relative(resolve(nuxt.options.buildDir, 'types'), nuxt.options.rootDir)
const getImportName = (name: string) => (name[0] === '.' ? './' + join(relativeRoot, name) : name).replace(/\.\w+$/, '')
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 [
"import { NuxtModule, RuntimeConfig } from 'nuxt/schema'",
"declare module 'nuxt/schema' {",
Expand All @@ -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(' | ')})[],` : '',
' }',
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',
addExport: false,
Expand Down
12 changes: 10 additions & 2 deletions packages/nuxt/src/head/runtime/components.ts
Expand Up @@ -12,8 +12,16 @@ import type {
Target
} from './types'

const removeUndefinedProps = (props: Props) =>
Object.fromEntries(Object.entries(props).filter(([, value]) => value !== undefined))
const removeUndefinedProps = (props: Props) => {
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) => {
useHead(() => metaFactory({ ...removeUndefinedProps(props), ...ctx.attrs }, ctx))
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/plugins/composable-keys.ts
Expand Up @@ -22,7 +22,10 @@ const NUXT_LIB_RE = /node_modules\/(nuxt|nuxt3|nuxt-nightly)\//
const SUPPORTED_EXT_RE = /\.(m?[jt]sx?|vue)/

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 keyedFunctions = new Set(options.composables.map(({ name }) => name))
Expand Down

0 comments on commit 3b94883

Please sign in to comment.