perf: unify shared patterns

This commit is contained in:
tbitw2549 2024-10-20 21:28:29 +03:00
parent f49c0497ad
commit dc4d032246
8 changed files with 13 additions and 11 deletions

View File

@ -114,7 +114,7 @@ export interface NavigateToOptions {
open?: OpenOptions
}
const QUOTE_RE = /"/g
const URL_QUOTE_RE = /"/g
/** @since 3.0.0 */
export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: NavigateToOptions): Promise<void | NavigationFailure | false> | false | void | RouteLocationRaw => {
if (!to) {
@ -167,7 +167,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
const redirect = async function (response: any) {
// TODO: consider deprecating in favour of `app:rendered` and removing
await nuxtApp.callHook('app:redirected')
const encodedLoc = location.replace(QUOTE_RE, '%22')
const encodedLoc = location.replace(URL_QUOTE_RE, '%22')
const encodedHeader = encodeURL(location, isExternalHost)
nuxtApp.ssrContext!._renderResponse = {

View File

@ -6,7 +6,7 @@ import { relative } from 'pathe'
import type { Component, ComponentsOptions } from 'nuxt/schema'
import { logger, tryUseNuxt } from '@nuxt/kit'
import { isVue } from '../../core/utils'
import { isVue, QUOTE_RE } from '../../core/utils'
interface LoaderOptions {
getComponents (): Component[]
@ -112,7 +112,6 @@ export const LoaderPlugin = (options: LoaderOptions) => createUnplugin(() => {
}
})
const QUOTE_RE = /["']/g
function findComponent (components: Component[], name: string, mode: LoaderOptions['mode']) {
const id = pascalCase(name).replace(QUOTE_RE, '')
// Prefer exact match

View File

@ -6,13 +6,12 @@ import { isIgnored, logger, useNuxt } from '@nuxt/kit'
import { withTrailingSlash } from 'ufo'
import type { Component, ComponentsDir } from 'nuxt/schema'
import { resolveComponentNameSegments } from '../core/utils'
import { QUOTE_RE, resolveComponentNameSegments } from '../core/utils'
const ISLAND_RE = /\.island(?:\.global)?$/
const GLOBAL_RE = /\.global(?:\.island)?$/
const COMPONENT_MODE_RE = /(?<=\.)(client|server)(\.global|\.island)*$/
const MODE_REPLACEMENT_RE = /(\.(client|server))?(\.global|\.island)*$/
const QUOTE_RE = /["']/g
/**
* Scan the components inside different components folders
* and return a unique list of components

View File

@ -18,6 +18,7 @@ import { distDir } from '../dirs'
import { toArray } from '../utils'
import { template as defaultSpaLoadingTemplate } from '../../../ui-templates/dist/templates/spa-loading-icon'
import { nuxtImportProtections } from './plugins/import-protection'
import { EXTENSION_RE } from './utils'
const logLevelMapReverse = {
silent: 0,
@ -374,7 +375,6 @@ export async function initNitro (nuxt: Nuxt & { _nitro?: Nitro }) {
// TODO: extract to shared utility?
const excludedAlias = [/^@vue\/.*$/, '#imports', 'vue-demi', /^#app/]
const EXTENSION_RE = /\b\.\w+$/g
const basePath = nitroConfig.typescript!.tsConfig!.compilerOptions?.baseUrl ? resolve(nuxt.options.buildDir, nitroConfig.typescript!.tsConfig!.compilerOptions?.baseUrl) : nuxt.options.buildDir
const aliases = nitroConfig.alias!
const tsConfig = nitroConfig.typescript!.tsConfig!

View File

@ -11,6 +11,7 @@ import type { NuxtTemplate } from 'nuxt/schema'
import type { Nitro } from 'nitro/types'
import { annotatePlugins, checkForCircularDependencies } from './app'
import { EXTENSION_RE } from './utils'
export const vueShim: NuxtTemplate = {
filename: 'types/vue-shim.d.ts',
@ -378,12 +379,11 @@ export const clientConfigTemplate: NuxtTemplate = {
},
}
const FILENAME_EXTENSION_RE = /\b\.\w+$/g
export const appConfigDeclarationTemplate: NuxtTemplate = {
filename: 'types/app.config.d.ts',
getContents ({ app, nuxt }) {
const typesDir = join(nuxt.options.buildDir, 'types')
const configPaths = app.configs.map(path => relative(typesDir, path).replace(FILENAME_EXTENSION_RE, ''))
const configPaths = app.configs.map(path => relative(typesDir, path).replace(EXTENSION_RE, ''))
return `
import type { CustomAppConfig } from 'nuxt/schema'

View File

@ -14,3 +14,6 @@ export function uniqueBy<T, K extends keyof T> (arr: T[], key: K) {
}
return res
}
export const QUOTE_RE = /["']/g
export const EXTENSION_RE = /\b\.\w+$/g

View File

@ -1,8 +1,8 @@
import { basename, dirname, extname, normalize } from 'pathe'
import { kebabCase, splitByCase } from 'scule'
import { withTrailingSlash } from 'ufo'
import { QUOTE_RE } from '.'
const QUOTE_RE = /["']/g
export function getNameFromPath (path: string, relativeTo?: string) {
const relativePath = relativeTo
? normalize(path).replace(withTrailingSlash(normalize(relativeTo)), '')

View File

@ -177,8 +177,9 @@ export const PageMetaPlugin = (options: PageMetaPluginOptions) => createUnplugin
// https://github.com/vuejs/vue-loader/pull/1911
// https://github.com/vitejs/vite/issues/8473
const QUERY_START_RE = /^\?/
const MACRO_RE = /&macro=true/
function rewriteQuery (id: string) {
return id.replace(/\?.+$/, r => '?macro=true&' + r.replace(QUERY_START_RE, '').replace(/&macro=true/, ''))
return id.replace(/\?.+$/, r => '?macro=true&' + r.replace(QUERY_START_RE, '').replace(MACRO_RE, ''))
}
function parseMacroQuery (id: string) {