From b7591e639dac2557251c8f75214f88492a88a246 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 30 Jan 2023 12:24:58 -0800 Subject: [PATCH] perf(nuxt): simplify generated variable names (#18629) --- packages/nuxt/src/core/templates.ts | 5 +++-- packages/nuxt/src/pages/utils.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/nuxt/src/core/templates.ts b/packages/nuxt/src/core/templates.ts index d69a997bc..5be03f559 100644 --- a/packages/nuxt/src/core/templates.ts +++ b/packages/nuxt/src/core/templates.ts @@ -6,6 +6,7 @@ import escapeRE from 'escape-string-regexp' import { hash } from 'ohash' import { camelCase } from 'scule' import { resolvePath } from 'mlly' +import { filename } from 'pathe/utils' export interface TemplateContext { nuxt: Nuxt @@ -53,7 +54,7 @@ export const clientPluginTemplate: NuxtTemplate = { const imports: string[] = [] for (const plugin of clientPlugins) { const path = relative(ctx.nuxt.options.rootDir, plugin.src) - const variable = genSafeVariableName(path).replace(/_(45|46|47)/g, '_') + '_' + hash(path) + const variable = genSafeVariableName(filename(plugin.src)).replace(/_(45|46|47)/g, '_') + '_' + hash(path) exports.push(variable) imports.push(genImport(plugin.src, variable)) } @@ -72,7 +73,7 @@ export const serverPluginTemplate: NuxtTemplate = { 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) + const variable = genSafeVariableName(filename(path)).replace(/_(45|46|47)/g, '_') + '_' + hash(path) exports.push(variable) imports.push(genImport(plugin.src, variable)) } diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index ea711ee5a..54f64e518 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -4,6 +4,8 @@ import type { NuxtPage } from '@nuxt/schema' import { resolveFiles, useNuxt } from '@nuxt/kit' import { genImport, genDynamicImport, genArrayFromRaw, genSafeVariableName } from 'knitwork' import escapeRE from 'escape-string-regexp' +import { filename } from 'pathe/utils' +import { hash } from 'ohash' import { uniqueBy } from '../core/utils' enum SegmentParserState { @@ -223,12 +225,11 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage) { } export function normalizeRoutes (routes: NuxtPage[], metaImports: Set = new Set()): { imports: Set, routes: string } { - const nuxt = useNuxt() return { imports: metaImports, routes: genArrayFromRaw(routes.map((page) => { const file = normalize(page.file) - const metaImportName = genSafeVariableName(relative(nuxt.options.rootDir, file)) + 'Meta' + const metaImportName = genSafeVariableName(filename(file) + hash(file)) + 'Meta' metaImports.add(genImport(`${file}?macro=true`, [{ name: 'default', as: metaImportName }])) let aliasCode = `${metaImportName}?.alias || []`