mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 15:50:32 +00:00
perf(nuxt): simplify generated variable names (#18629)
This commit is contained in:
parent
c9d0b2f6be
commit
b7591e639d
@ -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))
|
||||
}
|
||||
|
@ -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 || []`
|
||||
|
Loading…
Reference in New Issue
Block a user