Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(nuxt): prefer iterating rather than Object.fromEntries #24953

Merged
merged 24 commits into from Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
2f32758
perf(nuxt): refactor Object.fromEntries
GalacticHypernova Dec 29, 2023
cfefc75
chore: add return and remove fromEntries
GalacticHypernova Dec 29, 2023
dc4081a
Merge branch 'main' into patch-13
GalacticHypernova Dec 29, 2023
017a668
feat!: vite object.entries revamp !!MAY BREAK!!
GalacticHypernova Jan 3, 2024
e20f0ac
Merge branch 'main' into patch-13
GalacticHypernova Jan 3, 2024
47655f7
Update analyze.ts
GalacticHypernova Jan 3, 2024
f62cb8d
Update nitro for object
GalacticHypernova Jan 4, 2024
2b1c18b
chore: fix types
GalacticHypernova Jan 4, 2024
6a31413
Update nitro.ts
GalacticHypernova Jan 4, 2024
c70c403
Merge branch 'nuxt:main' into patch-13
GalacticHypernova Jan 4, 2024
bbb8996
[autofix.ci] apply automated fixes
autofix-ci[bot] Jan 4, 2024
a4b86af
object.fromentries
GalacticHypernova Jan 4, 2024
58105ab
chore: lint
GalacticHypernova Jan 4, 2024
00e4248
composable object.fromEntries
GalacticHypernova Jan 4, 2024
a138fca
template object.fromentries
GalacticHypernova Jan 4, 2024
2b77de6
layer-aliasing object.fromentries
GalacticHypernova Jan 4, 2024
640ce85
Merge branch 'main' into patch-13
GalacticHypernova Jan 5, 2024
f7b80bb
Merge branch 'main' into patch-13
GalacticHypernova Jan 5, 2024
80f4ca6
Merge branch 'main' into patch-13
GalacticHypernova Jan 5, 2024
17387d3
refactor: avoid reduce
danielroe Jan 5, 2024
5c8c7ae
Merge branch 'main' into patch-13
GalacticHypernova Jan 5, 2024
3f0bbb5
refactor: use boolean
danielroe Jan 8, 2024
2bc6e86
chore: revert less significant instances
danielroe Jan 8, 2024
238ba4b
Merge remote-tracking branch 'origin/main' into patch-13
danielroe Jan 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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