perf(nuxt): simplify generated variable names (#18629)

This commit is contained in:
Daniel Roe 2023-01-30 12:24:58 -08:00 committed by GitHub
parent c9d0b2f6be
commit b7591e639d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 4 deletions

View File

@ -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<TemplateContext> = {
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<TemplateContext> = {
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))
}

View File

@ -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<string> = new Set()): { imports: Set<string>, 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 || []`